fix: populate trace_id at span creation from incoming traceparent header#145
Open
tyrypyrking wants to merge 1 commit intoLukeMathWalker:mainfrom
Open
fix: populate trace_id at span creation from incoming traceparent header#145tyrypyrking wants to merge 1 commit intoLukeMathWalker:mainfrom
tyrypyrking wants to merge 1 commit intoLukeMathWalker:mainfrom
Conversation
8088f76 to
807d0c6
Compare
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
807d0c6 to
8c87980
Compare
Author
|
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #109.
When a request comes in with a
traceparentheader, thetrace_idwasn't showing up in the "REQUEST START" log line. It only appeared at "REQUEST END". The reason is thattrace_idwas set viaspan.record()after the span was already created, but subscribers liketracing-bunyan-formatteremit START duringon_new_span— by then the field is stillEmpty.The fix is straightforward: extract the
trace_idfrom the propagated context before creating the span, so it's baked into the span fields from the start. When there's no remote parent (notraceparentheader), the field staysEmptyand gets filled later byset_otel_parentexactly as before — no behavior change there.What changed
src/otel.rs— newextract_trace_id()that pulls trace_id from request headers via the global propagatorsrc/root_span_macro.rs—root_span!macro now callsextract_otel_trace_id()before span creation; twoinner_spanarms handle the "known trace_id" vs "empty" cases (unfortunately unavoidable sincetracing::Valueis sealed)