Skip to content

Commit

Permalink
Add check for sampled context
Browse files Browse the repository at this point in the history
  • Loading branch information
chusitoo committed Oct 29, 2023
1 parent 17da6d8 commit 8de6604
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
21 changes: 14 additions & 7 deletions opentracing-shim/src/tracer_shim.cc
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,20 @@ opentracing::expected<std::unique_ptr<opentracing::SpanContext>> 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<opentracing::SpanContext>(context_shim));
}

Expand Down
1 change: 1 addition & 0 deletions opentracing-shim/test/tracer_shim_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ TEST_F(TracerShimTest, ExtractOnlyBaggage)
auto span_context_shim = static_cast<shim::SpanContextShim *>(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;
Expand Down

0 comments on commit 8de6604

Please sign in to comment.