forked from getsentry/sentry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sampling.py
More file actions
683 lines (542 loc) · 21 KB
/
Copy pathtest_sampling.py
File metadata and controls
683 lines (542 loc) · 21 KB
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
import random
from collections import Counter
from unittest import mock
import pytest
import sentry_sdk
from sentry_sdk import capture_exception, start_span, start_transaction
from sentry_sdk.tracing_utils import Baggage
from sentry_sdk.utils import logger
def test_sampling_decided_only_for_transactions(sentry_init, capture_events):
sentry_init(traces_sample_rate=0.5)
with start_transaction(name="hi") as transaction:
assert transaction.sampled is not None
with start_span() as span:
assert span.sampled == transaction.sampled
with start_span() as span:
assert span.sampled is None
def test_sampling_decided_only_for_segments(sentry_init, capture_events):
sentry_init(
traces_sample_rate=0.5,
_experiments={"trace_lifecycle": "stream"},
)
with sentry_sdk.traces.start_span(name="hi") as segment:
assert segment.sampled is not None
with sentry_sdk.traces.start_span(name="hey") as span:
assert span.sampled == segment.sampled
@pytest.mark.parametrize("sampled", [True, False])
def test_nested_transaction_sampling_override(sentry_init, sampled):
sentry_init(traces_sample_rate=1.0)
with start_transaction(name="outer", sampled=sampled) as outer_transaction:
assert outer_transaction.sampled is sampled
with start_transaction(
name="inner", sampled=(not sampled)
) as inner_transaction:
assert inner_transaction.sampled is not sampled
assert outer_transaction.sampled is sampled
def test_no_double_sampling(sentry_init, capture_events):
# Transactions should not be subject to the global/error sample rate.
# Only the traces_sample_rate should apply.
sentry_init(traces_sample_rate=1.0, sample_rate=0.0)
events = capture_events()
with start_transaction(name="/"):
pass
assert len(events) == 1
def test_no_double_sampling_span_streaming(sentry_init, capture_items):
# Segments should not be subject to the global/error sample rate.
# Only the traces_sample_rate should apply.
sentry_init(
traces_sample_rate=1.0,
sample_rate=0.0,
_experiments={"trace_lifecycle": "stream"},
)
items = capture_items()
with sentry_sdk.traces.start_span(name="/"):
pass
sentry_sdk.flush()
assert len(items) == 1
@pytest.mark.parametrize("sampling_decision", [True, False])
def test_get_transaction_and_span_from_scope_regardless_of_sampling_decision(
sentry_init, sampling_decision
):
sentry_init(traces_sample_rate=1.0)
with start_transaction(name="/", sampled=sampling_decision):
with start_span(op="child-span"):
with start_span(op="child-child-span"):
scope = sentry_sdk.get_current_scope()
assert scope.span.op == "child-child-span"
assert scope.transaction.name == "/"
@pytest.mark.parametrize(
"traces_sample_rate,expected_decision",
[(0.0, False), (0.25, False), (0.75, True), (1.00, True)],
)
def test_uses_traces_sample_rate_correctly(
sentry_init,
traces_sample_rate,
expected_decision,
):
sentry_init(traces_sample_rate=traces_sample_rate)
baggage = Baggage(sentry_items={"sample_rand": "0.500000"})
transaction = start_transaction(name="dogpark", baggage=baggage)
assert transaction.sampled is expected_decision
@pytest.mark.parametrize(
"traces_sample_rate,expected_decision",
[(0.0, False), (0.25, False), (0.75, True), (1.00, True)],
)
def test_uses_traces_sample_rate_correctly_span_streaming(
sentry_init,
traces_sample_rate,
expected_decision,
):
sentry_init(
traces_sample_rate=traces_sample_rate,
_experiments={
"trace_lifecycle": "stream",
},
)
sentry_sdk.traces.continue_trace(
{
"sentry-trace": "0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331",
"baggage": "sentry-sample_rand=0.500000",
}
)
with sentry_sdk.traces.start_span(name="dogpark") as span:
assert span.sampled is expected_decision
@pytest.mark.parametrize(
"traces_sampler_return_value,expected_decision",
[(0.0, False), (0.25, False), (0.75, True), (1.00, True)],
)
def test_uses_traces_sampler_return_value_correctly(
sentry_init,
traces_sampler_return_value,
expected_decision,
):
sentry_init(traces_sampler=mock.Mock(return_value=traces_sampler_return_value))
baggage = Baggage(sentry_items={"sample_rand": "0.500000"})
transaction = start_transaction(name="dogpark", baggage=baggage)
assert transaction.sampled is expected_decision
@pytest.mark.parametrize(
"traces_sampler_return_value,expected_decision",
[(0.0, False), (0.25, False), (0.75, True), (1.00, True)],
)
def test_uses_traces_sampler_return_value_correctly_span_streaming(
sentry_init,
traces_sampler_return_value,
expected_decision,
):
sentry_init(
traces_sampler=mock.Mock(return_value=traces_sampler_return_value),
_experiments={
"trace_lifecycle": "stream",
},
)
sentry_sdk.traces.continue_trace(
{
"sentry-trace": "0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331",
"baggage": "sentry-sample_rand=0.500000",
}
)
with sentry_sdk.traces.start_span(name="dogpark") as span:
assert span.sampled is expected_decision
@pytest.mark.parametrize("traces_sampler_return_value", [True, False])
def test_tolerates_traces_sampler_returning_a_boolean(
sentry_init, traces_sampler_return_value
):
sentry_init(traces_sampler=mock.Mock(return_value=traces_sampler_return_value))
transaction = start_transaction(name="dogpark")
assert transaction.sampled is traces_sampler_return_value
@pytest.mark.parametrize("traces_sampler_return_value", [True, False])
def test_tolerates_traces_sampler_returning_a_boolean_span_streaming(
sentry_init, traces_sampler_return_value
):
sentry_init(
traces_sampler=mock.Mock(return_value=traces_sampler_return_value),
_experiments={
"trace_lifecycle": "stream",
},
)
with sentry_sdk.traces.start_span(name="dogpark") as span:
assert span.sampled is traces_sampler_return_value
@pytest.mark.parametrize("sampling_decision", [True, False])
def test_only_captures_transaction_when_sampled_is_true(
sentry_init, sampling_decision, capture_events
):
sentry_init(traces_sampler=mock.Mock(return_value=sampling_decision))
events = capture_events()
transaction = start_transaction(name="dogpark")
transaction.finish()
assert len(events) == (1 if sampling_decision else 0)
@pytest.mark.parametrize("sampling_decision", [True, False])
def test_only_captures_segment_when_sampled_is_true_span_streaming(
sentry_init, sampling_decision, capture_items
):
sentry_init(
traces_sampler=mock.Mock(
return_value=sampling_decision,
),
_experiments={
"trace_lifecycle": "stream",
},
)
items = capture_items()
span = sentry_sdk.traces.start_span(name="dogpark")
span.end()
sentry_sdk.flush()
assert len(items) == (1 if sampling_decision else 0)
@pytest.mark.parametrize(
"traces_sample_rate,traces_sampler_return_value", [(0, True), (1, False)]
)
def test_prefers_traces_sampler_to_traces_sample_rate(
sentry_init,
traces_sample_rate,
traces_sampler_return_value,
):
# make traces_sample_rate imply the opposite of traces_sampler, to prove
# that traces_sampler takes precedence
traces_sampler = mock.Mock(return_value=traces_sampler_return_value)
sentry_init(
traces_sample_rate=traces_sample_rate,
traces_sampler=traces_sampler,
)
transaction = start_transaction(name="dogpark")
assert traces_sampler.called is True
assert transaction.sampled is traces_sampler_return_value
@pytest.mark.parametrize(
"traces_sample_rate,traces_sampler_return_value", [(0, True), (1, False)]
)
def test_prefers_traces_sampler_to_traces_sample_rate_span_streaming(
sentry_init,
traces_sample_rate,
traces_sampler_return_value,
):
# make traces_sample_rate imply the opposite of traces_sampler, to prove
# that traces_sampler takes precedence
traces_sampler = mock.Mock(return_value=traces_sampler_return_value)
sentry_init(
traces_sample_rate=traces_sample_rate,
traces_sampler=traces_sampler,
_experiments={
"trace_lifecycle": "stream",
},
)
span = sentry_sdk.traces.start_span(name="dogpark")
assert traces_sampler.called is True
assert span.sampled is traces_sampler_return_value
@pytest.mark.parametrize("parent_sampling_decision", [True, False])
def test_ignores_inherited_sample_decision_when_traces_sampler_defined(
sentry_init, parent_sampling_decision
):
# make traces_sampler pick the opposite of the inherited decision, to prove
# that traces_sampler takes precedence
traces_sampler = mock.Mock(return_value=not parent_sampling_decision)
sentry_init(traces_sampler=traces_sampler)
transaction = start_transaction(
name="dogpark", parent_sampled=parent_sampling_decision
)
assert transaction.sampled is not parent_sampling_decision
@pytest.mark.parametrize("parent_sampling_decision", ["1", "0"])
def test_ignores_inherited_sample_decision_when_traces_sampler_defined_span_streaming(
sentry_init, parent_sampling_decision
):
# make traces_sampler pick the opposite of the inherited decision, to prove
# that traces_sampler takes precedence
traces_sampler = mock.Mock(return_value=not bool(int(parent_sampling_decision)))
sentry_init(
traces_sampler=traces_sampler,
_experiments={"trace_lifecycle": "stream"},
)
sentry_sdk.traces.continue_trace(
{
"sentry-trace": f"0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-{parent_sampling_decision}"
}
)
span = sentry_sdk.traces.start_span(name="dogpark")
assert span.sampled is not bool(int(parent_sampling_decision))
@pytest.mark.parametrize("explicit_decision", [True, False])
def test_traces_sampler_doesnt_overwrite_explicitly_passed_sampling_decision(
sentry_init, explicit_decision
):
# make traces_sampler pick the opposite of the explicit decision, to prove
# that the explicit decision takes precedence
traces_sampler = mock.Mock(return_value=not explicit_decision)
sentry_init(traces_sampler=traces_sampler)
transaction = start_transaction(name="dogpark", sampled=explicit_decision)
assert transaction.sampled is explicit_decision
@pytest.mark.parametrize("parent_sampling_decision", [True, False])
def test_inherits_parent_sampling_decision_when_traces_sampler_undefined(
sentry_init, parent_sampling_decision
):
# make sure the parent sampling decision is the opposite of what
# traces_sample_rate would produce, to prove the inheritance takes
# precedence
sentry_init(traces_sample_rate=0.5)
mock_random_value = 0.25 if parent_sampling_decision is False else 0.75
with mock.patch.object(random, "random", return_value=mock_random_value):
transaction = start_transaction(
name="dogpark", parent_sampled=parent_sampling_decision
)
assert transaction.sampled is parent_sampling_decision
@pytest.mark.parametrize("parent_sampling_decision", ["1", "0"])
def test_inherits_parent_sampling_decision_when_traces_sampler_undefined_span_streaming(
sentry_init, parent_sampling_decision
):
sentry_init(
traces_sample_rate=0.5,
_experiments={"trace_lifecycle": "stream"},
)
sentry_sdk.traces.continue_trace(
{
"sentry-trace": f"0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-{parent_sampling_decision}"
}
)
# make sure the parent sampling decision is the opposite of what
# traces_sample_rate would produce, to prove the inheritance takes
# precedence
mock_random_value = 0.25 if parent_sampling_decision == "0" else 0.75
with mock.patch.object(random, "random", return_value=mock_random_value):
span = sentry_sdk.traces.start_span(name="dogpark")
assert span.sampled is bool(int(parent_sampling_decision))
@pytest.mark.parametrize("parent_sampling_decision", [True, False])
def test_passes_parent_sampling_decision_in_sampling_context(
sentry_init, parent_sampling_decision
):
sentry_init(traces_sample_rate=1.0)
sentry_trace_header = (
"12312012123120121231201212312012-1121201211212012-{sampled}".format(
sampled=int(parent_sampling_decision)
)
)
transaction = sentry_sdk.continue_trace(
{"sentry-trace": sentry_trace_header},
name="dogpark",
)
def mock_set_initial_sampling_decision(_, sampling_context):
assert "parent_sampled" in sampling_context
assert sampling_context["parent_sampled"] is parent_sampling_decision
with mock.patch(
"sentry_sdk.tracing.Transaction._set_initial_sampling_decision",
mock_set_initial_sampling_decision,
):
start_transaction(transaction=transaction)
def test_passes_custom_sampling_context_from_start_transaction_to_traces_sampler(
sentry_init,
DictionaryContaining, # noqa: N803
):
traces_sampler = mock.Mock()
sentry_init(traces_sampler=traces_sampler)
start_transaction(custom_sampling_context={"dogs": "yes", "cats": "maybe"})
traces_sampler.assert_any_call(
DictionaryContaining({"dogs": "yes", "cats": "maybe"})
)
def test_custom_sampling_context(sentry_init):
class MyClass: ...
my_class = MyClass()
def traces_sampler(sampling_context):
assert "class" in sampling_context
assert "string" in sampling_context
assert sampling_context["class"] == my_class
assert sampling_context["string"] == "my string"
return 1.0
sentry_init(
traces_sampler=traces_sampler,
_experiments={"trace_lifecycle": "stream"},
)
sentry_sdk.get_current_scope().set_custom_sampling_context(
{
"class": my_class,
"string": "my string",
}
)
with sentry_sdk.traces.start_span(name="span"):
...
def test_custom_sampling_context_update_to_context_value_persists(sentry_init):
def traces_sampler(sampling_context):
if sampling_context["span_context"]["attributes"]["first"] is True:
assert sampling_context["custom_value"] == 1
else:
assert sampling_context["custom_value"] == 2
return 1.0
sentry_init(
traces_sampler=traces_sampler,
_experiments={"trace_lifecycle": "stream"},
)
sentry_sdk.traces.new_trace()
sentry_sdk.get_current_scope().set_custom_sampling_context({"custom_value": 1})
with sentry_sdk.traces.start_span(name="span", attributes={"first": True}):
...
sentry_sdk.traces.new_trace()
sentry_sdk.get_current_scope().set_custom_sampling_context({"custom_value": 2})
with sentry_sdk.traces.start_span(name="span", attributes={"first": False}):
...
def test_sample_rate_affects_errors(sentry_init, capture_events):
sentry_init(sample_rate=0)
events = capture_events()
try:
1 / 0
except Exception:
capture_exception()
assert len(events) == 0
@pytest.mark.parametrize(
"traces_sampler_return_value",
[
"dogs are great", # wrong type
(0, 1), # wrong type
{"Maisey": "Charllie"}, # wrong type
[True, True], # wrong type
{0.2012}, # wrong type
float("NaN"), # wrong type
None, # wrong type
-1.121, # wrong value
1.231, # wrong value
],
)
def test_warns_and_sets_sampled_to_false_on_invalid_traces_sampler_return_value(
sentry_init,
traces_sampler_return_value,
StringContaining, # noqa: N803
):
sentry_init(traces_sampler=mock.Mock(return_value=traces_sampler_return_value))
with mock.patch.object(logger, "warning", mock.Mock()):
transaction = start_transaction(name="dogpark")
logger.warning.assert_any_call(StringContaining("Given sample rate is invalid"))
assert transaction.sampled is False
@pytest.mark.parametrize(
"traces_sampler_return_value",
[
"dogs are great", # wrong type
(0, 1), # wrong type
{"Maisey": "Charllie"}, # wrong type
[True, True], # wrong type
{0.2012}, # wrong type
float("NaN"), # wrong type
None, # wrong type
-1.121, # wrong value
1.231, # wrong value
],
)
def test_warns_and_sets_sampled_to_false_on_invalid_traces_sampler_return_value_span_streaming(
sentry_init,
traces_sampler_return_value,
StringContaining, # noqa: N803
):
sentry_init(
traces_sampler=mock.Mock(return_value=traces_sampler_return_value),
_experiments={"trace_lifecycle": "stream"},
)
with mock.patch.object(logger, "warning", mock.Mock()):
span = sentry_sdk.traces.start_span(name="dogpark")
logger.warning.assert_any_call(StringContaining("Given sample rate is invalid"))
assert span.sampled is False
@pytest.mark.parametrize(
"traces_sample_rate,sampled_output,expected_record_lost_event_calls",
[
(None, False, []),
(
0.0,
False,
[("sample_rate", "transaction", None, 1), ("sample_rate", "span", None, 1)],
),
(1.0, True, []),
],
)
def test_records_lost_event_only_if_traces_sample_rate_enabled(
sentry_init,
capture_record_lost_event_calls,
traces_sample_rate,
sampled_output,
expected_record_lost_event_calls,
):
sentry_init(traces_sample_rate=traces_sample_rate)
record_lost_event_calls = capture_record_lost_event_calls()
transaction = start_transaction(name="dogpark")
assert transaction.sampled is sampled_output
transaction.finish()
# Use Counter because order of calls does not matter
assert Counter(record_lost_event_calls) == Counter(expected_record_lost_event_calls)
@pytest.mark.parametrize(
"traces_sampler,sampled_output,expected_record_lost_event_calls",
[
(None, False, []),
(
lambda _x: 0.0,
False,
[("sample_rate", "transaction", None, 1), ("sample_rate", "span", None, 1)],
),
(lambda _x: 1.0, True, []),
],
)
def test_records_lost_event_only_if_traces_sampler_enabled(
sentry_init,
capture_record_lost_event_calls,
traces_sampler,
sampled_output,
expected_record_lost_event_calls,
):
sentry_init(traces_sampler=traces_sampler)
record_lost_event_calls = capture_record_lost_event_calls()
transaction = start_transaction(name="dogpark")
assert transaction.sampled is sampled_output
transaction.finish()
# Use Counter because order of calls does not matter
assert Counter(record_lost_event_calls) == Counter(expected_record_lost_event_calls)
def test_unsampled_spans_produce_client_report_if_traces_sample_rate_defined(
sentry_init, capture_items, capture_record_lost_event_calls
):
sentry_init(
traces_sample_rate=0.0,
_experiments={"trace_lifecycle": "stream"},
)
items = capture_items("span")
record_lost_event_calls = capture_record_lost_event_calls()
with sentry_sdk.traces.start_span(name="segment"):
with sentry_sdk.traces.start_span(name="child1"):
pass
with sentry_sdk.traces.start_span(name="child2"):
pass
sentry_sdk.get_client().flush()
spans = [item.payload for item in items]
assert not spans
assert record_lost_event_calls == [
("sample_rate", "span", None, 1),
("sample_rate", "span", None, 1),
("sample_rate", "span", None, 1),
]
def test_unsampled_spans_produce_client_report_if_traces_sampler_defined(
sentry_init, capture_items, capture_record_lost_event_calls
):
sentry_init(
traces_sampler=lambda _: 0.0,
_experiments={"trace_lifecycle": "stream"},
)
items = capture_items("span")
record_lost_event_calls = capture_record_lost_event_calls()
with sentry_sdk.traces.start_span(name="segment"):
with sentry_sdk.traces.start_span(name="child1"):
pass
with sentry_sdk.traces.start_span(name="child2"):
pass
sentry_sdk.get_client().flush()
spans = [item.payload for item in items]
assert not spans
assert record_lost_event_calls == [
("sample_rate", "span", None, 1),
("sample_rate", "span", None, 1),
("sample_rate", "span", None, 1),
]
def test_no_client_reports_if_tracing_is_off(
sentry_init, capture_items, capture_record_lost_event_calls
):
sentry_init(
traces_sample_rate=None,
_experiments={"trace_lifecycle": "stream"},
)
items = capture_items("span")
record_lost_event_calls = capture_record_lost_event_calls()
with sentry_sdk.traces.start_span(name="segment"):
with sentry_sdk.traces.start_span(name="child1"):
pass
with sentry_sdk.traces.start_span(name="child2"):
pass
sentry_sdk.get_client().flush()
spans = [item.payload for item in items]
assert not spans
assert not record_lost_event_calls