From 8de66048056ee7d7bba0227a7b1956a292c4fba9 Mon Sep 17 00:00:00 2001 From: Alex Emirov Date: Sun, 29 Oct 2023 11:54:03 -0400 Subject: [PATCH] Add check for sampled context --- opentracing-shim/src/tracer_shim.cc | 21 ++++++++++++++------- opentracing-shim/test/tracer_shim_test.cc | 1 + 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/opentracing-shim/src/tracer_shim.cc b/opentracing-shim/src/tracer_shim.cc index c8805d15ef..41c6404624 100644 --- a/opentracing-shim/src/tracer_shim.cc +++ b/opentracing-shim/src/tracer_shim.cc @@ -148,13 +148,20 @@ opentracing::expected> TracerShim::ext auto span_context = opentelemetry::trace::GetSpan(context)->GetContext(); auto baggage = opentelemetry::baggage::GetBaggage(context); - // If the extracted SpanContext is invalid AND the extracted Baggage is empty, - // this operation MUST return a null value, and otherwise it MUST return a - // SpanContext Shim instance with the extracted values. - SpanContextShim *context_shim = (!span_context.IsValid() && utils::isBaggageEmpty(baggage)) - ? nullptr - : new (std::nothrow) SpanContextShim(span_context, baggage); - + // The operation MUST return a `SpanContext` Shim instance with the extracted values if any of + // these conditions are met: + // * `SpanContext` is valid. + // * `SpanContext` is sampled. + // * `SpanContext` contains non-empty extracted `Baggage`. + // Otherwise, the operation MUST return null or empty value. + SpanContextShim *context_shim = + (!span_context.IsValid() && !span_context.IsSampled() && utils::isBaggageEmpty(baggage)) + ? nullptr + : new (std::nothrow) SpanContextShim(span_context, baggage); + + // Note: Invalid but sampled `SpanContext` instances are returned as a way to support + // jaeger-debug-id headers (https://github.com/jaegertracing/jaeger-client-java#via-http-headers) + // which are used to force propagation of debug information. return opentracing::make_expected(std::unique_ptr(context_shim)); } diff --git a/opentracing-shim/test/tracer_shim_test.cc b/opentracing-shim/test/tracer_shim_test.cc index 7a06175323..9d57bbcae4 100644 --- a/opentracing-shim/test/tracer_shim_test.cc +++ b/opentracing-shim/test/tracer_shim_test.cc @@ -211,6 +211,7 @@ TEST_F(TracerShimTest, ExtractOnlyBaggage) auto span_context_shim = static_cast(span_context.value().get()); ASSERT_TRUE(span_context_shim != nullptr); ASSERT_FALSE(span_context_shim->context().IsValid()); + ASSERT_FALSE(span_context_shim->context().IsSampled()); ASSERT_FALSE(shim::utils::isBaggageEmpty(span_context_shim->baggage())); std::string value;