/* * @Date: 2021-06-13 23:33:05 * @LastEditors: viletyy * @LastEditTime: 2021-06-14 20:45:47 * @FilePath: /potato/internal/middleware/tracer.go */ package middleware import ( "context" "github.com/gin-gonic/gin" "github.com/opentracing/opentracing-go" "github.com/opentracing/opentracing-go/ext" "github.com/uber/jaeger-client-go" "github.com/viletyy/potato/global" ) func Tracing() gin.HandlerFunc { return func(c *gin.Context) { var newCtx context.Context var span opentracing.Span spanCtx, err := opentracing.GlobalTracer().Extract( opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(c.Request.Header), ) if err != nil { span, newCtx = opentracing.StartSpanFromContextWithTracer( c.Request.Context(), global.GO_TRACER, c.Request.URL.Path, ) } else { span, newCtx = opentracing.StartSpanFromContextWithTracer( c.Request.Context(), global.GO_TRACER, c.Request.URL.Path, opentracing.ChildOf(spanCtx), opentracing.Tag{Key: string(ext.Component), Value: "HTTP"}, ) } defer span.Finish() var traceID string var spanID string var spanContext = span.Context() switch spanContext.(type) { case jaeger.SpanContext: jaegerContext := spanContext.(jaeger.SpanContext) traceID = jaegerContext.TraceID().String() spanID = jaegerContext.SpanID().String() } c.Set("X-Trace-ID", traceID) c.Set("X-Span-ID", spanID) c.Request = c.Request.WithContext(newCtx) c.Next() } }