-
Notifications
You must be signed in to change notification settings - Fork 3
/
http_middleware.go
42 lines (36 loc) · 1.37 KB
/
http_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 otel
import (
"context"
"net/http"
rrcontext "github.com/roadrunner-server/context"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
)
// type alias for the middleware
type httpMiddleware func(http.Handler) http.Handler
func HTTPHandler(next http.Handler, middleware httpMiddleware) http.Handler {
return middleware(next)
}
func httpWrapper(prop propagation.TextMapPropagator, tr trace.TracerProvider, sn string) httpMiddleware {
return func(h http.Handler) http.Handler {
// init otelhttp handler only once
handler := otelhttp.NewHandler(h, "",
otelhttp.WithSpanNameFormatter(func(_ string, r *http.Request) string {
return r.RequestURI
}),
otelhttp.WithSpanOptions(
trace.WithSpanKind(trace.SpanKindServer),
),
otelhttp.WithPropagators(prop),
otelhttp.WithTracerProvider(tr),
otelhttp.WithMessageEvents(otelhttp.ReadEvents, otelhttp.WriteEvents))
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(r.Context(), rrcontext.OtelTracerNameKey, sn)
// have effect only if the span started outside
// if the OTEL middleware is the first in the line, has no effect (we haven't yet started a span)
prop.Inject(ctx, propagation.HeaderCarrier(r.Header))
handler.ServeHTTP(w, r.WithContext(ctx))
})
}
}