Skip to content

Commit 4eba6bb

Browse files
authored
test: Add sample_rand span streaming tests (#6781)
### Description Add streaming counterparts of the tests in `test_sample_rand.py`. #### Issues Part of #5395 #### Reminders - Please add tests to validate your changes, and lint your code using `uv run ruff`. - Add GH Issue ID _&_ Linear ID (if applicable) - PR title should use [conventional commit](https://develop.sentry.dev/engineering-practices/commit-messages/#type) style (`feat:`, `fix:`, `ref:`, `meta:`) - For external contributors: [CONTRIBUTING.md](https://github.com/getsentry/sentry-python/blob/master/CONTRIBUTING.md), [Sentry SDK development docs](https://develop.sentry.dev/sdk/), [Discord community](https://discord.gg/Ww9hbqr)
1 parent 8027343 commit 4eba6bb

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

tests/tracing/test_sample_rand.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,38 @@ def test_deterministic_sampled(sentry_init, capture_events, sample_rate, sample_
3232
assert len(events) == int(sample_rand < sample_rate)
3333

3434

35+
@pytest.mark.parametrize("sample_rand", (0.0, 0.25, 0.5, 0.75))
36+
@pytest.mark.parametrize("sample_rate", (0.0, 0.25, 0.5, 0.75, 1.0))
37+
def test_deterministic_sampled_span_streaming(
38+
sentry_init, capture_items, sample_rate, sample_rand
39+
):
40+
"""
41+
Test that sample_rand is generated on new traces, that it is used to
42+
make the sampling decision, and that it is included in the segment's
43+
baggage.
44+
"""
45+
sentry_init(
46+
traces_sample_rate=sample_rate,
47+
trace_lifecycle="stream",
48+
)
49+
items = capture_items("span")
50+
51+
with mock.patch(
52+
"sentry_sdk.tracing_utils.Random.randrange",
53+
return_value=int(sample_rand * 1000000),
54+
):
55+
with sentry_sdk.traces.start_span(name="span") as span:
56+
assert (
57+
span._get_baggage().sentry_items["sample_rand"] == f"{sample_rand:.6f}" # noqa: E231
58+
)
59+
60+
sentry_sdk.flush()
61+
62+
# Span captured if sample_rand < sample_rate, indicating that
63+
# sample_rand is used to make the sampling decision.
64+
assert len(items) == int(sample_rand < sample_rate)
65+
66+
3567
@pytest.mark.parametrize("sample_rand", (0.0, 0.25, 0.5, 0.75))
3668
@pytest.mark.parametrize("sample_rate", (0.0, 0.25, 0.5, 0.75, 1.0))
3769
def test_transaction_uses_incoming_sample_rand(
@@ -54,3 +86,36 @@ def test_transaction_uses_incoming_sample_rand(
5486
# Transaction event captured if sample_rand < sample_rate, indicating that
5587
# sample_rand is used to make the sampling decision.
5688
assert len(events) == int(sample_rand < sample_rate)
89+
90+
91+
@pytest.mark.parametrize("sample_rand", (0.0, 0.25, 0.5, 0.75))
92+
@pytest.mark.parametrize("sample_rate", (0.0, 0.25, 0.5, 0.75, 1.0))
93+
def test_segment_uses_incoming_sample_rand_span_streaming(
94+
sentry_init, capture_items, sample_rate, sample_rand
95+
):
96+
"""
97+
Test that the segment uses the sample_rand value from the incoming baggage.
98+
"""
99+
sentry_init(
100+
traces_sample_rate=sample_rate,
101+
trace_lifecycle="stream",
102+
)
103+
items = capture_items("span")
104+
105+
sentry_sdk.traces.continue_trace(
106+
{
107+
"sentry-trace": "0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331",
108+
"baggage": f"sentry-sample_rand={sample_rand:.6f}",
109+
}
110+
)
111+
112+
with sentry_sdk.traces.start_span(name="span") as span:
113+
assert (
114+
span._get_baggage().sentry_items["sample_rand"] == f"{sample_rand:.6f}" # noqa: E231
115+
)
116+
117+
sentry_sdk.flush()
118+
119+
# Span captured if sample_rand < sample_rate, indicating that
120+
# sample_rand is used to make the sampling decision.
121+
assert len(items) == int(sample_rand < sample_rate)

0 commit comments

Comments
 (0)