Skip to content

fix: populate trace_id at span creation from incoming traceparent header#145

Open
tyrypyrking wants to merge 1 commit intoLukeMathWalker:mainfrom
tyrypyrking:fix/trace-id-at-span-creation
Open

fix: populate trace_id at span creation from incoming traceparent header#145
tyrypyrking wants to merge 1 commit intoLukeMathWalker:mainfrom
tyrypyrking:fix/trace-id-at-span-creation

Conversation

@tyrypyrking
Copy link
Copy Markdown

@tyrypyrking tyrypyrking commented Apr 16, 2026

Fixes #109.

When a request comes in with a traceparent header, the trace_id wasn't showing up in the "REQUEST START" log line. It only appeared at "REQUEST END". The reason is that trace_id was set via span.record() after the span was already created, but subscribers like tracing-bunyan-formatter emit START during on_new_span — by then the field is still Empty.

The fix is straightforward: extract the trace_id from the propagated context before creating the span, so it's baked into the span fields from the start. When there's no remote parent (no traceparent header), the field stays Empty and gets filled later by set_otel_parent exactly as before — no behavior change there.

What changed

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

@tyrypyrking tyrypyrking force-pushed the fix/trace-id-at-span-creation branch from 8088f76 to 807d0c6 Compare April 16, 2026 13:42
trace_id from an incoming traceparent header was missing from the
"REQUEST START" log entry because it was only set via span.record()
after the span was created. Subscribers like tracing-bunyan-formatter
emit START during on_new_span, at which point trace_id was still Empty.

Extract the trace_id from the propagated context before creating the
span so it is available at span creation time. When no remote parent
is found, the field remains Empty and is filled later by set_otel_parent
as before.

Closes LukeMathWalker#109
@tyrypyrking tyrypyrking force-pushed the fix/trace-id-at-span-creation branch from 807d0c6 to 8c87980 Compare April 16, 2026 13:48
@tyrypyrking
Copy link
Copy Markdown
Author

tyrypyrking commented Apr 16, 2026

Not a great solution to my taste, quite a fair bit of boilerplate, but with this issue is seems like nothing else can be done. We just have to handle both cases to get trace_id before span creation, which results in this monstrosity.

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.

Missing trace_id value in REQUEST - START

1 participant