|
47 | 47 | from aiohttp.web_request import Request |
48 | 48 | from aiohttp.web_urldispatcher import UrlMappingMatchInfo |
49 | 49 | from aiohttp import TraceRequestStartParams, TraceRequestEndParams |
| 50 | + |
| 51 | + from collections.abc import Set |
50 | 52 | from types import SimpleNamespace |
51 | 53 | from typing import Any |
52 | 54 | from typing import Optional |
|
58 | 60 |
|
59 | 61 |
|
60 | 62 | TRANSACTION_STYLE_VALUES = ("handler_name", "method_and_path_pattern") |
| 63 | +DEFAULT_FAILED_REQUEST_STATUS_CODES = frozenset(range(500, 600)) |
61 | 64 |
|
62 | 65 |
|
63 | 66 | class AioHttpIntegration(Integration): |
64 | 67 | identifier = "aiohttp" |
65 | 68 | origin = f"auto.http.{identifier}" |
66 | 69 |
|
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 |
69 | 77 | if transaction_style not in TRANSACTION_STYLE_VALUES: |
70 | 78 | raise ValueError( |
71 | 79 | "Invalid value for transaction_style: %s (must be in %s)" |
72 | 80 | % (transaction_style, TRANSACTION_STYLE_VALUES) |
73 | 81 | ) |
74 | 82 | self.transaction_style = transaction_style |
| 83 | + self._failed_request_status_codes = failed_request_status_codes |
75 | 84 |
|
76 | 85 | @staticmethod |
77 | 86 | def setup_once(): |
@@ -99,7 +108,8 @@ def setup_once(): |
99 | 108 |
|
100 | 109 | async def sentry_app_handle(self, request, *args, **kwargs): |
101 | 110 | # 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: |
103 | 113 | return await old_handle(self, request, *args, **kwargs) |
104 | 114 |
|
105 | 115 | weak_request = weakref.ref(request) |
@@ -130,6 +140,13 @@ async def sentry_app_handle(self, request, *args, **kwargs): |
130 | 140 | response = await old_handle(self, request) |
131 | 141 | except HTTPException as e: |
132 | 142 | 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 | + |
133 | 150 | raise |
134 | 151 | except (asyncio.CancelledError, ConnectionResetError): |
135 | 152 | transaction.set_status(SPANSTATUS.CANCELLED) |
|
0 commit comments