github.com/m3db/m3@v1.5.0/src/query/api/v1/middleware/tracing.go (about)

     1  // Copyright (c) 2021 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package middleware
    22  
    23  import (
    24  	"fmt"
    25  	"net/http"
    26  	"strconv"
    27  
    28  	"github.com/gorilla/mux"
    29  	"github.com/opentracing-contrib/go-stdlib/nethttp"
    30  	"github.com/opentracing/opentracing-go"
    31  	"github.com/opentracing/opentracing-go/mocktracer"
    32  	"github.com/uber/jaeger-client-go"
    33  	"go.uber.org/zap"
    34  
    35  	"github.com/m3db/m3/src/query/util/logging"
    36  	"github.com/m3db/m3/src/x/instrument"
    37  )
    38  
    39  // Tracing applies OpenTracing compatible middleware, which will start a span
    40  // for each incoming request. Additionally if iOpts is non-nil the trace_id and span_id are added as fields to the
    41  // request scoped logger.
    42  func Tracing(tracer opentracing.Tracer, iOpts instrument.Options) mux.MiddlewareFunc {
    43  	return func(base http.Handler) http.Handler {
    44  		return nethttp.MiddlewareFunc(
    45  			tracer,
    46  			func(w http.ResponseWriter, r *http.Request) {
    47  				span := opentracing.SpanFromContext(r.Context())
    48  				if span != nil && iOpts != nil {
    49  					var (
    50  						traceID string
    51  						spanID  string
    52  					)
    53  					switch sCtx := span.Context().(type) {
    54  					case mocktracer.MockSpanContext:
    55  						traceID = strconv.Itoa(sCtx.TraceID)
    56  						spanID = strconv.Itoa(sCtx.SpanID)
    57  					case jaeger.SpanContext:
    58  						traceID = sCtx.TraceID().String()
    59  						spanID = sCtx.SpanID().String()
    60  					}
    61  					if spanID != "" && traceID != "" {
    62  						r = r.WithContext(logging.NewContext(r.Context(), iOpts,
    63  							zap.String("trace_id", traceID),
    64  							zap.String("span_id", spanID)))
    65  					}
    66  				}
    67  				base.ServeHTTP(w, r)
    68  			},
    69  			nethttp.OperationNameFunc(func(r *http.Request) string {
    70  				return fmt.Sprintf("%s %s", r.Method, r.URL.Path)
    71  			}))
    72  	}
    73  }