Skip to content

fix(tracing-actix-web): populate trace_id at span creation#724

Merged
JohnTitor merged 2 commits intoactix:mainfrom
tyrypyrking:fix/trace-id-at-span-creation
May 1, 2026
Merged

fix(tracing-actix-web): populate trace_id at span creation#724
JohnTitor merged 2 commits intoactix:mainfrom
tyrypyrking:fix/trace-id-at-span-creation

Conversation

@tyrypyrking
Copy link
Copy Markdown
Contributor

@tyrypyrking tyrypyrking commented Apr 30, 2026

PR Type

Bug Fix

PR Checklist

  • Tests for the changes have been added / updated.
  • Documentation comments have been added / updated.
  • A changelog entry has been made for the appropriate packages.
  • Format code with the nightly rustfmt (cargo +nightly fmt).

Overview

When a request arrives with a traceparent header, the trace_id was missing from the "REQUEST START" log entry and only appeared at "REQUEST END". The cause: trace_id was set via span.record() after the span had already been created, but subscribers like tracing-bunyan-formatter emit START during on_new_span — at which point the field is still Empty.

This PR extracts the trace_id from the propagated context before creating the span, so it is baked into the span fields from the start. When there is no remote parent (no traceparent header), the field stays Empty and gets filled later by set_otel_parent exactly as before — no behavior change in that path.

What changed

  • tracing-actix-web/src/otel.rs — new extract_trace_id() that pulls the trace_id from request headers via the global propagator.
  • tracing-actix-web/src/root_span_macro.rsroot_span! now calls extract_otel_trace_id() before span creation; two inner_span arms handle the "known trace_id" vs "empty" cases (unavoidable since tracing::Value is sealed).

No breaking changes.

Original PR was at the old repo: LukeMathWalker/tracing-actix-web#145

Copy link
Copy Markdown
Member

@JohnTitor JohnTitor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! LGTM.

@JohnTitor JohnTitor enabled auto-merge May 1, 2026 09:00
@JohnTitor JohnTitor added this pull request to the merge queue May 1, 2026
Merged via the queue into actix:main with commit a850e5c May 1, 2026
10 checks passed
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.

2 participants