Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[opentracing-shim] Add check for sampled context #2390

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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