Last active
July 8, 2016 20:16
-
-
Save bg451/866aa164006f844953a679a8338ec621 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Acts as our index page | |
func indexHandler(w http.ResponseWriter, r *http.Request) { | |
w.Write([]byte(`<a href="/home"> Click here to start a request </a>`)) | |
} | |
func homeHandler(w http.ResponseWriter, r *http.Request) { | |
w.Write([]byte("Request started")) | |
go func() { | |
http.Get("http://localhost:8080/async") | |
}() | |
http.Get("http://localhost:8080/service") | |
time.Sleep(time.Duration(rand.Intn(200)) * time.Millisecond) | |
w.Write([]byte("Request done!")) | |
} | |
// Mocks a service endpoint that makes a DB call | |
func serviceHandler(w http.ResponseWriter, r *http.Request) { | |
// ... | |
http.Get("http://localhost:8080/db") | |
time.Sleep(time.Duration(rand.Intn(200)) * time.Millisecond) | |
// ... | |
} | |
// Mocks a DB call | |
func dbHandler(w http.ResponseWriter, r *http.Request) { | |
time.Sleep(time.Duration(rand.Intn(200)) * time.Millisecond) | |
// here would be the actual call to a DB. | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func main() { | |
port := 8080 | |
addr := fmt.Sprintf(":%d", port) | |
mux := http.NewServeMux() | |
mux.HandleFunc("/", indexHandler) | |
mux.HandleFunc("/home", homeHandler) | |
mux.HandleFunc("/async", serviceHandler) | |
mux.HandleFunc("/service", serviceHandler) | |
mux.HandleFunc("/db", dbHandler) | |
fmt.Printf("Go to http://localhost:%d/home to start a request!\n", *port) | |
log.Fatal(http.ListenAndServe(addr, mux)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func homeHandler(w http.ResponseWriter, r *http.Request) { | |
span := opentracing.StartSpan("/home") // Start a span using the global, in this case noop, tracer | |
defer span.Finish() | |
// ... the rest of the function | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
// We record any errors now. | |
_, err := http.Get("http://localhost:8080/service") | |
if err != nil { | |
span.SetTag("error", true) // Tag the span as errored | |
span.LogEvent(fmt.Sprintf("GET service error: %v", err)) // Log the error | |
} | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func homeHandler(w http.ResponseWriter, r *http.Request) { | |
w.Write([]byte("Request started")) | |
span := opentracing.StartSpan("/home") | |
defer span.Finish() | |
// Since we have to inject our span into the HTTP headers, we create a request | |
asyncReq, _ := http.NewRequest("GET", "http://localhost:8080/async", nil) | |
// Inject the span context into the header | |
err := sp.Tracer().Inject(sp.Context(), | |
opentracing.TextMap, | |
opentracing.HTTPHeaderTextMapCarrier(asyncReq.Header)) | |
if err != nil { | |
log.Fatalf("Could not inject span context into header: %v", err) | |
} | |
go func() { | |
if _, err := http.DefaultClient.Do(asyncReq); err != nil { | |
span.SetTag("error", true) | |
span.LogEvent(fmt.Sprintf("GET /async error: %v", err)) | |
} | |
}() | |
// Repeat for the /service call. | |
// .... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func serviceHandler(w http.ResponseWriter, r *http.Request) { | |
var sp opentracing.Span | |
opName := r.URL.Path | |
// Attempt to join a trace by getting trace context from the headers. | |
wireContext, err := opentracing.GlobalTracer().Join(opName, | |
opentracing.TextMap, | |
opentracing.HTTPHeaderTextMapCarrier(r.Header)) | |
if err != nil || wireContext == nil { | |
// If for whatever reason we can't join, go ahead an start a new | |
sp = opentracing.StartSpan(opName) | |
} else { | |
sp = opentracing.StartSpan(opName, opentracing.ChildOf(wireContext)) | |
} | |
defer sp.Finish() | |
// ... rest of the function |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Import ( | |
“sourcegraph.com/sourcegraph/appdash” | |
“sourcegraph.com/sourcegraph/appdash/traceapp” | |
appdashot "sourcegraph.com/sourcegraph/appdash/opentracing" | |
) | |
func main() { | |
// ... | |
store := appdash.NewMemoryStore() | |
// Listen on any available TCP port locally. | |
l, err := net.ListenTCP("tcp", &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 0}) | |
if err != nil { | |
log.Fatal(err) | |
} | |
collectorPort := l.Addr().(*net.TCPAddr).Port | |
// Start an Appdash collection server that will listen for spans and | |
// annotations and add them to the local collector (stored in-memory). | |
cs := appdash.NewServer(l, appdash.NewLocalCollector(store)) | |
go cs.Start() | |
// Print the URL at which the web UI will be running. | |
appdashURLStr := fmt.Sprintf("http://localhost:%d", 8700) | |
appdashURL, err := url.Parse(appdashURLStr) | |
if err != nil { | |
log.Fatalf("Error parsing %s: %s", appdashURLStr, err) | |
} | |
fmt.Printf("To see your traces, go to %s/traces\n", appdashURL) | |
// Start the web UI in a separate goroutine. | |
tapp := traceapp.New(nil) | |
tapp.Store = store | |
tapp.Queryer = store | |
go func() { | |
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", appdashPort), tapp)) | |
}() | |
tracer: = appdashot.NewTracer(appdash.NewRemoteCollector(addr)) | |
opentracing.InitGlobalTracer(tracer) | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment