-
Notifications
You must be signed in to change notification settings - Fork 37
/
handler.go
41 lines (32 loc) · 1009 Bytes
/
handler.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
// (c) Copyright IBM Corp. 2021
// (c) Copyright Instana Inc. 2016
//go:build go1.12
// +build go1.12
package instamux
import (
"net/http"
"github.com/gorilla/mux"
instana "github.com/instana/go-sensor"
)
// NewRouter is wrapper for mux.NewRouter()
func NewRouter(sensor instana.TracerLogger) *mux.Router {
r := mux.NewRouter()
AddMiddleware(sensor, r)
return r
}
// AddMiddleware instruments the mux.Router instance with Instana
func AddMiddleware(sensor instana.TracerLogger, router *mux.Router) {
router.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
r := mux.CurrentRoute(req)
pathTemplate, err := r.GetPathTemplate()
if err != nil {
sensor.Logger().Debug("can not get path template from the route: ", err)
pathTemplate = ""
}
instana.TracingNamedHandlerFunc(sensor, r.GetName(), pathTemplate, func(w http.ResponseWriter, req *http.Request) {
next.ServeHTTP(w, req)
})(w, req)
})
})
}