infra(tracing): Fix span naming order-of-operations bug (#90025)

This commit is contained in:
Dave Henderson 2024-07-04 07:05:14 -04:00 committed by GitHub
parent 8f99d58aaf
commit 7ac757afcb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -87,10 +87,6 @@ func RequestTracing(tracer tracing.Tracer) web.Middleware {
// generic span name for requests where there's no route operation name
spanName := fmt.Sprintf("HTTP %s <unknown>", req.Method)
// TODO: do not depend on web.Context from the future
if routeOperation, exists := RouteOperationName(web.FromContext(req.Context()).Req); exists {
spanName = fmt.Sprintf("HTTP %s %s", req.Method, routeOperation)
}
ctx, span := tracer.Start(ctx, spanName, trace.WithAttributes(
semconv.HTTPURLKey.String(req.RequestURI),
@ -105,6 +101,13 @@ func RequestTracing(tracer tracing.Tracer) web.Middleware {
next.ServeHTTP(rw, req)
// Reset the span name after the request has been processed, as
// the route operation may have been injected by middleware.
// TODO: do not depend on web.Context from the future
if routeOperation, exists := RouteOperationName(web.FromContext(req.Context()).Req); exists {
span.SetName(fmt.Sprintf("HTTP %s %s", req.Method, routeOperation))
}
status := rw.Status()
span.SetAttributes(semconv.HTTPStatusCode(status))