forked from getsentry/sentry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sample_rand.py
More file actions
56 lines (45 loc) · 2 KB
/
Copy pathtest_sample_rand.py
File metadata and controls
56 lines (45 loc) · 2 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
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.randrange",
return_value=int(sample_rand * 1000000),
):
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)