Skip to content

Commit dc924b3

Browse files
authored
Fix remote implicit builder context sampling (#405)
1 parent ddcf8a4 commit dc924b3

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

opentelemetry/src/sdk/trace/tracer.rs

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -176,21 +176,22 @@ impl crate::trace::Tracer for Tracer {
176176
let mut flags = 0;
177177
let mut span_trace_state = Default::default();
178178

179-
let parent_cx = builder
180-
.parent_context
181-
.take()
182-
.map(|cx| {
183-
// Sampling expects to be able to access the parent span via `span` so wrap remote span
184-
// context in a wrapper span if necessary. Remote span contexts will be passed to
185-
// subsequent context's, so wrapping is only necessary if there is no active span.
186-
match cx.remote_span_context() {
187-
Some(remote_sc) if !cx.has_active_span() => {
188-
cx.with_span(Span::new(remote_sc.clone(), None, self.clone()))
189-
}
190-
_ => cx,
179+
let parent_cx = {
180+
let cx = builder
181+
.parent_context
182+
.take()
183+
.unwrap_or_else(Context::current);
184+
185+
// Sampling expects to be able to access the parent span via `span` so wrap remote span
186+
// context in a wrapper span if necessary. Remote span contexts will be passed to
187+
// subsequent context's, so wrapping is only necessary if there is no active span.
188+
match cx.remote_span_context() {
189+
Some(remote_sc) if !cx.has_active_span() => {
190+
cx.with_span(Span::new(remote_sc.clone(), None, self.clone()))
191191
}
192-
})
193-
.unwrap_or_else(Context::current);
192+
_ => cx,
193+
}
194+
};
194195
let parent_span_context = if parent_cx.has_active_span() {
195196
Some(parent_cx.span().span_context())
196197
} else {
@@ -309,7 +310,7 @@ mod tests {
309310
testing::trace::TestSpan,
310311
trace::{
311312
Link, Span, SpanBuilder, SpanContext, SpanId, SpanKind, TraceContextExt, TraceId,
312-
TraceState, Tracer, TracerProvider, TRACE_FLAG_SAMPLED,
313+
TraceState, Tracer, TracerProvider, TRACE_FLAG_NOT_SAMPLED, TRACE_FLAG_SAMPLED,
313314
},
314315
Context, KeyValue,
315316
};
@@ -389,9 +390,21 @@ mod tests {
389390
let tracer_provider = sdk::trace::TracerProvider::builder()
390391
.with_config(config)
391392
.build();
393+
let tracer = tracer_provider.get_tracer("test", None);
392394

393395
let _attached = Context::current_with_span(TestSpan(SpanContext::empty_context())).attach();
394-
let tracer = tracer_provider.get_tracer("test", None);
396+
let span = tracer.span_builder("must_not_be_sampled").start(&tracer);
397+
assert!(!span.span_context().is_sampled());
398+
399+
let _attached = Context::current()
400+
.with_remote_span_context(SpanContext::new(
401+
TraceId::from_u128(1),
402+
SpanId::from_u64(1),
403+
TRACE_FLAG_NOT_SAMPLED,
404+
true,
405+
Default::default(),
406+
))
407+
.attach();
395408
let span = tracer.span_builder("must_not_be_sampled").start(&tracer);
396409

397410
assert!(!span.span_context().is_sampled());

0 commit comments

Comments
 (0)