Read stable HTTP semconv attributes when building HTTP span messages

Merged
SA
2mo ago
+123-12

Fixes #2513 (the second half; #2514 handled the stale message after update_name).

What

_tweak_http_spans now understands the stable HTTP semantic conventions as well as the old ones:

oldstable
http.methodhttp.request.method
http.targeturl.path
query parsed from http.urlurl.query
http.urlurl.full

The old keys still take precedence when both are present (as with OTEL_SEMCONV_STABILITY_OPT_IN=http/dup), so spans from the contrib instrumentations are unchanged.

Why

FastAPI's built-in telemetry (0.142+) starts the server span as GET with only the stable attributes, then renames it to GET /items/{item_id} once routing has matched. After #2514 the message followed the rename, but _tweak_http_spans only read the old keys, so it found just http.route and left the message as GET /items/{item_id} instead of building GET /items/1.

Details

  • The query string now comes from url.query when present, otherwise it's parsed from the URL as before, and is rendered the same way (/path ? a='1' & b='2 3').
  • http.target is still derived from http.url for spans on the old conventions, but is not added to spans that only use url.full, to avoid mixing deprecated attributes into stable-semconv spans.

Before / After

Real FastAPI 0.142.2 with its built-in telemetry, GET /items/1?x=1&y=2:

main:  name='GET /items/{item_id}'  logfire.msg='GET /items/{item_id}'
PR:    name='GET /items/{item_id}'  logfire.msg="GET /items/1 ? x='1' & y='2'"

Tests

Added test_http_spans_stable_semconv in tests/test_logfire.py covering a renamed server span with a query string, a server span without one, and client spans with and without server.address. The lockfile pins FastAPI 0.141.1 which has no built-in telemetry, so the test drives the tracer directly the same way #2514's test does; the FastAPI 0.142.2 output above was checked manually.

🤖 Generated with Claude Code

https://claude.ai/code/session_013cgYhnUKiuH97ZvDynSqgf