Skip to content

Comments

otel: set http.route span attribute using otel SDK directly#243

Merged
raphael merged 1 commit intov3from
fix/otel-plugin-direct-route-attr
Feb 16, 2026
Merged

otel: set http.route span attribute using otel SDK directly#243
raphael merged 1 commit intov3from
fix/otel-plugin-direct-route-attr

Conversation

@raphael
Copy link
Member

@raphael raphael commented Feb 16, 2026

Summary

Replaces the removed otelhttp.WithRouteTag (dropped in otelhttp v0.65.0) with a direct call to the OTel SDK to set http.route on the span:

trace.SpanFromContext(r.Context()).SetAttributes(semconv.HTTPRoute("/path"))

This restores the http.route span attribute that was lost after upgrading otelhttp.

When is this plugin needed?

The plugin is needed when otelhttp wraps the mux externally:

handler := otelhttp.NewHandler(mux, "service")

In this pattern, otelhttp.RequestTraceAttrs runs before the mux dispatches, so r.Pattern is not yet available and http.route won't be set automatically.

Alternative (no plugin)

When using otelhttp as a mux middleware (recommended), the Goa muxer's ServeHTTP sets r.Pattern before the middleware chain runs, so otelhttp picks up the route automatically:

mux := goahttp.NewMuxer()
mux.Use(otelhttp.NewMiddleware("service"))

Test plan

  • Golden files updated — handlers now set semconv.HTTPRoute via trace.SpanFromContext
  • go test ./otel/ passes

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.
@raphael raphael merged commit a63ea42 into v3 Feb 16, 2026
5 checks passed
@raphael raphael deleted the fix/otel-plugin-direct-route-attr branch February 16, 2026 23:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant