Skip to content

Commit d554a6c

Browse files
authored
Backpressure: only downsample a max of 10 times (#2347)
1 parent 0390635 commit d554a6c

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

sentry_sdk/monitor.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
from typing import Optional
1111

1212

13+
MAX_DOWNSAMPLE_FACTOR = 10
14+
15+
1316
class Monitor(object):
1417
"""
1518
Performs health checks in a separate thread once every interval seconds
@@ -25,7 +28,7 @@ def __init__(self, transport, interval=10):
2528
self.interval = interval # type: float
2629

2730
self._healthy = True
28-
self._downsample_factor = 1 # type: int
31+
self._downsample_factor = 0 # type: int
2932

3033
self._thread = None # type: Optional[Thread]
3134
self._thread_lock = Lock()
@@ -64,13 +67,14 @@ def run(self):
6467
def set_downsample_factor(self):
6568
# type: () -> None
6669
if self._healthy:
67-
if self._downsample_factor > 1:
70+
if self._downsample_factor > 0:
6871
logger.debug(
6972
"[Monitor] health check positive, reverting to normal sampling"
7073
)
71-
self._downsample_factor = 1
74+
self._downsample_factor = 0
7275
else:
73-
self._downsample_factor *= 2
76+
if self.downsample_factor < MAX_DOWNSAMPLE_FACTOR:
77+
self._downsample_factor += 1
7478
logger.debug(
7579
"[Monitor] health check negative, downsampling with a factor of %d",
7680
self._downsample_factor,

sentry_sdk/tracing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ def finish(self, hub=None, end_timestamp=None):
595595
# exclusively based on sample rate but also traces sampler, but
596596
# we handle this the same here.
597597
if client.transport and has_tracing_enabled(client.options):
598-
if client.monitor and client.monitor.downsample_factor > 1:
598+
if client.monitor and client.monitor.downsample_factor > 0:
599599
reason = "backpressure"
600600
else:
601601
reason = "sample_rate"
@@ -758,7 +758,7 @@ def _set_initial_sampling_decision(self, sampling_context):
758758
self.sample_rate = float(sample_rate)
759759

760760
if client.monitor:
761-
self.sample_rate /= client.monitor.downsample_factor
761+
self.sample_rate /= 2**client.monitor.downsample_factor
762762

763763
# if the function returned 0 (or false), or if `traces_sample_rate` is
764764
# 0, it's a sign the transaction should be dropped

tests/test_monitor.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def test_monitor_if_enabled(sentry_init):
3737
assert monitor._thread is None
3838

3939
assert monitor.is_healthy() is True
40-
assert monitor.downsample_factor == 1
40+
assert monitor.downsample_factor == 0
4141
assert monitor._thread is not None
4242
assert monitor._thread.name == "sentry.monitor"
4343

@@ -49,11 +49,11 @@ def test_monitor_unhealthy(sentry_init):
4949
monitor.interval = 0.1
5050

5151
assert monitor.is_healthy() is True
52-
monitor.run()
53-
assert monitor.is_healthy() is False
54-
assert monitor.downsample_factor == 2
55-
monitor.run()
56-
assert monitor.downsample_factor == 4
52+
53+
for i in range(15):
54+
monitor.run()
55+
assert monitor.is_healthy() is False
56+
assert monitor.downsample_factor == (i + 1 if i < 10 else 10)
5757

5858

5959
def test_transaction_uses_downsampled_rate(
@@ -75,7 +75,7 @@ def test_transaction_uses_downsampled_rate(
7575
assert monitor.is_healthy() is True
7676
monitor.run()
7777
assert monitor.is_healthy() is False
78-
assert monitor.downsample_factor == 2
78+
assert monitor.downsample_factor == 1
7979

8080
with start_transaction(name="foobar") as transaction:
8181
assert transaction.sampled is False

0 commit comments

Comments
 (0)