From 855a3e006e51f13e35709a5ec30b26d166373465 Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Mon, 9 Mar 2020 15:35:02 +0100 Subject: [PATCH] create span regardless of a context or not --- middleware/tracing.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) 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))