-
Notifications
You must be signed in to change notification settings - Fork 0
/
omux.go
50 lines (40 loc) · 1.04 KB
/
omux.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
package omux
import (
"net/http"
"github.com/gorilla/mux"
"github.com/opentracing/opentracing-go/ext"
"github.com/opentracing/opentracing-go"
)
const (
UnknownMuxRoute = "UNKNOWN"
)
func TracingMiddleware(next http.HandlerFunc) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var span opentracing.Span
name := getName(r)
wireContext, err := opentracing.GlobalTracer().Extract(
opentracing.HTTPHeaders,
opentracing.HTTPHeadersCarrier(r.Header),
)
if err != nil {
// TODO do something with the errors, in particular opentracing.ErrorSpanContextNotFound
}
span = opentracing.StartSpan(
name,
ext.RPCServerOption(wireContext),
)
defer span.Finish()
ctx := opentracing.ContextWithSpan(r.Context(), span)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func getName(r *http.Request) string {
route := mux.CurrentRoute(r)
if name := route.GetName(); name != "" {
return name
}
if regex, err := route.GetPathRegexp(); err != nil {
return regex
}
return UnknownMuxRoute
}