@@ -2,6 +2,21 @@ defmodule Sentry.Opentelemetry.SamplerTest do
22 use Sentry.Case , async: true
33
44 alias Sentry.OpenTelemetry.Sampler
5+ alias Sentry.ClientReport
6+
7+ defp create_test_span_context ( span_id \\ 123_456_789 ) do
8+ {
9+ :span_ctx ,
10+ 12_345_678_901_234_567_890_123_456_789_012 ,
11+ span_id ,
12+ 1 ,
13+ [ ] ,
14+ true ,
15+ false ,
16+ true ,
17+ nil
18+ }
19+ end
520
621 setup do
722 original_rate = Sentry.Config . traces_sample_rate ( )
@@ -14,18 +29,29 @@ defmodule Sentry.Opentelemetry.SamplerTest do
1429 end
1530
1631 describe "span name dropping" do
17- test "drops spans with the given name" do
32+ test "drops spans with the given name and records discarded event" do
33+ :sys . replace_state ( ClientReport.Sender , fn _ -> % { } end )
34+
35+ test_ctx = create_test_span_context ( )
36+
1837 assert { :drop , [ ] , [ ] } =
19- Sampler . should_sample ( nil , nil , nil , "Elixir.Oban.Stager process" , nil , nil ,
38+ Sampler . should_sample ( test_ctx , nil , nil , "Elixir.Oban.Stager process" , nil , nil ,
2039 drop: [ "Elixir.Oban.Stager process" ]
2140 )
41+
42+ Process . sleep ( 10 )
43+
44+ state = :sys . get_state ( ClientReport.Sender )
45+ assert state == % { { :sample_rate , "transaction" } => 1 }
2246 end
2347
2448 test "records and samples spans not in drop list" do
2549 Sentry.Config . put_config ( :traces_sample_rate , 1.0 )
2650
51+ test_ctx = create_test_span_context ( )
52+
2753 assert { :record_and_sample , [ ] , tracestate } =
28- Sampler . should_sample ( nil , 123 , nil , "Elixir.Oban.Worker process" , nil , nil ,
54+ Sampler . should_sample ( test_ctx , 123 , nil , "Elixir.Oban.Worker process" , nil , nil ,
2955 drop: [ ]
3056 )
3157
@@ -36,21 +62,32 @@ defmodule Sentry.Opentelemetry.SamplerTest do
3662 end
3763
3864 describe "sampling based on traces_sample_rate" do
39- test "always drops when sample rate is 0.0" do
65+ test "always drops when sample rate is 0.0 and records discarded event" do
66+ :sys . replace_state ( ClientReport.Sender , fn _ -> % { } end )
67+
4068 Sentry.Config . put_config ( :traces_sample_rate , 0.0 )
4169
70+ test_ctx = create_test_span_context ( )
71+
4272 assert { :drop , [ ] , tracestate } =
43- Sampler . should_sample ( nil , 123 , nil , "test span" , nil , nil , drop: [ ] )
73+ Sampler . should_sample ( test_ctx , 123 , nil , "test span" , nil , nil , drop: [ ] )
4474
4575 assert { "sentry-sample_rate" , "0.0" } in tracestate
4676 assert { "sentry-sampled" , "false" } in tracestate
77+
78+ Process . sleep ( 10 )
79+
80+ state = :sys . get_state ( ClientReport.Sender )
81+ assert state == % { { :sample_rate , "transaction" } => 1 }
4782 end
4883
4984 test "always samples when sample rate is 1.0" do
5085 Sentry.Config . put_config ( :traces_sample_rate , 1.0 )
5186
87+ test_ctx = create_test_span_context ( )
88+
5289 assert { :record_and_sample , [ ] , tracestate } =
53- Sampler . should_sample ( nil , 123 , nil , "test span" , nil , nil , drop: [ ] )
90+ Sampler . should_sample ( test_ctx , 123 , nil , "test span" , nil , nil , drop: [ ] )
5491
5592 assert { "sentry-sample_rate" , "1.0" } in tracestate
5693 assert { "sentry-sampled" , "true" } in tracestate
@@ -63,8 +100,10 @@ defmodule Sentry.Opentelemetry.SamplerTest do
63100
64101 results =
65102 Enum . map ( trace_ids , fn trace_id ->
103+ test_ctx = create_test_span_context ( )
104+
66105 { decision , [ ] , _tracestate } =
67- Sampler . should_sample ( nil , trace_id , nil , "test span" , nil , nil , drop: [ ] )
106+ Sampler . should_sample ( test_ctx , trace_id , nil , "test span" , nil , nil , drop: [ ] )
68107
69108 decision == :record_and_sample
70109 end )
@@ -73,23 +112,44 @@ defmodule Sentry.Opentelemetry.SamplerTest do
73112
74113 assert sampled_count > 30 and sampled_count < 70
75114 end
115+
116+ test "records discarded events when randomly dropped by sample rate" do
117+ :sys . replace_state ( ClientReport.Sender , fn _ -> % { } end )
118+
119+ Sentry.Config . put_config ( :traces_sample_rate , 0.001 )
120+
121+ Enum . each ( 1 .. 50 , fn trace_id ->
122+ test_ctx = create_test_span_context ( )
123+ Sampler . should_sample ( test_ctx , trace_id , nil , "test span" , nil , nil , drop: [ ] )
124+ end )
125+
126+ Process . sleep ( 10 )
127+
128+ state = :sys . get_state ( ClientReport.Sender )
129+ discarded_count = Map . get ( state , { :sample_rate , "transaction" } , 0 )
130+ assert discarded_count > 0 , "Expected some spans to be dropped and recorded"
131+ end
76132 end
77133
78134 describe "parent span inheritance" do
79135 test "inherits sampling decision from parent span with same trace_id" do
80136 Sentry.Config . put_config ( :traces_sample_rate , 1.0 )
81137
138+ test_ctx = create_test_span_context ( )
139+
82140 assert { :record_and_sample , [ ] , _tracestate } =
83- Sampler . should_sample ( nil , 123 , nil , "test span" , nil , nil , drop: [ ] )
141+ Sampler . should_sample ( test_ctx , 123 , nil , "test span" , nil , nil , drop: [ ] )
84142 end
85143 end
86144
87145 describe "tracestate management" do
88146 test "builds tracestate with correct format" do
89147 Sentry.Config . put_config ( :traces_sample_rate , 0.75 )
90148
149+ test_ctx = create_test_span_context ( )
150+
91151 { _decision , [ ] , tracestate } =
92- Sampler . should_sample ( nil , 123 , nil , "test span" , nil , nil , drop: [ ] )
152+ Sampler . should_sample ( test_ctx , 123 , nil , "test span" , nil , nil , drop: [ ] )
93153
94154 assert List . keyfind ( tracestate , "sentry-sample_rate" , 0 )
95155 assert List . keyfind ( tracestate , "sentry-sample_rand" , 0 )
0 commit comments