only write attributes after other middleware (and request)

This commit is contained in:
Felix Hildén 2025-09-21 15:22:40 +03:00
parent 168f0e02b8
commit d28346cbdd
2 changed files with 7 additions and 6 deletions

View file

@ -366,9 +366,10 @@ func TestTracing_OpenTelemetry_Span_Attributes(t *testing.T) {
// Verify that the span attributes were set correctly with placeholder replacement // Verify that the span attributes were set correctly with placeholder replacement
expectedAttributes := map[string]string{ expectedAttributes := map[string]string{
"placeholder": "POST", "placeholder": "POST",
"static": "test-service", "static": "test-service",
"mixed": "prefix-POST-suffix", "mixed": "prefix-POST-suffix",
"http.response.status_code": "200", // OTEL default
} }
actualAttributes := make(map[string]string) actualAttributes := make(map[string]string)

View file

@ -104,6 +104,9 @@ func (ot *openTelemetryWrapper) serveHTTP(w http.ResponseWriter, r *http.Request
} }
} }
next := ctx.Value(nextCallCtxKey).(*nextCall)
next.err = next.next.ServeHTTP(w, r)
// Add custom span attributes to the current span // Add custom span attributes to the current span
span := trace.SpanFromContext(ctx) span := trace.SpanFromContext(ctx)
if span.IsRecording() && len(ot.spanAttributes) > 0 { if span.IsRecording() && len(ot.spanAttributes) > 0 {
@ -116,9 +119,6 @@ func (ot *openTelemetryWrapper) serveHTTP(w http.ResponseWriter, r *http.Request
} }
span.SetAttributes(attributes...) span.SetAttributes(attributes...)
} }
next := ctx.Value(nextCallCtxKey).(*nextCall)
next.err = next.next.ServeHTTP(w, r)
} }
// ServeHTTP propagates call to the by wrapped by `otelhttp` next handler. // ServeHTTP propagates call to the by wrapped by `otelhttp` next handler.