From 4fa752748116f37af82b59e9b74cb042afa035c1 Mon Sep 17 00:00:00 2001 From: Yuta Yamaguchi Date: Thu, 24 Aug 2023 08:47:05 +0900 Subject: [PATCH] fix: trace id did not match when propagating invalid context (#55) ## Motivation Fix following issues * https://github.com/tokio-rs/tracing-opentelemetry/issues/54 * https://github.com/tokio-rs/tracing-opentelemetry/issues/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 --- src/span_ext.rs | 1 - tests/trace_state_propagation.rs | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/span_ext.rs b/src/span_ext.rs index a683521..94d96a9 100644 --- a/src/span_ext.rs +++ b/src/span_ext.rs @@ -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; } }); } diff --git a/tests/trace_state_propagation.rs b/tests/trace_state_propagation.rs index 2ad209b..a020a74 100644 --- a/tests/trace_state_propagation.rs +++ b/tests/trace_state_propagation.rs @@ -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();