@@ -32,6 +32,38 @@ 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+ 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+ sentry_sdk .flush ()
61+
62+ # Span captured if sample_rand < sample_rate, indicating that
63+ # sample_rand is used to make the sampling decision.
64+ assert len (items ) == int (sample_rand < sample_rate )
65+
66+
3567@pytest .mark .parametrize ("sample_rand" , (0.0 , 0.25 , 0.5 , 0.75 ))
3668@pytest .mark .parametrize ("sample_rate" , (0.0 , 0.25 , 0.5 , 0.75 , 1.0 ))
3769def test_transaction_uses_incoming_sample_rand (
@@ -54,3 +86,36 @@ def test_transaction_uses_incoming_sample_rand(
5486 # Transaction event captured if sample_rand < sample_rate, indicating that
5587 # sample_rand is used to make the sampling decision.
5688 assert len (events ) == int (sample_rand < sample_rate )
89+
90+
91+ @pytest .mark .parametrize ("sample_rand" , (0.0 , 0.25 , 0.5 , 0.75 ))
92+ @pytest .mark .parametrize ("sample_rate" , (0.0 , 0.25 , 0.5 , 0.75 , 1.0 ))
93+ def test_segment_uses_incoming_sample_rand_span_streaming (
94+ sentry_init , capture_items , sample_rate , sample_rand
95+ ):
96+ """
97+ Test that the segment uses the sample_rand value from the incoming baggage.
98+ """
99+ sentry_init (
100+ traces_sample_rate = sample_rate ,
101+ trace_lifecycle = "stream" ,
102+ )
103+ items = capture_items ("span" )
104+
105+ sentry_sdk .traces .continue_trace (
106+ {
107+ "sentry-trace" : "0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331" ,
108+ "baggage" : f"sentry-sample_rand={ sample_rand :.6f} " ,
109+ }
110+ )
111+
112+ with sentry_sdk .traces .start_span (name = "span" ) as span :
113+ assert (
114+ span ._get_baggage ().sentry_items ["sample_rand" ] == f"{ sample_rand :.6f} " # noqa: E231
115+ )
116+
117+ sentry_sdk .flush ()
118+
119+ # Span captured if sample_rand < sample_rate, indicating that
120+ # sample_rand is used to make the sampling decision.
121+ assert len (items ) == int (sample_rand < sample_rate )
0 commit comments