Keep span message in sync with the name after update_name

Merged
DA
2mo ago
+47-2

Part of #2513 (the first half: the stale message after update_name; the stable-semconv half is left for a follow-up)

What

span.update_name() on a logfire-wrapped span now keeps logfire.msg in sync with the new name.

Why

_ProxyTracer.start_span seeds logfire.msg from the name the span is started with. _LogfireWrappedSpan.update_name only forwarded the rename to the SDK span, so a span started as GET and later renamed to GET /items/{item_id} kept logfire.msg = 'GET'. Because name and message no longer matched, _tweak_http_spans also returned early and never built the HTTP message.

How

In update_name, if the span has no logfire.msg_template and its message still equals the old name (i.e. it was never set explicitly), set the message to the new name. An explicit logfire.msg or a templated message is left alone. Once name and message match again, _tweak_http_spans runs as usual.

I did not touch _tweak_http_spans to understand the stable semconv attributes (http.request.method, url.path, ...); that looks like a separate change.

Before / After

Tracer-level reproducer (start a span as GET with the old semconv HTTP attributes http.method, http.target, http.route, call update_name('GET /items/{item_id}'), end it):

Before (main):

name   = GET /items/{item_id}
logfire.msg = GET

After:

name   = GET /items/{item_id}
logfire.msg = GET /items/1

With FastAPI's built-in telemetry, which sets the stable keys (http.request.method, url.path), the message now becomes GET /items/{item_id} (was GET). Getting GET /items/1 there needs _tweak_http_spans to read the stable keys, which is the follow-up above.

Tests

Added test_update_name_updates_message in tests/test_logfire.py (renamed HTTP server span, plain rename, explicit message kept, templated message kept). It fails on main with ('GET /items/{item_id}', 'GET') != ('GET /items/{item_id}', 'GET /items/1') and passes with the fix. ruff and pyright pass, and tracer.py coverage was 100% in a local run.