Skip to content

Commit

Permalink
Merge branch 'master' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
jtescher committed Dec 28, 2020
2 parents 537c873 + cf2b471 commit d207da5
Showing 1 changed file with 49 additions and 30 deletions.
79 changes: 49 additions & 30 deletions opentelemetry/src/sdk/trace/tracer.rs
Expand Up @@ -67,7 +67,7 @@ impl Tracer {
#[allow(clippy::too_many_arguments)]
fn make_sampling_decision(
&self,
parent_cx: Option<&Context>,
parent_cx: &Context,
trace_id: TraceId,
name: &str,
span_kind: &SpanKind,
Expand All @@ -77,16 +77,22 @@ impl Tracer {
let provider = self.provider()?;
let sampler = &provider.config().default_sampler;

let sampling_result =
sampler.should_sample(parent_cx, trace_id, name, span_kind, attributes, links);
let sampling_result = sampler.should_sample(
Some(parent_cx),
trace_id,
name,
span_kind,
attributes,
links,
);

self.process_sampling_result(sampling_result, parent_cx)
}

fn process_sampling_result(
&self,
sampling_result: SamplingResult,
parent_cx: Option<&Context>,
parent_cx: &Context,
) -> Option<(u8, Vec<KeyValue>, TraceState)> {
match sampling_result {
SamplingResult {
Expand All @@ -98,19 +104,15 @@ impl Tracer {
attributes,
trace_state,
} => {
let trace_flags = parent_cx
.map(|ctx| ctx.span().span_context().trace_flags())
.unwrap_or(0);
let trace_flags = parent_cx.span().span_context().trace_flags();
Some((trace_flags & !TRACE_FLAG_SAMPLED, attributes, trace_state))
}
SamplingResult {
decision: SamplingDecision::RecordAndSample,
attributes,
trace_state,
} => {
let trace_flags = parent_cx
.map(|ctx| ctx.span().span_context().trace_flags())
.unwrap_or(0);
let trace_flags = parent_cx.span().span_context().trace_flags();
Some((trace_flags | TRACE_FLAG_SAMPLED, attributes, trace_state))
}
}
Expand Down Expand Up @@ -174,24 +176,26 @@ impl crate::trace::Tracer for Tracer {
let mut flags = 0;
let mut span_trace_state = Default::default();

let parent_cx = builder.parent_context.take().map(|cx| {
// Sampling expects to be able to access the parent span via `span` so wrap remote span
// context in a wrapper span if necessary. Remote span contexts will be passed to
// subsequent context's, so wrapping is only necessary if there is no active span.
match cx.remote_span_context() {
Some(remote_sc) if !cx.has_active_span() => {
cx.with_span(Span::new(remote_sc.clone(), None, self.clone()))
let parent_cx = builder
.parent_context
.take()
.map(|cx| {
// Sampling expects to be able to access the parent span via `span` so wrap remote span
// context in a wrapper span if necessary. Remote span contexts will be passed to
// subsequent context's, so wrapping is only necessary if there is no active span.
match cx.remote_span_context() {
Some(remote_sc) if !cx.has_active_span() => {
cx.with_span(Span::new(remote_sc.clone(), None, self.clone()))
}
_ => cx,
}
_ => cx,
}
});
let parent_span_context = parent_cx.as_ref().and_then(|parent_cx| {
if parent_cx.has_active_span() {
Some(parent_cx.span().span_context())
} else {
None
}
});
})
.unwrap_or_else(Context::current);
let parent_span_context = if parent_cx.has_active_span() {
Some(parent_cx.span().span_context())
} else {
None
};
// Build context for sampling decision
let (no_parent, trace_id, parent_span_id, remote_parent, parent_trace_flags) =
parent_span_context
Expand Down Expand Up @@ -221,10 +225,10 @@ impl crate::trace::Tracer for Tracer {
// * There is no parent or a remote parent, in which case make decision now
// * There is a local parent, in which case defer to the parent's decision
let sampling_decision = if let Some(sampling_result) = builder.sampling_result.take() {
self.process_sampling_result(sampling_result, parent_cx.as_ref())
self.process_sampling_result(sampling_result, &parent_cx)
} else if no_parent || remote_parent {
self.make_sampling_decision(
parent_cx.as_ref(),
&parent_cx,
trace_id,
&builder.name,
&span_kind,
Expand Down Expand Up @@ -288,7 +292,7 @@ impl crate::trace::Tracer for Tracer {

// Call `on_start` for all processors
for processor in provider.span_processors() {
processor.on_start(&span, parent_cx.as_ref().unwrap_or(&Context::new()))
processor.on_start(&span, &parent_cx)
}

span
Expand Down Expand Up @@ -377,4 +381,19 @@ mod tests {

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

#[test]
fn uses_current_context_for_builders_if_unset() {
let sampler = Sampler::ParentBased(Box::new(Sampler::AlwaysOn));
let config = Config::default().with_default_sampler(sampler);
let tracer_provider = sdk::trace::TracerProvider::builder()
.with_config(config)
.build();

let _attached = Context::current_with_span(TestSpan(SpanContext::empty_context())).attach();
let tracer = tracer_provider.get_tracer("test", None);
let span = tracer.span_builder("must_not_be_sampled").start(&tracer);

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

0 comments on commit d207da5

Please sign in to comment.