diff --git a/sentry_sdk/transport.py b/sentry_sdk/transport.py index f459c87c01..8b41c836d1 100644 --- a/sentry_sdk/transport.py +++ b/sentry_sdk/transport.py @@ -211,6 +211,35 @@ def _parse_rate_limits( continue +class _RateLimits: + """Deadlines until which each data category is rate limited. + + The ``None`` category holds the limit for all categories. ``update`` is the only + write operation, and it keeps the later of the stored and the new deadline, so a + shorter rate limit never replaces a longer one, as the rate limiting spec requires: + https://develop.sentry.dev/sdk/foundations/transport/rate-limiting/#stage-2-determine-rate-limits + """ + + __slots__ = ("__deadlines",) + + def __init__(self) -> None: + self.__deadlines: "Dict[Optional[str], datetime]" = {} + + def update(self, category: "Optional[str]", until: datetime) -> None: + """Extend the limit for ``category`` to ``until``, unless it already ends later.""" + current = self.__deadlines.get(category) + if current is None or until > current: + self.__deadlines[category] = until + + def is_active(self, category: "Optional[str]") -> bool: + until = self.__deadlines.get(category) + return until is not None and until > datetime.now(timezone.utc) + + def any_active(self) -> bool: + now = datetime.now(timezone.utc) + return any(until > now for until in self.__deadlines.values()) + + class HttpTransportCore(Transport): """Shared base class for sync and async transports.""" @@ -224,7 +253,7 @@ def __init__(self: "Self", options: "Dict[str, Any]") -> None: self.options: "Dict[str, Any]" = options self._worker = self._create_worker(options) self._auth = self.parsed_dsn.to_auth("sentry.python/%s" % VERSION) - self._disabled_until: "Dict[Optional[EventDataCategory], datetime]" = {} + self._disabled_until = _RateLimits() # We only use this Retry() class for the `get_retry_after` method it exposes self._retry = urllib3.util.Retry() self._discarded_events: "DefaultDict[Tuple[EventDataCategory, str], int]" = ( @@ -343,7 +372,8 @@ def _update_rate_limits( header = self._get_header_value(response, "x-sentry-rate-limits") if header: logger.warning("Rate-limited via x-sentry-rate-limits") - self._disabled_until.update(_parse_rate_limits(header)) + for category, until in _parse_rate_limits(header): + self._disabled_until.update(category, until) # old sentries only communicate global rate limit hits via the # retry-after header on 429. This header can also be emitted on new @@ -356,8 +386,8 @@ def _update_rate_limits( if retry_after_value is not None else None ) or 60 - self._disabled_until[None] = datetime.now(timezone.utc) + timedelta( - seconds=retry_after + self._disabled_until.update( + None, datetime.now(timezone.utc) + timedelta(seconds=retry_after) ) def _handle_request_error( @@ -463,16 +493,11 @@ def _fetch_pending_client_report( ) def _check_disabled(self, category: str) -> bool: - def _disabled(bucket: "Any") -> bool: - ts = self._disabled_until.get(bucket) - return ts is not None and ts > datetime.now(timezone.utc) - - return _disabled(category) or _disabled(None) + rate_limits = self._disabled_until + return rate_limits.is_active(category) or rate_limits.is_active(None) def _is_rate_limited(self: "Self") -> bool: - return any( - ts > datetime.now(timezone.utc) for ts in self._disabled_until.values() - ) + return self._disabled_until.any_active() def _is_worker_full(self: "Self") -> bool: return self._worker.full() diff --git a/tests/test_transport.py b/tests/test_transport.py index b955222061..133001229e 100644 --- a/tests/test_transport.py +++ b/tests/test_transport.py @@ -44,6 +44,7 @@ AsyncHttpTransport, HttpTransport, _parse_rate_limits, + _RateLimits, ) server = None @@ -473,6 +474,22 @@ def test_parse_rate_limits(input, expected): assert dict(_parse_rate_limits(input, now=NOW)) == expected +def test_rate_limits_keep_longest_deadline(): + rate_limits = _RateLimits() + now = datetime.now(timezone.utc) + + rate_limits.update("error", now - timedelta(seconds=1)) + assert not rate_limits.is_active("error") + + # A longer deadline replaces a shorter one. + rate_limits.update("error", now + timedelta(hours=1)) + assert rate_limits.is_active("error") + + # A shorter deadline does not replace a longer one. + rate_limits.update("error", now - timedelta(seconds=1)) + assert rate_limits.is_active("error") + + def test_envelope_too_large_response(capturing_server, make_client): client = make_client() @@ -511,7 +528,7 @@ def test_simple_rate_limits(capturing_server, make_client): assert capturing_server.captured[0].path == "/api/132/envelope/" capturing_server.clear_captured() - assert set(client.transport._disabled_until) == set([None]) + assert client.transport._disabled_until.is_active(None) client.capture_event({"type": "transaction"}) client.capture_event({"type": "event"}) @@ -547,7 +564,9 @@ def record_lost_event(reason, data_category=None, item=None): assert capturing_server.captured[0].path == "/api/132/envelope/" capturing_server.clear_captured() - assert set(client.transport._disabled_until) == set(["transaction"]) + rate_limits = client.transport._disabled_until + assert rate_limits.is_active("transaction") + assert not rate_limits.is_active(None) client.capture_event({"type": "transaction"}) client.capture_event({"type": "transaction"}) @@ -600,7 +619,10 @@ def intercepting_fetch(*args, **kwargs): assert capturing_server.captured[0].path == "/api/132/envelope/" capturing_server.clear_captured() - assert set(client.transport._disabled_until) == set(["attachment", "transaction"]) + rate_limits = client.transport._disabled_until + assert rate_limits.is_active("attachment") + assert rate_limits.is_active("transaction") + assert not rate_limits.is_active(None) client.capture_event({"type": "transaction"}) client.capture_event({"type": "transaction"}) @@ -694,7 +716,7 @@ def test_complex_limits_without_data_category( assert capturing_server.captured[0].path == "/api/132/envelope/" capturing_server.clear_captured() - assert set(client.transport._disabled_until) == set([None]) + assert client.transport._disabled_until.is_active(None) client.capture_event({"type": "transaction"}) client.capture_event({"type": "transaction"}) @@ -826,7 +848,9 @@ def test_log_item_limits(capturing_server, response_code, item, make_client): assert capturing_server.captured[0].path == "/api/132/envelope/" capturing_server.clear_captured() - assert set(client.transport._disabled_until) == {"log_item"} + rate_limits = client.transport._disabled_until + assert rate_limits.is_active("log_item") + assert not rate_limits.is_active(None) client.transport.capture_envelope(envelope) client.capture_event({"type": "transaction"})