@@ -32,6 +32,36 @@ def test_deterministic_sampled(sentry_init, capture_events, sample_rate, sample_
3232 assert len (events ) == int (sample_rand < sample_rate )
3333
3434
35+ @pytest .mark .parametrize ("sample_rand" , (0.0 , 0.25 , 0.5 , 0.75 ))
36+ @pytest .mark .parametrize ("sample_rate" , (0.0 , 0.25 , 0.5 , 0.75 , 1.0 ))
37+ def test_deterministic_sampled_span_streaming (
38+ sentry_init , capture_items , sample_rate , sample_rand
39+ ):
40+ """
41+ Test that sample_rand is generated on new traces, that it is used to
42+ make the sampling decision, and that it is included in the segment's
43+ baggage.
44+ """
45+ sentry_init (
46+ traces_sample_rate = sample_rate ,
47+ _experiments = {"trace_lifecycle" : "stream" },
48+ )
49+ items = capture_items ("span" )
50+
51+ with mock .patch (
52+ "sentry_sdk.tracing_utils.Random.randrange" ,
53+ return_value = int (sample_rand * 1000000 ),
54+ ):
55+ with sentry_sdk .traces .start_span (name = "span" ) as span :
56+ assert (
57+ span ._get_baggage ().sentry_items ["sample_rand" ] == f"{ sample_rand :.6f} " # noqa: E231
58+ )
59+
60+ # Span captured if sample_rand < sample_rate, indicating that
61+ # sample_rand is used to make the sampling decision.
62+ assert len (items ) == int (sample_rand < sample_rate )
63+
64+
3565@pytest .mark .parametrize ("sample_rand" , (0.0 , 0.25 , 0.5 , 0.75 ))
3666@pytest .mark .parametrize ("sample_rate" , (0.0 , 0.25 , 0.5 , 0.75 , 1.0 ))
3767def test_transaction_uses_incoming_sample_rand (
@@ -54,3 +84,31 @@ def test_transaction_uses_incoming_sample_rand(
5484 # Transaction event captured if sample_rand < sample_rate, indicating that
5585 # sample_rand is used to make the sampling decision.
5686 assert len (events ) == int (sample_rand < sample_rate )
87+
88+
89+ @pytest .mark .parametrize ("sample_rand" , (0.0 , 0.25 , 0.5 , 0.75 ))
90+ @pytest .mark .parametrize ("sample_rate" , (0.0 , 0.25 , 0.5 , 0.75 , 1.0 ))
91+ def test_transaction_uses_incoming_sample_rand_span_streaming (
92+ sentry_init , capture_items , sample_rate , sample_rand
93+ ):
94+ """
95+ Test that the segment uses the sample_rand value from the incoming baggage.
96+ """
97+ sentry_init (
98+ traces_sample_rate = sample_rate ,
99+ _experiments = {"trace_lifecycle" : "stream" },
100+ )
101+ items = capture_items ()
102+
103+ sentry_sdk .traces .continue_trace (
104+ {"baggage" : "sentry-sample_rand={sample_rand:.6f}" }
105+ )
106+
107+ with sentry_sdk .traces .start_span (name = "span" ) as span :
108+ assert (
109+ span ._get_baggage ().sentry_items ["sample_rand" ] == f"{ sample_rand :.6f} " # noqa: E231
110+ )
111+
112+ # Span captured if sample_rand < sample_rate, indicating that
113+ # sample_rand is used to make the sampling decision.
114+ assert len (items ) == int (sample_rand < sample_rate )
0 commit comments