@@ -71,17 +71,13 @@ def test_sampling_traces_sample_rate_50(sentry_init, capture_envelopes):
7171
7272 envelopes = capture_envelopes ()
7373
74- with mock .patch (
75- "sentry_sdk.integrations.opentelemetry.sampler.random" , return_value = 0.2
76- ): # drop
74+ with mock .patch ("random.random" , return_value = 0.2 ): # drop
7775 with sentry_sdk .start_span (description = "request a" ):
7876 with sentry_sdk .start_span (description = "cache a" ):
7977 with sentry_sdk .start_span (description = "db a" ):
8078 ...
8179
82- with mock .patch (
83- "sentry_sdk.integrations.opentelemetry.sampler.random" , return_value = 0.7
84- ): # keep
80+ with mock .patch ("random.random" , return_value = 0.7 ): # keep
8581 with sentry_sdk .start_span (description = "request b" ):
8682 with sentry_sdk .start_span (description = "cache b" ):
8783 with sentry_sdk .start_span (description = "db b" ):
@@ -101,46 +97,34 @@ def test_sampling_traces_sample_rate_50(sentry_init, capture_envelopes):
10197def test_sampling_traces_sampler (sentry_init , capture_envelopes ):
10298 def keep_only_a (sampling_context ):
10399 if " a" in sampling_context ["transaction_context" ]["name" ]:
104- return 0.05
100+ return 1
105101 else :
106102 return 0
107103
108- sentry_init (
109- traces_sample_rate = 1.0 ,
110- traces_sampler = keep_only_a ,
111- )
104+ sentry_init (traces_sampler = keep_only_a )
112105
113106 envelopes = capture_envelopes ()
114107
115- # Make sure random() always returns the same values
116- with mock .patch (
117- "sentry_sdk.integrations.opentelemetry.sampler.random" ,
118- side_effect = [0.04 for _ in range (12 )],
119- ):
120-
121- with sentry_sdk .start_span (description = "request a" ): # keep
122- with sentry_sdk .start_span (description = "cache a" ): # keep
123- with sentry_sdk .start_span (description = "db a" ): # keep
124- ...
108+ # children inherit from root spans
109+ with sentry_sdk .start_span (description = "request a" ): # keep
110+ with sentry_sdk .start_span (description = "cache a" ):
111+ with sentry_sdk .start_span (description = "db a" ):
112+ ...
125113
126- with sentry_sdk .start_span (description = "request b" ): # drop
127- with sentry_sdk .start_span (description = "cache b" ): # drop
128- with sentry_sdk .start_span (description = "db b" ): # drop
129- ...
114+ with sentry_sdk .start_span (description = "request b" ): # drop
115+ with sentry_sdk .start_span (description = "cache b" ):
116+ with sentry_sdk .start_span (description = "db b" ):
117+ ...
130118
131- with sentry_sdk .start_span (description = "request c" ): # drop
132- with sentry_sdk .start_span (
133- description = "cache a c"
134- ): # keep (but trx dropped, so not collected)
135- with sentry_sdk .start_span (
136- description = "db a c"
137- ): # keep (but trx dropped, so not collected)
138- ...
119+ with sentry_sdk .start_span (description = "request c" ): # drop
120+ with sentry_sdk .start_span (description = "cache a c" ):
121+ with sentry_sdk .start_span (description = "db a c" ):
122+ ...
139123
140- with sentry_sdk .start_span (description = "new a c" ): # keep
141- with sentry_sdk .start_span (description = "cache c" ): # drop
142- with sentry_sdk .start_span (description = "db c" ): # drop
143- ...
124+ with sentry_sdk .start_span (description = "new a c" ): # keep
125+ with sentry_sdk .start_span (description = "cache c" ):
126+ with sentry_sdk .start_span (description = "db c" ):
127+ ...
144128
145129 assert len (envelopes ) == 2
146130 (envelope1 , envelope2 ) = envelopes
@@ -150,7 +134,7 @@ def keep_only_a(sampling_context):
150134 assert transaction1 ["transaction" ] == "request a"
151135 assert len (transaction1 ["spans" ]) == 2
152136 assert transaction2 ["transaction" ] == "new a c"
153- assert len (transaction2 ["spans" ]) == 0
137+ assert len (transaction2 ["spans" ]) == 2
154138
155139
156140def test_sampling_traces_sampler_boolean (sentry_init , capture_envelopes ):
@@ -168,21 +152,21 @@ def keep_only_a(sampling_context):
168152 envelopes = capture_envelopes ()
169153
170154 with sentry_sdk .start_span (description = "request a" ): # keep
171- with sentry_sdk .start_span (description = "cache a" ): # keep
172- with sentry_sdk .start_span (description = "db X" ): # drop
155+ with sentry_sdk .start_span (description = "cache a" ):
156+ with sentry_sdk .start_span (description = "db X" ):
173157 ...
174158
175159 with sentry_sdk .start_span (description = "request b" ): # drop
176- with sentry_sdk .start_span (description = "cache b" ): # drop
177- with sentry_sdk .start_span (description = "db b" ): # drop
160+ with sentry_sdk .start_span (description = "cache b" ):
161+ with sentry_sdk .start_span (description = "db b" ):
178162 ...
179163
180164 assert len (envelopes ) == 1
181165 (envelope ,) = envelopes
182166 transaction = envelope .items [0 ].payload .json
183167
184168 assert transaction ["transaction" ] == "request a"
185- assert len (transaction ["spans" ]) == 1
169+ assert len (transaction ["spans" ]) == 2
186170
187171
188172@pytest .mark .parametrize (
@@ -237,21 +221,24 @@ def test_sampling_parent_sampled(
237221
238222
239223@pytest .mark .parametrize (
240- "traces_sample_rate, expected_num_of_envelopes" ,
224+ "traces_sample_rate, upstream_sampled, expected_num_of_envelopes" ,
241225 [
242226 # special case for testing, do not pass any traces_sample_rate to init() (the default traces_sample_rate=None will be used)
243- (- 1 , 0 ),
227+ (- 1 , 0 , 0 ),
244228 # traces_sample_rate=None means do not create new traces, and also do not continue incoming traces. So, no envelopes at all.
245- (None , 0 ),
229+ (None , 1 , 0 ),
246230 # traces_sample_rate=0 means do not create new traces (0% of the requests), but continue incoming traces. So envelopes will be created only if there is an incoming trace.
247- (0 , 0 ),
231+ (0 , 0 , 0 ),
232+ (0 , 1 , 1 ),
248233 # traces_sample_rate=1 means create new traces for 100% of requests (and also continue incoming traces, of course).
249- (1 , 1 ),
234+ (1 , 0 , 0 ),
235+ (1 , 1 , 1 ),
250236 ],
251237)
252238def test_sampling_parent_dropped (
253239 sentry_init ,
254240 traces_sample_rate ,
241+ upstream_sampled ,
255242 expected_num_of_envelopes ,
256243 capture_envelopes ,
257244):
@@ -265,7 +252,7 @@ def test_sampling_parent_dropped(
265252
266253 # The upstream service has dropped the request
267254 headers = {
268- "sentry-trace" : "771a43a4192642f0b136d5159a501700-1234567890abcdef-0 " ,
255+ "sentry-trace" : f "771a43a4192642f0b136d5159a501700-1234567890abcdef-{ upstream_sampled } " ,
269256 }
270257 with sentry_sdk .continue_trace (headers ):
271258 with sentry_sdk .start_span (description = "request a" ):
0 commit comments