diff --git a/tests/tracing/test_sample_rand.py b/tests/tracing/test_sample_rand.py index 4a74950b30..60c2cd91b6 100644 --- a/tests/tracing/test_sample_rand.py +++ b/tests/tracing/test_sample_rand.py @@ -32,6 +32,36 @@ def test_deterministic_sampled(sentry_init, capture_events, sample_rate, sample_ 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( @@ -54,3 +84,31 @@ def test_transaction_uses_incoming_sample_rand( # 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": f"sentry-sample_rand={sample_rand:.6f}"} + ) + + 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) diff --git a/uv.lock b/uv.lock index 855c128ef8..fb1818bba8 100644 --- a/uv.lock +++ b/uv.lock @@ -6,9 +6,6 @@ resolution-markers = [ "python_full_version < '3.15'", ] -[options] -prerelease-mode = "allow" - [manifest] [manifest.dependency-groups]