otel: set http.route span attribute using otel SDK directly#243
Merged
Conversation
Replace the removed otelhttp.WithRouteTag with a direct call to trace.SpanFromContext(r.Context()).SetAttributes(semconv.HTTPRoute(...)) in each generated handler. This restores the http.route span attribute that was lost when WithRouteTag was removed in otelhttp v0.65.0. The plugin is needed when otelhttp wraps the mux externally via otelhttp.NewHandler(mux, ...), since r.Pattern is not available before the mux dispatches. When using mux.Use(otelhttp.NewMiddleware(...)) instead, the Goa muxer sets r.Pattern early and the plugin is not necessary.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replaces the removed
otelhttp.WithRouteTag(dropped in otelhttp v0.65.0) with a direct call to the OTel SDK to sethttp.routeon the span:This restores the
http.routespan attribute that was lost after upgrading otelhttp.When is this plugin needed?
The plugin is needed when
otelhttpwraps the mux externally:In this pattern,
otelhttp.RequestTraceAttrsruns before the mux dispatches, sor.Patternis not yet available andhttp.routewon't be set automatically.Alternative (no plugin)
When using
otelhttpas a mux middleware (recommended), the Goa muxer'sServeHTTPsetsr.Patternbefore the middleware chain runs, sootelhttppicks up the route automatically:Test plan
semconv.HTTPRouteviatrace.SpanFromContextgo test ./otel/passes