Skip to content

Commit

Permalink
Add an example on how to instrument a 3rd-party HTTP router
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Slotin committed Jul 10, 2020
1 parent 1cd70f6 commit e25b01e
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions example_httpserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,29 @@ func Example_tracingHandlerFunc() {
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)
}
}

0 comments on commit e25b01e

Please sign in to comment.