-
Notifications
You must be signed in to change notification settings - Fork 535
/
Copy pathtest_sampler.py
334 lines (272 loc) · 12 KB
/
test_sampler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import pytest
from unittest import mock
from opentelemetry import trace
import sentry_sdk
USE_DEFAULT_TRACES_SAMPLE_RATE = -1
tracer = trace.get_tracer(__name__)
@pytest.mark.parametrize(
"traces_sample_rate, expected_num_of_envelopes",
[
# special case for testing, do not pass any traces_sample_rate to init() (the default traces_sample_rate=0 will be used)
(USE_DEFAULT_TRACES_SAMPLE_RATE, 0),
# traces_sample_rate=None means do not create new traces, and also do not continue incoming traces. So, no envelopes at all.
(None, 0),
# 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.
(0, 0),
# traces_sample_rate=1 means create new traces for 100% of requests (and also continue incoming traces, of course).
(1, 2),
],
)
def test_sampling_traces_sample_rate_0_or_100(
sentry_init,
capture_envelopes,
traces_sample_rate,
expected_num_of_envelopes,
):
kwargs = {}
if traces_sample_rate != USE_DEFAULT_TRACES_SAMPLE_RATE:
kwargs["traces_sample_rate"] = traces_sample_rate
sentry_init(**kwargs)
envelopes = capture_envelopes()
with sentry_sdk.start_span(description="request a"):
with sentry_sdk.start_span(description="cache a"):
with sentry_sdk.start_span(description="db a"):
...
with sentry_sdk.start_span(description="request b"):
with sentry_sdk.start_span(description="cache b"):
with sentry_sdk.start_span(description="db b"):
...
assert len(envelopes) == expected_num_of_envelopes
if expected_num_of_envelopes == 2:
(transaction_a, transaction_b) = [
envelope.items[0].payload.json for envelope in envelopes
]
assert transaction_a["transaction"] == "request a"
assert transaction_b["transaction"] == "request b"
spans_a = transaction_a["spans"]
assert len(spans_a) == 2
assert spans_a[0]["description"] == "cache a"
assert spans_a[1]["description"] == "db a"
spans_b = transaction_b["spans"]
assert len(spans_b) == 2
assert spans_b[0]["description"] == "cache b"
assert spans_b[1]["description"] == "db b"
def test_sampling_traces_sample_rate_50(sentry_init, capture_envelopes):
sentry_init(traces_sample_rate=0.5)
envelopes = capture_envelopes()
with mock.patch(
"sentry_sdk.tracing_utils.Random.uniform", return_value=0.2
): # drop
with sentry_sdk.start_span(description="request a"):
with sentry_sdk.start_span(description="cache a"):
with sentry_sdk.start_span(description="db a"):
...
with mock.patch(
"sentry_sdk.tracing_utils.Random.uniform", return_value=0.7
): # keep
with sentry_sdk.start_span(description="request b"):
with sentry_sdk.start_span(description="cache b"):
with sentry_sdk.start_span(description="db b"):
...
assert len(envelopes) == 1
(envelope,) = envelopes
transaction = envelope.items[0].payload.json
assert transaction["transaction"] == "request a"
spans = transaction["spans"]
assert len(spans) == 2
assert spans[0]["description"] == "cache a"
assert spans[1]["description"] == "db a"
def test_sampling_traces_sampler(sentry_init, capture_envelopes):
def keep_only_a(sampling_context):
if " a" in sampling_context["transaction_context"]["name"]:
return 1
else:
return 0
sentry_init(traces_sampler=keep_only_a)
envelopes = capture_envelopes()
# children inherit from root spans
with sentry_sdk.start_span(description="request a"): # keep
with sentry_sdk.start_span(description="cache a"):
with sentry_sdk.start_span(description="db a"):
...
with sentry_sdk.start_span(description="request b"): # drop
with sentry_sdk.start_span(description="cache b"):
with sentry_sdk.start_span(description="db b"):
...
with sentry_sdk.start_span(description="request c"): # drop
with sentry_sdk.start_span(description="cache a c"):
with sentry_sdk.start_span(description="db a c"):
...
with sentry_sdk.start_span(description="new a c"): # keep
with sentry_sdk.start_span(description="cache c"):
with sentry_sdk.start_span(description="db c"):
...
assert len(envelopes) == 2
(envelope1, envelope2) = envelopes
transaction1 = envelope1.items[0].payload.json
transaction2 = envelope2.items[0].payload.json
assert transaction1["transaction"] == "request a"
assert len(transaction1["spans"]) == 2
assert transaction2["transaction"] == "new a c"
assert len(transaction2["spans"]) == 2
def test_sampling_traces_sampler_boolean(sentry_init, capture_envelopes):
def keep_only_a(sampling_context):
if " a" in sampling_context["transaction_context"]["name"]:
return True
else:
return False
sentry_init(
traces_sample_rate=1.0,
traces_sampler=keep_only_a,
)
envelopes = capture_envelopes()
with sentry_sdk.start_span(description="request a"): # keep
with sentry_sdk.start_span(description="cache a"):
with sentry_sdk.start_span(description="db X"):
...
with sentry_sdk.start_span(description="request b"): # drop
with sentry_sdk.start_span(description="cache b"):
with sentry_sdk.start_span(description="db b"):
...
assert len(envelopes) == 1
(envelope,) = envelopes
transaction = envelope.items[0].payload.json
assert transaction["transaction"] == "request a"
assert len(transaction["spans"]) == 2
@pytest.mark.parametrize(
"traces_sample_rate, expected_num_of_envelopes",
[
# special case for testing, do not pass any traces_sample_rate to init() (the default traces_sample_rate=0 will be used)
(USE_DEFAULT_TRACES_SAMPLE_RATE, 1),
# traces_sample_rate=None means do not create new traces, and also do not continue incoming traces. So, no envelopes at all.
(None, 0),
# 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.
(0, 1),
# traces_sample_rate=1 means create new traces for 100% of requests (and also continue incoming traces, of course).
(1, 1),
],
)
def test_sampling_parent_sampled(
sentry_init,
traces_sample_rate,
expected_num_of_envelopes,
capture_envelopes,
):
kwargs = {}
if traces_sample_rate != USE_DEFAULT_TRACES_SAMPLE_RATE:
kwargs["traces_sample_rate"] = traces_sample_rate
sentry_init(**kwargs)
envelopes = capture_envelopes()
# The upstream service has sampled the request
headers = {
"sentry-trace": "771a43a4192642f0b136d5159a501700-1234567890abcdef-1",
}
with sentry_sdk.continue_trace(headers):
with sentry_sdk.start_span(description="request a"):
with sentry_sdk.start_span(description="cache a"):
with sentry_sdk.start_span(description="db X"):
...
assert len(envelopes) == expected_num_of_envelopes
if expected_num_of_envelopes == 1:
(envelope,) = envelopes
transaction = envelope.items[0].payload.json
assert transaction["transaction"] == "request a"
assert (
transaction["contexts"]["trace"]["trace_id"]
== "771a43a4192642f0b136d5159a501700"
)
assert transaction["contexts"]["trace"]["span_id"] != "1234567890abcdef"
assert transaction["contexts"]["trace"]["parent_span_id"] == "1234567890abcdef"
@pytest.mark.parametrize(
"traces_sample_rate, upstream_sampled, expected_num_of_envelopes",
[
# special case for testing, do not pass any traces_sample_rate to init() (the default traces_sample_rate=0 will be used)
(USE_DEFAULT_TRACES_SAMPLE_RATE, 0, 0),
(USE_DEFAULT_TRACES_SAMPLE_RATE, 1, 1),
# traces_sample_rate=None means do not create new traces, and also do not continue incoming traces. So, no envelopes at all.
(None, 0, 0),
(None, 1, 0),
# 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.
(0, 0, 0),
(0, 1, 1),
# traces_sample_rate=1 means create new traces for 100% of requests (and also continue incoming traces, of course).
(1, 0, 0),
(1, 1, 1),
],
)
def test_sampling_parent_dropped(
sentry_init,
traces_sample_rate,
upstream_sampled,
expected_num_of_envelopes,
capture_envelopes,
):
kwargs = {}
if traces_sample_rate != USE_DEFAULT_TRACES_SAMPLE_RATE:
kwargs["traces_sample_rate"] = traces_sample_rate
sentry_init(**kwargs)
envelopes = capture_envelopes()
# The upstream service has dropped the request
headers = {
"sentry-trace": f"771a43a4192642f0b136d5159a501700-1234567890abcdef-{upstream_sampled}",
}
with sentry_sdk.continue_trace(headers):
with sentry_sdk.start_span(description="request a"):
with sentry_sdk.start_span(description="cache a"):
with sentry_sdk.start_span(description="db X"):
...
assert len(envelopes) == expected_num_of_envelopes
if expected_num_of_envelopes == 1:
(envelope,) = envelopes
transaction = envelope.items[0].payload.json
assert transaction["transaction"] == "request a"
assert (
transaction["contexts"]["trace"]["trace_id"]
== "771a43a4192642f0b136d5159a501700"
)
assert transaction["contexts"]["trace"]["span_id"] != "1234567890abcdef"
assert transaction["contexts"]["trace"]["parent_span_id"] == "1234567890abcdef"
@pytest.mark.parametrize(
"traces_sample_rate, expected_num_of_envelopes",
[
# special case for testing, do not pass any traces_sample_rate to init() (the default traces_sample_rate=0 will be used)
(USE_DEFAULT_TRACES_SAMPLE_RATE, 0),
# traces_sample_rate=None means do not create new traces, and also do not continue incoming traces. So, no envelopes at all.
(None, 0),
# 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.
(0, 0),
# traces_sample_rate=1 means create new traces for 100% of requests (and also continue incoming traces, of course).
(1, 1),
],
)
def test_sampling_parent_deferred(
sentry_init,
traces_sample_rate,
expected_num_of_envelopes,
capture_envelopes,
):
kwargs = {}
if traces_sample_rate != USE_DEFAULT_TRACES_SAMPLE_RATE:
kwargs["traces_sample_rate"] = traces_sample_rate
sentry_init(**kwargs)
envelopes = capture_envelopes()
# The upstream service has deferred the sampling decision to us.
headers = {
"sentry-trace": "771a43a4192642f0b136d5159a501700-1234567890abcdef-",
}
with sentry_sdk.continue_trace(headers):
with sentry_sdk.start_span(description="request a"):
with sentry_sdk.start_span(description="cache a"):
with sentry_sdk.start_span(description="db X"):
...
assert len(envelopes) == expected_num_of_envelopes
if expected_num_of_envelopes == 1:
(envelope,) = envelopes
transaction = envelope.items[0].payload.json
assert transaction["transaction"] == "request a"
assert (
transaction["contexts"]["trace"]["trace_id"]
== "771a43a4192642f0b136d5159a501700"
)
assert transaction["contexts"]["trace"]["span_id"] != "1234567890abcdef"
assert transaction["contexts"]["trace"]["parent_span_id"] == "1234567890abcdef"