Skip to content

Commit

Permalink
Perform sampling as explained in the specification.
Browse files Browse the repository at this point in the history
* Sampling should always defer to the existing sampler. The logic before
  this patch reflected something similar to what is described in the
  ParentBased sampler.

Relates #800

Signed-off-by: Harold Dost <h.dost@criteo.com>
  • Loading branch information
hdost committed Jul 14, 2022
1 parent b3e6e6a commit d3a777a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 47 deletions.
11 changes: 0 additions & 11 deletions opentelemetry-api/src/trace/tracer.rs
Expand Up @@ -271,9 +271,6 @@ pub struct SpanBuilder {

/// Span status
pub status: Status,

/// Sampling result
pub sampling_result: Option<SamplingResult>,
}

/// SpanBuilder methods
Expand Down Expand Up @@ -373,14 +370,6 @@ impl SpanBuilder {
SpanBuilder { status, ..self }
}

/// Assign sampling result
pub fn with_sampling_result(self, sampling_result: SamplingResult) -> Self {
SpanBuilder {
sampling_result: Some(sampling_result),
..self
}
}

/// Builds a span with the given tracer from this configuration.
pub fn start<T: Tracer>(self, tracer: &T) -> T::Span {
tracer.build_with_context(self, &Context::current())
Expand Down
46 changes: 10 additions & 36 deletions opentelemetry-sdk/src/trace/tracer.rs
Expand Up @@ -152,10 +152,7 @@ impl opentelemetry_api::trace::Tracer for Tracer {
let span_kind = builder.span_kind.take().unwrap_or(SpanKind::Internal);
let mut attribute_options = builder.attributes.take().unwrap_or_default();
let mut link_options = builder.links.take();
let mut no_parent = true;
let mut remote_parent = false;
let mut parent_span_id = SpanId::INVALID;
let mut parent_trace_flags = TraceFlags::default();
let trace_id;

let parent_span = if parent_cx.has_active_span() {
Expand All @@ -166,47 +163,24 @@ impl opentelemetry_api::trace::Tracer for Tracer {

// Build context for sampling decision
if let Some(sc) = parent_span.as_ref().map(|parent| parent.span_context()) {
no_parent = false;
remote_parent = sc.is_remote();
parent_span_id = sc.span_id();
parent_trace_flags = sc.trace_flags();
trace_id = sc.trace_id();
} else {
trace_id = builder
.trace_id
.unwrap_or_else(|| config.id_generator.new_trace_id());
};

// There are 3 paths for sampling.
//
// * Sampling has occurred elsewhere and is already stored in the builder
// * 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)
} else if no_parent || remote_parent {
self.make_sampling_decision(
parent_cx,
trace_id,
&builder.name,
&span_kind,
&attribute_options,
link_options.as_deref().unwrap_or(&[]),
provider.config(),
&self.instrumentation_lib,
)
} else {
// has parent that is local: use parent if sampled, or don't record.
parent_span
.filter(|span| span.span_context().is_sampled())
.map(|span| {
(
parent_trace_flags,
Vec::new(),
span.span_context().trace_state().clone(),
)
})
};
let sampling_decision = self.make_sampling_decision(
parent_cx,
trace_id,
&builder.name,
&span_kind,
&attribute_options,
link_options.as_deref().unwrap_or(&[]),
provider.config(),
&self.instrumentation_lib,
);

let SpanBuilder {
name,
Expand Down

0 comments on commit d3a777a

Please sign in to comment.