Skip to content

Commit 4fa7527

Browse files
authored
fix: trace id did not match when propagating invalid context (#55)
## Motivation Fix following issues * #54 * #45 ## Solution * Revert https://github.com/tokio-rs/tracing-opentelemetry/pull/26/files * When an invalid(empty) Context is extracted from the propagator and passed to OpenTelemetrySpanExt::set_parent(), if none is set for trace_id, root and child trace id will not match * I can confirm that this change works in [reproduction](https://github.com/notheotherben/tracing-opentelemetry-traceid-repro) that @notheotherben created * add regression test case which propagate empty context
1 parent 11d57b8 commit 4fa7527

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/span_ext.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ impl OpenTelemetrySpanExt for tracing::Span {
143143
get_context.with_context(subscriber, id, move |data, _tracer| {
144144
if let Some(cx) = cx.take() {
145145
data.parent_cx = cx;
146-
data.builder.trace_id = None;
147146
}
148147
});
149148
}

tests/trace_state_propagation.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,24 @@ fn trace_root_with_children() {
6363
assert_shared_attrs_eq(&spans[0].span_context, &spans[1].span_context);
6464
}
6565

66+
#[test]
67+
fn propagate_invalid_context() {
68+
let (_tracer, provider, exporter, subscriber) = test_tracer();
69+
let propagator = TraceContextPropagator::new();
70+
let invalid_cx = propagator.extract(&HashMap::new()); // empty context extracted
71+
72+
tracing::subscriber::with_default(subscriber, || {
73+
let root = tracing::debug_span!("root");
74+
root.set_parent(invalid_cx);
75+
root.in_scope(|| tracing::debug_span!("child"));
76+
});
77+
78+
drop(provider); // flush all spans
79+
let spans = exporter.0.lock().unwrap();
80+
assert_eq!(spans.len(), 2);
81+
assert_shared_attrs_eq(&spans[0].span_context, &spans[1].span_context);
82+
}
83+
6684
#[test]
6785
fn inject_context_into_outgoing_requests() {
6886
let (_tracer, _provider, _exporter, subscriber) = test_tracer();

0 commit comments

Comments
 (0)