- Notifications
You must be signed in to change notification settings - Fork 568
Backpressure prototype #2189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Backpressure prototype #2189
Changes from all commits
Commits
Show all changes
12 commits Select commit Hold shift + click to select a range
1ab168f Initital Monitor impl
sl0thentr0py 301a60b Add downsample_factor to monitor
sl0thentr0py 12504a2 Use downsample factor in sampling decision
sl0thentr0py 795d461 Add queue full check, pass in transport to monitor
sl0thentr0py 259da75 Make interval 10
sl0thentr0py 4735cdb Forgot to start thread
sl0thentr0py 6f5f9da Expose new enable_backpressure_handling config
sl0thentr0py 03a46fd Move to experiments
sl0thentr0py 7c82ec1 writing tests much fun
sl0thentr0py 0e8a384 Fix flaky tests with explicit runs
sl0thentr0py a8f4e92 Make downsample_factor property
sl0thentr0py 1f27ad4 Move health logic to transport
sl0thentr0py File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import os | ||
| import time | ||
| from threading import Thread, Lock | ||
| | ||
| import sentry_sdk | ||
| from sentry_sdk.utils import logger | ||
| from sentry_sdk._types import TYPE_CHECKING | ||
| | ||
| if TYPE_CHECKING: | ||
| from typing import Optional | ||
| | ||
| | ||
| class Monitor(object): | ||
| """ | ||
| Performs health checks in a separate thread once every interval seconds | ||
| and updates the internal state. Other parts of the SDK only read this state | ||
| and act accordingly. | ||
| """ | ||
| | ||
| name = "sentry.monitor" | ||
| | ||
| def __init__(self, transport, interval=10): | ||
| # type: (sentry_sdk.transport.Transport, float) -> None | ||
| self.transport = transport # type: sentry_sdk.transport.Transport | ||
| self.interval = interval # type: float | ||
| | ||
| self._healthy = True | ||
| self._downsample_factor = 1 # type: int | ||
| | ||
| self._thread = None # type: Optional[Thread] | ||
| self._thread_lock = Lock() | ||
| self._thread_for_pid = None # type: Optional[int] | ||
| self._running = True | ||
| | ||
| def _ensure_running(self): | ||
| # type: () -> None | ||
| if self._thread_for_pid == os.getpid() and self._thread is not None: | ||
| return None | ||
| | ||
| with self._thread_lock: | ||
| if self._thread_for_pid == os.getpid() and self._thread is not None: | ||
| return None | ||
| | ||
| def _thread(): | ||
| # type: (...) -> None | ||
| while self._running: | ||
| time.sleep(self.interval) | ||
| if self._running: | ||
| self.run() | ||
| | ||
| thread = Thread(name=self.name, target=_thread) | ||
| thread.daemon = True | ||
| thread.start() | ||
| self._thread = thread | ||
| self._thread_for_pid = os.getpid() | ||
| | ||
| return None | ||
| | ||
| def run(self): | ||
| # type: () -> None | ||
| self.check_health() | ||
| self.set_downsample_factor() | ||
| | ||
| def set_downsample_factor(self): | ||
| # type: () -> None | ||
| if self._healthy: | ||
| if self._downsample_factor > 1: | ||
| logger.debug( | ||
| "[Monitor] health check positive, reverting to normal sampling" | ||
| ) | ||
| self._downsample_factor = 1 | ||
| else: | ||
| self._downsample_factor *= 2 | ||
| logger.debug( | ||
| "[Monitor] health check negative, downsampling with a factor of %d", | ||
| self._downsample_factor, | ||
| ) | ||
| | ||
| def check_health(self): | ||
| # type: () -> None | ||
| """ | ||
| Perform the actual health checks, | ||
| currently only checks if the transport is rate-limited. | ||
| TODO: augment in the future with more checks. | ||
| """ | ||
| self._healthy = self.transport.is_healthy() | ||
| | ||
| def is_healthy(self): | ||
| # type: () -> bool | ||
| self._ensure_running() | ||
| return self._healthy | ||
| | ||
| @property | ||
| def downsample_factor(self): | ||
| # type: () -> int | ||
| self._ensure_running() | ||
| return self._downsample_factor | ||
| | ||
| def kill(self): | ||
| # type: () -> None | ||
| self._running = False | ||
| | ||
| def __del__(self): | ||
| # type: () -> None | ||
| self.kill() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import random | ||
| | ||
| from sentry_sdk import Hub, start_transaction | ||
| from sentry_sdk.transport import Transport | ||
| | ||
| | ||
| class HealthyTestTransport(Transport): | ||
| def _send_event(self, event): | ||
| pass | ||
| | ||
| def _send_envelope(self, envelope): | ||
| pass | ||
| | ||
| def is_healthy(self): | ||
| return True | ||
| | ||
| | ||
| class UnhealthyTestTransport(HealthyTestTransport): | ||
| def is_healthy(self): | ||
| return False | ||
| | ||
| | ||
| def test_no_monitor_if_disabled(sentry_init): | ||
| sentry_init(transport=HealthyTestTransport()) | ||
| assert Hub.current.client.monitor is None | ||
| | ||
| | ||
| def test_monitor_if_enabled(sentry_init): | ||
| sentry_init( | ||
| transport=HealthyTestTransport(), | ||
| _experiments={"enable_backpressure_handling": True}, | ||
| ) | ||
| | ||
| monitor = Hub.current.client.monitor | ||
| assert monitor is not None | ||
| assert monitor._thread is None | ||
| | ||
| assert monitor.is_healthy() is True | ||
| assert monitor.downsample_factor == 1 | ||
| assert monitor._thread is not None | ||
| assert monitor._thread.name == "sentry.monitor" | ||
| | ||
| | ||
| def test_monitor_unhealthy(sentry_init): | ||
| sentry_init( | ||
| transport=UnhealthyTestTransport(), | ||
| _experiments={"enable_backpressure_handling": True}, | ||
| ) | ||
| | ||
| monitor = Hub.current.client.monitor | ||
| monitor.interval = 0.1 | ||
| | ||
| assert monitor.is_healthy() is True | ||
| monitor.run() | ||
| assert monitor.is_healthy() is False | ||
| assert monitor.downsample_factor == 2 | ||
| monitor.run() | ||
| assert monitor.downsample_factor == 4 | ||
| | ||
| | ||
| def test_transaction_uses_downsampled_rate( | ||
| sentry_init, capture_client_reports, monkeypatch | ||
| ): | ||
| sentry_init( | ||
| traces_sample_rate=1.0, | ||
| transport=UnhealthyTestTransport(), | ||
| _experiments={"enable_backpressure_handling": True}, | ||
| ) | ||
| | ||
| reports = capture_client_reports() | ||
| | ||
| monitor = Hub.current.client.monitor | ||
| monitor.interval = 0.1 | ||
| | ||
| # make sure rng doesn't sample | ||
| monkeypatch.setattr(random, "random", lambda: 0.9) | ||
| | ||
| assert monitor.is_healthy() is True | ||
| monitor.run() | ||
| assert monitor.is_healthy() is False | ||
| assert monitor.downsample_factor == 2 | ||
| | ||
| with start_transaction(name="foobar") as transaction: | ||
| assert transaction.sampled is False | ||
| assert transaction.sample_rate == 0.5 | ||
| | ||
| assert reports == [("backpressure", "transaction")] |
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.