-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.go
34 lines (29 loc) · 1.08 KB
/
middleware.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
package OpenTracing
import (
"fmt"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"net/http"
)
// Middleware - create a new openTracing span and put it into request context
func Middleware(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
var span opentracing.Span
ctx := r.Context()
carrier := opentracing.HTTPHeadersCarrier(r.Header)
wireContext, err := opentracing.GlobalTracer().Extract(opentracing.HTTPHeaders, carrier)
if err != nil {
span, ctx = opentracing.StartSpanFromContext(r.Context(), fmt.Sprintf("%s: %s", r.Method, r.URL.Path))
} else {
span, ctx = opentracing.StartSpanFromContext(r.Context(), fmt.Sprintf("%s: %s", r.Method, r.URL.Path), opentracing.ChildOf(wireContext))
}
defer span.Finish()
if err := span.Tracer().Inject(span.Context(), opentracing.HTTPHeaders, carrier); err != nil {
span.SetTag(string(ext.Error), true)
} else {
r = r.WithContext(opentracing.ContextWithSpan(ctx, span))
}
next.ServeHTTP(w, r.WithContext(ctx))
}
return http.HandlerFunc(fn)
}