Skip to content

Commit 7ac757a

Browse files
infra(tracing): Fix span naming order-of-operations bug (grafana#90025)
1 parent 8f99d58 commit 7ac757a

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

pkg/middleware/request_tracing.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,6 @@ func RequestTracing(tracer tracing.Tracer) web.Middleware {
8787

8888
// generic span name for requests where there's no route operation name
8989
spanName := fmt.Sprintf("HTTP %s <unknown>", req.Method)
90-
// TODO: do not depend on web.Context from the future
91-
if routeOperation, exists := RouteOperationName(web.FromContext(req.Context()).Req); exists {
92-
spanName = fmt.Sprintf("HTTP %s %s", req.Method, routeOperation)
93-
}
9490

9591
ctx, span := tracer.Start(ctx, spanName, trace.WithAttributes(
9692
semconv.HTTPURLKey.String(req.RequestURI),
@@ -105,6 +101,13 @@ func RequestTracing(tracer tracing.Tracer) web.Middleware {
105101

106102
next.ServeHTTP(rw, req)
107103

104+
// Reset the span name after the request has been processed, as
105+
// the route operation may have been injected by middleware.
106+
// TODO: do not depend on web.Context from the future
107+
if routeOperation, exists := RouteOperationName(web.FromContext(req.Context()).Req); exists {
108+
span.SetName(fmt.Sprintf("HTTP %s %s", req.Method, routeOperation))
109+
}
110+
108111
status := rw.Status()
109112

110113
span.SetAttributes(semconv.HTTPStatusCode(status))

0 commit comments

Comments
 (0)