Propagate W3C trace context for unsampled traces in event metadata - #401
Conversation
PR Summary by QodoPropagate W3C context for unsampled event traces
AI Description
Diagram
High-Level Assessment
Files changed (7)
|
Code Review by Qodo🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)
Great, no issues found!Qodo reviewed your code and found no material issues that require reviewTip of the day💡 Did you know, you can ask Qodo to dismiss a finding you disagree with, with your reason on record |
9a6b8f9 to
e1e13aa
Compare
e1e13aa to
7d7c689
Compare
Fixes #400
Previously, the Java client only wrote tracing metadata (
$traceId/$spanId) to appended events when the current trace was sampled. With head-based sampling in production, most events carried no trace context at all. Consumers reading them later couldn't link their spans back to the operation that produced the event, and events written this way can't be fixed retroactively.Events now carry the standard W3C Trace Context in their metadata. The client writes
$traceparentin the W3C format00-{traceId}-{spanId}-{flags}whenever a trace is active, sampled or not, so consumers can always link back to the producer and see its real sampling decision. When the trace carries vendor state, such as a Datadog sampling decision, the client also writes$tracestate.On read, the client prefers
$traceparentand honors its actual flags. Events written by older clients only have$traceId/$spanId, and the client falls back to those, so existing data keeps working.For a sampled trace, the client keeps writing the legacy fields so consumers on older client versions keep working:
{ "$traceparent": "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01", "$tracestate": "dd=s:1", "$traceId": "0af7651916cd43dd8448eb211c80319c", "$spanId": "b7ad6b7169203331" }For an unsampled trace, which previously got nothing, the client writes only the new field:
{ "$traceparent": "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-00" }Upgrading
Consumers using the default
ParentBasedsampler now respect the producer's sampling decision, so events from unsampled traces no longer produce sampled consumer spans. This is the correct W3C behavior, but you might see fewer consumer spans if your producers sample aggressively.If you registered an OpenTelemetry SDK with an
alwaysOffsampler, event metadata is now stamped with$traceparent, where previously nothing was written.The client now clears reserved metadata fields it manages:
$tracestateon every append, and$traceId/$spanIdwhen the trace is unsampled. If you were setting those fields yourself, they no longer survive the append.Ref: DEV-1890