Skip to content

Commit

Permalink
fix: trace id did not match when propagating invalid context (#55)
Browse files Browse the repository at this point in the history
## 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
  • Loading branch information
ymgyt committed Aug 23, 2023
1 parent 11d57b8 commit 4fa7527
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/span_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ impl OpenTelemetrySpanExt for tracing::Span {
get_context.with_context(subscriber, id, move |data, _tracer| {
if let Some(cx) = cx.take() {
data.parent_cx = cx;
data.builder.trace_id = None;
}
});
}
Expand Down
18 changes: 18 additions & 0 deletions tests/trace_state_propagation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,24 @@ fn trace_root_with_children() {
assert_shared_attrs_eq(&spans[0].span_context, &spans[1].span_context);
}

#[test]
fn propagate_invalid_context() {
let (_tracer, provider, exporter, subscriber) = test_tracer();
let propagator = TraceContextPropagator::new();
let invalid_cx = propagator.extract(&HashMap::new()); // empty context extracted

tracing::subscriber::with_default(subscriber, || {
let root = tracing::debug_span!("root");
root.set_parent(invalid_cx);
root.in_scope(|| tracing::debug_span!("child"));
});

drop(provider); // flush all spans
let spans = exporter.0.lock().unwrap();
assert_eq!(spans.len(), 2);
assert_shared_attrs_eq(&spans[0].span_context, &spans[1].span_context);
}

#[test]
fn inject_context_into_outgoing_requests() {
let (_tracer, _provider, _exporter, subscriber) = test_tracer();
Expand Down

0 comments on commit 4fa7527

Please sign in to comment.