@@ -374,7 +374,16 @@ defmodule Sentry.Opentelemetry.SamplerTest do
374374 Sampler . should_sample ( test_ctx , 123 , nil , "test span" , nil , % { } , drop: [ ] )
375375 end
376376
377- test "traces_sampler can override parent sampling decision" do
377+ test "child spans inherit parent sampling decision without calling traces_sampler" do
378+ { :ok , sampler_call_count } = Agent . start_link ( fn -> 0 end )
379+
380+ sampler_fun = fn _sampling_context ->
381+ Agent . update ( sampler_call_count , & ( & 1 + 1 ) )
382+ false
383+ end
384+
385+ put_test_config ( traces_sampler: sampler_fun )
386+
378387 trace_tracestate = [
379388 { "sentry-sample_rate" , "1.0" } ,
380389 { "sentry-sample_rand" , "0.5" } ,
@@ -388,15 +397,40 @@ defmodule Sentry.Opentelemetry.SamplerTest do
388397 token = :otel_ctx . attach ( ctx_with_span )
389398
390399 try do
391- put_test_config ( traces_sampler: fn _ -> false end )
392-
393400 result =
394401 Sampler . should_sample ( ctx_with_span , 123 , nil , "child span" , nil , % { } , drop: [ ] )
395402
396- assert { :drop , [ ] , _tracestate } = result
403+ assert { :record_and_sample , [ ] , returned_tracestate } = result
404+ assert returned_tracestate == trace_tracestate
405+
406+ call_count = Agent . get ( sampler_call_count , & & 1 )
407+ assert call_count == 0
397408 after
398409 :otel_ctx . detach ( token )
410+ Agent . stop ( sampler_call_count )
411+ end
412+ end
413+
414+ test "traces_sampler is only called for root spans" do
415+ { :ok , sampler_call_count } = Agent . start_link ( fn -> 0 end )
416+
417+ sampler_fun = fn _sampling_context ->
418+ Agent . update ( sampler_call_count , & ( & 1 + 1 ) )
419+ true
399420 end
421+
422+ put_test_config ( traces_sampler: sampler_fun )
423+
424+ test_ctx = create_test_span_context ( )
425+
426+ result = Sampler . should_sample ( test_ctx , 123 , nil , "root span" , nil , % { } , drop: [ ] )
427+
428+ assert { :record_and_sample , [ ] , _tracestate } = result
429+
430+ call_count = Agent . get ( sampler_call_count , & & 1 )
431+ assert call_count == 1
432+
433+ Agent . stop ( sampler_call_count )
400434 end
401435
402436 test "handles traces_sampler errors gracefully" do
0 commit comments