37
37
//! cause gaps in the distributed trace, and because of this OpenTelemetry API
38
38
//! MUST NOT allow this combination.
39
39
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
+ } ;
42
44
43
45
/// The `ShouldSample` interface allows implementations to provide samplers
44
46
/// which will return a sampling `SamplingResult` based on information that
@@ -48,7 +50,7 @@ pub trait ShouldSample: Send + Sync + std::fmt::Debug {
48
50
#[ allow( clippy:: too_many_arguments) ]
49
51
fn should_sample (
50
52
& self ,
51
- parent_context : Option < & SpanContext > ,
53
+ parent_context : Option < & Context > ,
52
54
trace_id : TraceId ,
53
55
name : & str ,
54
56
span_kind : & SpanKind ,
@@ -98,7 +100,7 @@ pub enum Sampler {
98
100
impl ShouldSample for Sampler {
99
101
fn should_sample (
100
102
& self ,
101
- parent_context : Option < & SpanContext > ,
103
+ parent_context : Option < & Context > ,
102
104
trace_id : TraceId ,
103
105
name : & str ,
104
106
span_kind : & SpanKind ,
@@ -116,7 +118,8 @@ impl ShouldSample for Sampler {
116
118
. should_sample ( parent_context, trace_id, name, span_kind, attributes, links)
117
119
. decision ,
118
120
|ctx| {
119
- if ctx. is_sampled ( ) {
121
+ let parent_span_context = ctx. span ( ) . span_context ( ) ;
122
+ if parent_span_context. is_sampled ( ) {
120
123
SamplingDecision :: RecordAndSample
121
124
} else {
122
125
SamplingDecision :: Drop
@@ -147,7 +150,7 @@ impl ShouldSample for Sampler {
147
150
attributes : Vec :: new ( ) ,
148
151
// all sampler in SDK will not modify trace state.
149
152
trace_state : match parent_context {
150
- Some ( ctx) => ctx. trace_state ( ) . clone ( ) ,
153
+ Some ( ctx) => ctx. span ( ) . span_context ( ) . trace_state ( ) . clone ( ) ,
151
154
None => TraceState :: default ( ) ,
152
155
} ,
153
156
}
@@ -158,7 +161,8 @@ impl ShouldSample for Sampler {
158
161
mod tests {
159
162
use super :: * ;
160
163
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 } ;
162
166
use rand:: Rng ;
163
167
164
168
#[ rustfmt:: skip]
@@ -216,16 +220,19 @@ mod tests {
216
220
for _ in 0 ..total {
217
221
let parent_context = if parent {
218
222
let trace_flags = if sample_parent { TRACE_FLAG_SAMPLED } else { 0 } ;
219
- Some ( SpanContext :: new (
223
+ let span_context = SpanContext :: new (
220
224
TraceId :: from_u128 ( 1 ) ,
221
225
SpanId :: from_u64 ( 1 ) ,
222
226
trace_flags,
223
227
false ,
224
228
TraceState :: default ( ) ,
225
- ) )
229
+ ) ;
230
+
231
+ Some ( Context :: current_with_span ( TestSpan ( span_context) ) )
226
232
} else {
227
233
None
228
234
} ;
235
+
229
236
let trace_id = TraceId :: from_u128 ( rng. gen ( ) ) ;
230
237
if sampler
231
238
. should_sample (
0 commit comments