Skip to content

Commit 3ad9fa9

Browse files
feat(aiohttp): Add failed_request_status_codes
`failed_request_status_codes` allows users to specify the status codes, whose corresponding `HTTPException` types, should be reported to Sentry. By default, these include 5xx statuses, which is a change from the previous default behavior, where no `HTTPException`s would be reported to Sentry. Closes #3535
1 parent 64e2977 commit 3ad9fa9

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

sentry_sdk/integrations/aiohttp.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
from aiohttp.web_request import Request
4848
from aiohttp.web_urldispatcher import UrlMappingMatchInfo
4949
from aiohttp import TraceRequestStartParams, TraceRequestEndParams
50+
51+
from collections.abc import Set
5052
from types import SimpleNamespace
5153
from typing import Any
5254
from typing import Optional
@@ -58,20 +60,27 @@
5860

5961

6062
TRANSACTION_STYLE_VALUES = ("handler_name", "method_and_path_pattern")
63+
DEFAULT_FAILED_REQUEST_STATUS_CODES = frozenset(range(500, 600))
6164

6265

6366
class AioHttpIntegration(Integration):
6467
identifier = "aiohttp"
6568
origin = f"auto.http.{identifier}"
6669

67-
def __init__(self, transaction_style="handler_name"):
68-
# type: (str) -> None
70+
def __init__(
71+
self,
72+
transaction_style="handler_name", # type: str
73+
*,
74+
failed_request_status_codes=DEFAULT_FAILED_REQUEST_STATUS_CODES, # type: Set[int]
75+
):
76+
# type: (...) -> None
6977
if transaction_style not in TRANSACTION_STYLE_VALUES:
7078
raise ValueError(
7179
"Invalid value for transaction_style: %s (must be in %s)"
7280
% (transaction_style, TRANSACTION_STYLE_VALUES)
7381
)
7482
self.transaction_style = transaction_style
83+
self._failed_request_status_codes = failed_request_status_codes
7584

7685
@staticmethod
7786
def setup_once():
@@ -99,7 +108,8 @@ def setup_once():
99108

100109
async def sentry_app_handle(self, request, *args, **kwargs):
101110
# type: (Any, Request, *Any, **Any) -> Any
102-
if sentry_sdk.get_client().get_integration(AioHttpIntegration) is None:
111+
integration = sentry_sdk.get_client().get_integration(AioHttpIntegration)
112+
if integration is None:
103113
return await old_handle(self, request, *args, **kwargs)
104114

105115
weak_request = weakref.ref(request)
@@ -130,6 +140,13 @@ async def sentry_app_handle(self, request, *args, **kwargs):
130140
response = await old_handle(self, request)
131141
except HTTPException as e:
132142
transaction.set_http_status(e.status_code)
143+
144+
if (
145+
e.status_code
146+
in integration._failed_request_status_codes
147+
):
148+
_capture_exception()
149+
133150
raise
134151
except (asyncio.CancelledError, ConnectionResetError):
135152
transaction.set_status(SPANSTATUS.CANCELLED)

0 commit comments

Comments
 (0)