@@ -71,17 +71,13 @@ def test_sampling_traces_sample_rate_50(sentry_init, capture_envelopes):
71
71
72
72
envelopes = capture_envelopes ()
73
73
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
77
75
with sentry_sdk .start_span (description = "request a" ):
78
76
with sentry_sdk .start_span (description = "cache a" ):
79
77
with sentry_sdk .start_span (description = "db a" ):
80
78
...
81
79
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
85
81
with sentry_sdk .start_span (description = "request b" ):
86
82
with sentry_sdk .start_span (description = "cache b" ):
87
83
with sentry_sdk .start_span (description = "db b" ):
@@ -101,46 +97,34 @@ def test_sampling_traces_sample_rate_50(sentry_init, capture_envelopes):
101
97
def test_sampling_traces_sampler (sentry_init , capture_envelopes ):
102
98
def keep_only_a (sampling_context ):
103
99
if " a" in sampling_context ["transaction_context" ]["name" ]:
104
- return 0.05
100
+ return 1
105
101
else :
106
102
return 0
107
103
108
- sentry_init (
109
- traces_sample_rate = 1.0 ,
110
- traces_sampler = keep_only_a ,
111
- )
104
+ sentry_init (traces_sampler = keep_only_a )
112
105
113
106
envelopes = capture_envelopes ()
114
107
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
+ ...
125
113
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
+ ...
130
118
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
+ ...
139
123
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
+ ...
144
128
145
129
assert len (envelopes ) == 2
146
130
(envelope1 , envelope2 ) = envelopes
@@ -150,7 +134,7 @@ def keep_only_a(sampling_context):
150
134
assert transaction1 ["transaction" ] == "request a"
151
135
assert len (transaction1 ["spans" ]) == 2
152
136
assert transaction2 ["transaction" ] == "new a c"
153
- assert len (transaction2 ["spans" ]) == 0
137
+ assert len (transaction2 ["spans" ]) == 2
154
138
155
139
156
140
def test_sampling_traces_sampler_boolean (sentry_init , capture_envelopes ):
@@ -168,21 +152,21 @@ def keep_only_a(sampling_context):
168
152
envelopes = capture_envelopes ()
169
153
170
154
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" ):
173
157
...
174
158
175
159
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" ):
178
162
...
179
163
180
164
assert len (envelopes ) == 1
181
165
(envelope ,) = envelopes
182
166
transaction = envelope .items [0 ].payload .json
183
167
184
168
assert transaction ["transaction" ] == "request a"
185
- assert len (transaction ["spans" ]) == 1
169
+ assert len (transaction ["spans" ]) == 2
186
170
187
171
188
172
@pytest .mark .parametrize (
@@ -237,21 +221,24 @@ def test_sampling_parent_sampled(
237
221
238
222
239
223
@pytest .mark .parametrize (
240
- "traces_sample_rate, expected_num_of_envelopes" ,
224
+ "traces_sample_rate, upstream_sampled, expected_num_of_envelopes" ,
241
225
[
242
226
# 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 ),
244
228
# 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 ),
246
230
# 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 ),
248
233
# 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 ),
250
236
],
251
237
)
252
238
def test_sampling_parent_dropped (
253
239
sentry_init ,
254
240
traces_sample_rate ,
241
+ upstream_sampled ,
255
242
expected_num_of_envelopes ,
256
243
capture_envelopes ,
257
244
):
@@ -265,7 +252,7 @@ def test_sampling_parent_dropped(
265
252
266
253
# The upstream service has dropped the request
267
254
headers = {
268
- "sentry-trace" : "771a43a4192642f0b136d5159a501700-1234567890abcdef-0 " ,
255
+ "sentry-trace" : f "771a43a4192642f0b136d5159a501700-1234567890abcdef-{ upstream_sampled } " ,
269
256
}
270
257
with sentry_sdk .continue_trace (headers ):
271
258
with sentry_sdk .start_span (description = "request a" ):
0 commit comments