From 9be0c64cb403fbd39340115c344f2ab64f8e2d68 Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Fri, 24 Jul 2026 13:42:27 +0200 Subject: [PATCH] test: Add streaming monitor test --- tests/test_monitor.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/test_monitor.py b/tests/test_monitor.py index 131440d910..96902abca2 100644 --- a/tests/test_monitor.py +++ b/tests/test_monitor.py @@ -90,6 +90,38 @@ def test_transaction_uses_downsampled_rate( ) +def test_segment_uses_downsampled_rate( + sentry_init, capture_record_lost_event_calls, monkeypatch +): + sentry_init( + traces_sample_rate=1.0, + trace_lifecycle="stream", + transport=UnhealthyTestTransport(), + ) + + record_lost_event_calls = capture_record_lost_event_calls() + + monitor = sentry_sdk.get_client().monitor + monitor.interval = 0.1 + + assert monitor.is_healthy() is True + monitor.run() + assert monitor.is_healthy() is False + assert monitor.downsample_factor == 1 + + # make sure we don't sample the segment + with mock.patch("sentry_sdk.tracing_utils.Random.randrange", return_value=750000): + with sentry_sdk.traces.start_span(name="foobar") as segment: + assert segment.sampled is False + assert segment._sample_rate == 0.5 + + assert Counter(record_lost_event_calls) == Counter( + [ + ("backpressure", "span", None, 1), + ] + ) + + def test_monitor_no_thread_on_shutdown_no_errors(sentry_init): sentry_init(transport=HealthyTestTransport())