@@ -58,3 +58,37 @@ fn test_passive_sampler(mut ctx: TestTelemetryContext) {
5858
5959 assert_eq ! ( ctx. traces( Default :: default ( ) ) . len( ) , 0 ) ;
6060}
61+
62+ #[ with_test_telemetry( test) ]
63+ fn test_span_accessors ( mut ctx : TestTelemetryContext ) {
64+ // Initially, no trace (and no span) is present
65+ assert ! ( !tracing:: span_is_sampled( ) ) ;
66+ assert_eq ! ( tracing:: trace_id( ) , None ) ;
67+
68+ {
69+ // Start a new trace
70+ let _root_scope = tracing:: start_trace ( "my first span" , Default :: default ( ) ) ;
71+ assert ! ( tracing:: span_is_sampled( ) ) ;
72+
73+ let trace_id = tracing:: trace_id ( ) . expect ( "root scope should set trace ID" ) ;
74+ let trace_state =
75+ tracing:: state_for_trace_stitching ( ) . expect ( "root scope should set stitching state" ) ;
76+ assert_eq ! ( trace_state. trace_id( ) . to_string( ) , trace_id) ;
77+
78+ // Enter a child span, which should have the same trace ID
79+ let _child_scope = tracing:: span ( "my child span" ) ;
80+ assert ! ( tracing:: span_is_sampled( ) ) ;
81+ assert_eq ! ( tracing:: trace_id( ) , Some ( trace_id) ) ;
82+ }
83+
84+ // Change to the passive sampler, which won't sample new root traces
85+ ctx. set_tracing_settings ( TracingSettings {
86+ sampling_strategy : SamplingStrategy :: Passive ,
87+ ..Default :: default ( )
88+ } ) ;
89+ let _ctx_scope = ctx. scope ( ) ;
90+
91+ let _unsampled_scope = tracing:: start_trace ( "my unsampled span" , Default :: default ( ) ) ;
92+ assert ! ( !tracing:: span_is_sampled( ) ) ;
93+ assert_eq ! ( tracing:: trace_id( ) , None ) ;
94+ }
0 commit comments