-
Notifications
You must be signed in to change notification settings - Fork 39
/
example_httpserver_test.go
66 lines (53 loc) · 2.48 KB
/
example_httpserver_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// (c) Copyright IBM Corp. 2021
// (c) Copyright Instana Inc. 2020
package instana_test
import (
"log"
"net/http"
instana "github.com/instana/go-sensor"
"github.com/opentracing/opentracing-go"
)
// This example shows how to instrument an HTTP server with Instana tracing
func Example_tracingNamedHandlerFunc() {
sensor := instana.NewSensor("my-http-server")
// To instrument a handler function, pass it as an argument to instana.TracingHandlerFunc()
http.HandleFunc("/", instana.TracingHandlerFunc(sensor, "/", func(w http.ResponseWriter, req *http.Request) {
// Extract the parent span and use its tracer to initialize any child spans to trace the calls
// inside the handler, e.g. database queries, 3rd-party API requests, etc.
if parent, ok := instana.SpanFromContext(req.Context()); ok {
sp := parent.Tracer().StartSpan("index", opentracing.ChildOf(parent.Context()))
defer sp.Finish()
}
// ...
w.Write([]byte("OK"))
}))
// In case your handler is implemented as an http.Handler, pass its ServeHTTP method instead.
// You can also use instana.TracingNamedHandlerFunc() to provide a unique route identifier to
// group the calls to this route later in Instana UI.
http.HandleFunc("/files", instana.TracingNamedHandlerFunc(sensor, "index", "/:path", http.FileServer(http.Dir("./")).ServeHTTP))
if err := http.ListenAndServe(":0", nil); err != nil {
log.Fatalf("failed to start server: %s", err)
}
}
// This example demonstrates how to instrument a 3rd-party HTTP router that uses pattern matching to make sure
// the original path template is forwarded to Instana
func Example_httpRoutePatternMatching() {
sensor := instana.NewSensor("my-http-server")
// Initialize your router. Here for simplicity we use stdlib http.ServeMux, however you
// can use any router that supports http.Handler/http.HandlerFunc, such as github.com/gorilla/mux
r := http.NewServeMux()
// Wrap the handler using instana.TracingHandlerFunc() and pass the path template
// used to route requests to it. This value will be attached to the span and displayed
// in UI as `http.path_tpl` if the request path is different (we assume that this request
// has been routed to the handler via a matched pattern)
r.HandleFunc("/articles/{category}/{id:[0-9]+}", instana.TracingHandlerFunc(
sensor,
"/articles/{category}/{id:[0-9]+}",
func(w http.ResponseWriter, req *http.Request) {
// ...
},
))
if err := http.ListenAndServe(":0", nil); err != nil {
log.Fatalf("failed to start server: %s", err)
}
}