@@ -176,21 +176,22 @@ impl crate::trace::Tracer for Tracer {
176
176
let mut flags = 0 ;
177
177
let mut span_trace_state = Default :: default ( ) ;
178
178
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 ( ) ) )
191
191
}
192
- } )
193
- . unwrap_or_else ( Context :: current) ;
192
+ _ => cx,
193
+ }
194
+ } ;
194
195
let parent_span_context = if parent_cx. has_active_span ( ) {
195
196
Some ( parent_cx. span ( ) . span_context ( ) )
196
197
} else {
@@ -309,7 +310,7 @@ mod tests {
309
310
testing:: trace:: TestSpan ,
310
311
trace:: {
311
312
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 ,
313
314
} ,
314
315
Context , KeyValue ,
315
316
} ;
@@ -389,9 +390,21 @@ mod tests {
389
390
let tracer_provider = sdk:: trace:: TracerProvider :: builder ( )
390
391
. with_config ( config)
391
392
. build ( ) ;
393
+ let tracer = tracer_provider. get_tracer ( "test" , None ) ;
392
394
393
395
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 ( ) ;
395
408
let span = tracer. span_builder ( "must_not_be_sampled" ) . start ( & tracer) ;
396
409
397
410
assert ! ( !span. span_context( ) . is_sampled( ) ) ;
0 commit comments