-
Notifications
You must be signed in to change notification settings - Fork 536
/
Copy pathtest_sample_rand.py
89 lines (72 loc) · 3.12 KB
/
test_sample_rand.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
import decimal
from decimal import Inexact, FloatOperation
from unittest import mock
import pytest
import sentry_sdk
from sentry_sdk.tracing_utils import Baggage
@pytest.mark.parametrize("sample_rand", (0.0, 0.25, 0.5, 0.75))
@pytest.mark.parametrize("sample_rate", (0.0, 0.25, 0.5, 0.75, 1.0))
def test_deterministic_sampled(sentry_init, capture_events, sample_rate, sample_rand):
"""
Test that sample_rand is generated on new traces, that it is used to
make the sampling decision, and that it is included in the transaction's
baggage.
"""
sentry_init(traces_sample_rate=sample_rate)
events = capture_events()
with mock.patch(
"sentry_sdk.tracing_utils.Random.uniform", return_value=sample_rand
):
with sentry_sdk.start_transaction() as transaction:
assert (
transaction.get_baggage().sentry_items["sample_rand"]
== f"{sample_rand:.6f}" # noqa: E231
)
# Transaction event captured if sample_rand < sample_rate, indicating that
# sample_rand is used to make the sampling decision.
assert len(events) == int(sample_rand < sample_rate)
@pytest.mark.parametrize("sample_rand", (0.0, 0.25, 0.5, 0.75))
@pytest.mark.parametrize("sample_rate", (0.0, 0.25, 0.5, 0.75, 1.0))
def test_transaction_uses_incoming_sample_rand(
sentry_init, capture_events, sample_rate, sample_rand
):
"""
Test that the transaction uses the sample_rand value from the incoming baggage.
"""
baggage = Baggage(sentry_items={"sample_rand": f"{sample_rand:.6f}"}) # noqa: E231
sentry_init(traces_sample_rate=sample_rate)
events = capture_events()
with sentry_sdk.start_transaction(baggage=baggage) as transaction:
assert (
transaction.get_baggage().sentry_items["sample_rand"]
== f"{sample_rand:.6f}" # noqa: E231
)
# Transaction event captured if sample_rand < sample_rate, indicating that
# sample_rand is used to make the sampling decision.
assert len(events) == int(sample_rand < sample_rate)
def test_decimal_context(sentry_init, capture_events):
"""
Ensure that having a user altered decimal context with a precision below 6
does not cause an InvalidOperation exception.
"""
sentry_init(traces_sample_rate=1.0)
events = capture_events()
old_prec = decimal.getcontext().prec
old_inexact = decimal.getcontext().traps[Inexact]
old_float_operation = decimal.getcontext().traps[FloatOperation]
decimal.getcontext().prec = 2
decimal.getcontext().traps[Inexact] = True
decimal.getcontext().traps[FloatOperation] = True
try:
with mock.patch(
"sentry_sdk.tracing_utils.Random.uniform", return_value=0.123456789
):
with sentry_sdk.start_transaction() as transaction:
assert (
transaction.get_baggage().sentry_items["sample_rand"] == "0.123456"
)
finally:
decimal.getcontext().prec = old_prec
decimal.getcontext().traps[Inexact] = old_inexact
decimal.getcontext().traps[FloatOperation] = old_float_operation
assert len(events) == 1