Skip to content

Commit 2adb74e

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 2adb74e

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

sentry_sdk/integrations/aiohttp.py

Lines changed: 19 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
@@ -64,14 +66,20 @@ class AioHttpIntegration(Integration):
6466
identifier = "aiohttp"
6567
origin = f"auto.http.{identifier}"
6668

67-
def __init__(self, transaction_style="handler_name"):
68-
# type: (str) -> None
69+
def __init__(
70+
self,
71+
transaction_style="handler_name", # type: str
72+
*,
73+
failed_request_status_codes=frozenset(range(500, 600)), # type: Set[int]
74+
):
75+
# type: (...) -> None
6976
if transaction_style not in TRANSACTION_STYLE_VALUES:
7077
raise ValueError(
7178
"Invalid value for transaction_style: %s (must be in %s)"
7279
% (transaction_style, TRANSACTION_STYLE_VALUES)
7380
)
7481
self.transaction_style = transaction_style
82+
self._failed_request_status_codes = failed_request_status_codes
7583

7684
@staticmethod
7785
def setup_once():
@@ -99,7 +107,8 @@ def setup_once():
99107

100108
async def sentry_app_handle(self, request, *args, **kwargs):
101109
# type: (Any, Request, *Any, **Any) -> Any
102-
if sentry_sdk.get_client().get_integration(AioHttpIntegration) is None:
110+
integration = sentry_sdk.get_client().get_integration(AioHttpIntegration)
111+
if integration is None:
103112
return await old_handle(self, request, *args, **kwargs)
104113

105114
weak_request = weakref.ref(request)
@@ -130,6 +139,13 @@ async def sentry_app_handle(self, request, *args, **kwargs):
130139
response = await old_handle(self, request)
131140
except HTTPException as e:
132141
transaction.set_http_status(e.status_code)
142+
143+
if (
144+
e.status_code
145+
in integration._failed_request_status_codes
146+
):
147+
_capture_exception()
148+
133149
raise
134150
except (asyncio.CancelledError, ConnectionResetError):
135151
transaction.set_status(SPANSTATUS.CANCELLED)

0 commit comments

Comments
 (0)