-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.go
43 lines (38 loc) · 1.25 KB
/
server.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
// +build go1.7
package gorilla
import (
"net/http"
"github.com/gorilla/mux"
"github.com/opentracing-contrib/go-stdlib/nethttp"
opentracing "github.com/opentracing/opentracing-go"
)
const defaultComponentName = "github.com/gorilla/mux"
// Middleware wraps an http.Handler and traces incoming requests.
// Additionally, it adds the span to the request's context.
//
// By default, the operation name of the spans is set to "HTTP {method}".
// This can be overriden with options.
//
// The options allow fine tuning the behavior of the middleware.
//
// Example:
// pattern := "/api/custerms/{id}"
// mw := gorilla.Middleware(
// tracer,
// handler,
// )
// r := mux.NewRouter()
// r.HandleFunc(pattern, mw)
func Middleware(tr opentracing.Tracer, h http.Handler, options ...nethttp.MWOption) http.Handler {
opNameFunc := func(r *http.Request) string {
if route := mux.CurrentRoute(r); route != nil {
if tpl, err := route.GetPathTemplate(); err == nil {
return r.Proto + " " + r.Method + " " + tpl
}
}
return r.Proto + " " + r.Method
}
var opts = []nethttp.MWOption{nethttp.OperationNameFunc(opNameFunc), nethttp.MWComponentName(defaultComponentName)}
opts = append(opts, options...)
return nethttp.Middleware(tr, h, opts...)
}