gitlab.com/gitlab-org/labkit@v1.21.0/tracing/inbound_http.go (about) 1 package tracing 2 3 import ( 4 "net/http" 5 6 opentracing "github.com/opentracing/opentracing-go" 7 "github.com/opentracing/opentracing-go/ext" 8 "gitlab.com/gitlab-org/labkit/correlation" 9 ) 10 11 // Handler will extract tracing from inbound request. 12 func Handler(h http.Handler, opts ...HandlerOption) http.Handler { 13 config := applyHandlerOptions(opts) 14 15 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 16 if isHealthCheck(r) { 17 h.ServeHTTP(w, r) 18 return 19 } 20 21 tracer := opentracing.GlobalTracer() 22 if tracer == nil { 23 h.ServeHTTP(w, r) 24 return 25 } 26 27 wireContext, _ := tracer.Extract( 28 opentracing.HTTPHeaders, 29 opentracing.HTTPHeadersCarrier(r.Header)) 30 31 // Create the span referring to the RPC client if available. 32 // If wireContext == nil, a root span will be created. 33 additionalStartSpanOpts := []opentracing.StartSpanOption{ 34 ext.RPCServerOption(wireContext), 35 } 36 37 correlationID := correlation.ExtractFromContext(r.Context()) 38 if correlationID != "" { 39 additionalStartSpanOpts = append(additionalStartSpanOpts, opentracing.Tag{Key: "correlation_id", Value: correlationID}) 40 } 41 42 serverSpan := opentracing.StartSpan( 43 config.getOperationName(r), 44 additionalStartSpanOpts..., 45 ) 46 serverSpan.SetTag("http.method", r.Method) 47 serverSpan.SetTag("http.url", r.URL) 48 defer serverSpan.Finish() 49 50 ctx := opentracing.ContextWithSpan(r.Context(), serverSpan) 51 52 h.ServeHTTP(w, r.WithContext(ctx)) 53 }) 54 } 55 56 func isHealthCheck(r *http.Request) bool { 57 return r.URL.Path == "/-/liveness" || r.URL.Path == "/-/readiness" 58 }