Skip to content

Commit a0ade71

Browse files
authored
feat: use parent context instead of SpanContext (open-telemetry#368)
1 parent ea61575 commit a0ade71

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

opentelemetry/src/sdk/trace/sampler.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@
3737
//! cause gaps in the distributed trace, and because of this OpenTelemetry API
3838
//! MUST NOT allow this combination.
3939
40-
use crate::trace::{Link, SpanContext, SpanKind, TraceId, TraceState};
41-
use crate::KeyValue;
40+
use crate::{
41+
trace::{Link, SpanKind, TraceContextExt, TraceId, TraceState},
42+
Context, KeyValue,
43+
};
4244

4345
/// The `ShouldSample` interface allows implementations to provide samplers
4446
/// which will return a sampling `SamplingResult` based on information that
@@ -48,7 +50,7 @@ pub trait ShouldSample: Send + Sync + std::fmt::Debug {
4850
#[allow(clippy::too_many_arguments)]
4951
fn should_sample(
5052
&self,
51-
parent_context: Option<&SpanContext>,
53+
parent_context: Option<&Context>,
5254
trace_id: TraceId,
5355
name: &str,
5456
span_kind: &SpanKind,
@@ -98,7 +100,7 @@ pub enum Sampler {
98100
impl ShouldSample for Sampler {
99101
fn should_sample(
100102
&self,
101-
parent_context: Option<&SpanContext>,
103+
parent_context: Option<&Context>,
102104
trace_id: TraceId,
103105
name: &str,
104106
span_kind: &SpanKind,
@@ -116,7 +118,8 @@ impl ShouldSample for Sampler {
116118
.should_sample(parent_context, trace_id, name, span_kind, attributes, links)
117119
.decision,
118120
|ctx| {
119-
if ctx.is_sampled() {
121+
let parent_span_context = ctx.span().span_context();
122+
if parent_span_context.is_sampled() {
120123
SamplingDecision::RecordAndSample
121124
} else {
122125
SamplingDecision::Drop
@@ -147,7 +150,7 @@ impl ShouldSample for Sampler {
147150
attributes: Vec::new(),
148151
// all sampler in SDK will not modify trace state.
149152
trace_state: match parent_context {
150-
Some(ctx) => ctx.trace_state().clone(),
153+
Some(ctx) => ctx.span().span_context().trace_state().clone(),
151154
None => TraceState::default(),
152155
},
153156
}
@@ -158,7 +161,8 @@ impl ShouldSample for Sampler {
158161
mod tests {
159162
use super::*;
160163
use crate::sdk::trace::{Sampler, SamplingDecision, ShouldSample};
161-
use crate::trace::{SpanId, TraceState, TRACE_FLAG_SAMPLED};
164+
use crate::testing::trace::TestSpan;
165+
use crate::trace::{SpanContext, SpanId, TraceState, TRACE_FLAG_SAMPLED};
162166
use rand::Rng;
163167

164168
#[rustfmt::skip]
@@ -216,16 +220,19 @@ mod tests {
216220
for _ in 0..total {
217221
let parent_context = if parent {
218222
let trace_flags = if sample_parent { TRACE_FLAG_SAMPLED } else { 0 };
219-
Some(SpanContext::new(
223+
let span_context = SpanContext::new(
220224
TraceId::from_u128(1),
221225
SpanId::from_u64(1),
222226
trace_flags,
223227
false,
224228
TraceState::default(),
225-
))
229+
);
230+
231+
Some(Context::current_with_span(TestSpan(span_context)))
226232
} else {
227233
None
228234
};
235+
229236
let trace_id = TraceId::from_u128(rng.gen());
230237
if sampler
231238
.should_sample(

opentelemetry/src/sdk/trace/tracer.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,18 @@ impl Tracer {
7373
span_kind: &SpanKind,
7474
attributes: &[KeyValue],
7575
links: &[Link],
76+
ctx: &Context,
7677
) -> Option<(u8, Vec<KeyValue>, TraceState)> {
7778
let provider = self.provider()?;
7879
let sampler = &provider.config().default_sampler;
80+
81+
let ctx = parent_context.map(|span_context| {
82+
let span = Span::new(span_context.clone(), None, self.clone());
83+
ctx.with_span(span)
84+
});
85+
7986
let sampling_result =
80-
sampler.should_sample(parent_context, trace_id, name, span_kind, attributes, links);
87+
sampler.should_sample(ctx.as_ref(), trace_id, name, span_kind, attributes, links);
8188

8289
self.process_sampling_result(sampling_result, parent_context)
8390
}
@@ -217,6 +224,7 @@ impl crate::trace::Tracer for Tracer {
217224
&span_kind,
218225
&attribute_options,
219226
link_options.as_deref().unwrap_or(&[]),
227+
cx,
220228
)
221229
} else {
222230
// has parent that is local: use parent if sampled, or don't record.
@@ -303,14 +311,19 @@ mod tests {
303311
impl ShouldSample for TestSampler {
304312
fn should_sample(
305313
&self,
306-
parent_context: Option<&SpanContext>,
314+
parent_context: Option<&Context>,
307315
_trace_id: TraceId,
308316
_name: &str,
309317
_span_kind: &SpanKind,
310318
_attributes: &[KeyValue],
311319
_links: &[Link],
312320
) -> SamplingResult {
313-
let trace_state = parent_context.unwrap().trace_state().clone();
321+
let trace_state = parent_context
322+
.unwrap()
323+
.span()
324+
.span_context()
325+
.trace_state()
326+
.clone();
314327
SamplingResult {
315328
decision: SamplingDecision::RecordAndSample,
316329
attributes: Vec::new(),

0 commit comments

Comments
 (0)