diff --git a/middleware/tracing.go b/middleware/tracing.go index 9382e8a14e..663c52181f 100644 --- a/middleware/tracing.go +++ b/middleware/tracing.go @@ -14,10 +14,15 @@ func Trace(next http.Handler) http.Handler { var span *trace.Span tc := tracecontext.HTTPFormat{} - sc, ok := tc.SpanContextFromRequest(r) - if ok { + // reconstruct span context from request + if sc, ok := tc.SpanContextFromRequest(r); ok { + // if there is one, add it to the new span ctx, span = trace.StartSpanWithRemoteParent(r.Context(), r.URL.String(), sc) defer span.End() + } else { + // create a new span if there is no context + ctx, span = trace.StartSpan(r.Context(), r.URL.String()) + defer span.End() } next.ServeHTTP(w, r.WithContext(ctx))