-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.go
42 lines (33 loc) · 1.05 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
35
36
37
38
39
40
41
42
package tracing
import (
"net/http"
opentracing "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/urfave/negroni"
)
func (t *Tracer) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
var span opentracing.Span
opName := r.URL.Path
// It's very possible that Hydra is fronted by a proxy which could have initiated a trace.
// If so, we should attempt to join it.
remoteContext, err := opentracing.GlobalTracer().Extract(
opentracing.HTTPHeaders,
opentracing.HTTPHeadersCarrier(r.Header),
)
if err != nil {
span = opentracing.StartSpan(opName)
} else {
span = opentracing.StartSpan(opName, opentracing.ChildOf(remoteContext))
}
defer span.Finish()
r = r.WithContext(opentracing.ContextWithSpan(r.Context(), span))
next(rw, r)
ext.HTTPMethod.Set(span, r.Method)
if negroniWriter, ok := rw.(negroni.ResponseWriter); ok {
statusCode := uint16(negroniWriter.Status())
if statusCode >= 400 {
ext.Error.Set(span, true)
}
ext.HTTPStatusCode.Set(span, statusCode)
}
}