Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions tests/tracing/test_sample_rand.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,36 @@
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_deterministic_sampled_span_streaming(
sentry_init, capture_items, 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 segment's
baggage.
"""
sentry_init(
traces_sample_rate=sample_rate,
_experiments={"trace_lifecycle": "stream"},
)
items = capture_items("span")

with mock.patch(
"sentry_sdk.tracing_utils.Random.randrange",
return_value=int(sample_rand * 1000000),
):
with sentry_sdk.traces.start_span(name="span") as span:
assert (
span._get_baggage().sentry_items["sample_rand"] == f"{sample_rand:.6f}" # noqa: E231
)

# Span captured if sample_rand < sample_rate, indicating that
# sample_rand is used to make the sampling decision.
assert len(items) == 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(
Expand All @@ -54,3 +84,31 @@
# 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_span_streaming(
sentry_init, capture_items, sample_rate, sample_rand
):
"""
Test that the segment uses the sample_rand value from the incoming baggage.
"""
sentry_init(
traces_sample_rate=sample_rate,
_experiments={"trace_lifecycle": "stream"},
)
items = capture_items()

sentry_sdk.traces.continue_trace(
{"baggage": "sentry-sample_rand={sample_rand:.6f}"}
)

Check warning on line 105 in tests/tracing/test_sample_rand.py

View check run for this annotation

@sentry/warden / warden: code-review

Missing f-string prefix causes baggage `sample_rand` to never be interpolated in test

The baggage string `"sentry-sample_rand={sample_rand:.6f}"` is a plain string literal, not an f-string, so `{sample_rand:.6f}` is passed literally to `continue_trace` instead of the actual `sample_rand` value. The subsequent assertion compares the parsed baggage value against the formatted float, so this test will always fail. Impact is limited to test correctness rather than production behavior.
Comment thread
sentrivana marked this conversation as resolved.

with sentry_sdk.traces.start_span(name="span") as span:
assert (
span._get_baggage().sentry_items["sample_rand"] == f"{sample_rand:.6f}" # noqa: E231
)

# Span captured if sample_rand < sample_rate, indicating that
# sample_rand is used to make the sampling decision.
assert len(items) == int(sample_rand < sample_rate)
3 changes: 0 additions & 3 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading