From 1f8f29ed9567bddf538bd3005e7a0b65040fc1cc Mon Sep 17 00:00:00 2001 From: William Mak Date: Thu, 15 Oct 2026 09:59:54 -0400 Subject: [PATCH] fix(events): Handle 408s from snuba - When snuba returns a 408, bubble that up to the user instead of 500ing - Fixes SENTRY-41H2 --- src/sentry/api/utils.py | 4 ++++ tests/sentry/api/test_utils.py | 28 ++++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/sentry/api/utils.py b/src/sentry/api/utils.py index 260de7bae236..d039cc9fa28a 100644 --- a/src/sentry/api/utils.py +++ b/src/sentry/api/utils.py @@ -434,6 +434,10 @@ def handle_query_errors() -> Iterator[None]: except SnubaRPCError as error: message = "Internal error. Please try again." arg = error.args[0] if len(error.args) > 0 else None + if getattr(arg, "code", None) == 408: + sentry_sdk.set_tag("query.error_reason", "Timeout") + sentry_sdk.set_attribute("query.error_reason", "Timeout") + raise ParseError(detail=TIMEOUT_RPC_ERROR_MESSAGE) if isinstance(arg, TimeoutError): sentry_sdk.set_tag("query.error_reason", "Timeout") sentry_sdk.set_attribute("query.error_reason", "Timeout") diff --git a/tests/sentry/api/test_utils.py b/tests/sentry/api/test_utils.py index 1223cc02603b..b2c7d1761561 100644 --- a/tests/sentry/api/test_utils.py +++ b/tests/sentry/api/test_utils.py @@ -5,7 +5,7 @@ import pytest from django.db import OperationalError from django.utils import timezone -from rest_framework.exceptions import APIException, Throttled, ValidationError +from rest_framework.exceptions import APIException, ParseError, Throttled, ValidationError from sentry_sdk import Scope from snuba_sdk.column import InvalidColumnError @@ -39,7 +39,7 @@ SnubaError, UnqualifiedQueryError, ) -from sentry.utils.snuba_rpc import SnubaRPCTooManySimultaneous, SnubaRPCUnavailable +from sentry.utils.snuba_rpc import SnubaRPCError, SnubaRPCTooManySimultaneous, SnubaRPCUnavailable class GetDateRangeFromParamsTest(unittest.TestCase): @@ -277,6 +277,30 @@ def test_handle_snuba_rpc_unavailable( mock_warning.assert_called_once_with("snuba_rpc.unavailable") mock_capture_exception.assert_not_called() + @patch("sentry.api.utils.sentry_sdk.capture_exception") + @patch("sentry.api.utils.sentry_sdk.set_attribute") + @patch("sentry.api.utils.sentry_sdk.set_tag") + def test_handle_snuba_rpc_query_timeout( + self, + mock_set_tag: MagicMock, + mock_set_attribute: MagicMock, + mock_capture_exception: MagicMock, + ) -> None: + from sentry_protos.snuba.v1.error_pb2 import Error as ErrorProto + + error_proto = ErrorProto( + code=408, + message="Query timed out, this violates the EAP SLO", + ) + with pytest.raises(ParseError) as raised: + with handle_query_errors(): + raise SnubaRPCError(error_proto) + + assert raised.value.status_code == 400 + mock_set_tag.assert_called_once_with("query.error_reason", "Timeout") + mock_set_attribute.assert_called_once_with("query.error_reason", "Timeout") + mock_capture_exception.assert_not_called() + class ClampDateRangeTest(unittest.TestCase): def test_no_clamp_if_range_under_max(self) -> None: