Skip to content

Commit 0b64a40

Browse files
committed
Allow to override asgi capturers
1 parent a5ce968 commit 0b64a40

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

sentry_sdk/integrations/asgi.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,6 @@
6060
TRANSACTION_STYLE_VALUES = ("endpoint", "url")
6161

6262

63-
def _capture_exception(exc, mechanism_type="asgi"):
64-
# type: (Any, str) -> None
65-
66-
event, hint = event_from_exception(
67-
exc,
68-
client_options=sentry_sdk.get_client().options,
69-
mechanism={"type": mechanism_type, "handled": False},
70-
)
71-
sentry_sdk.capture_event(event, hint=hint)
72-
73-
7463
def _looks_like_asgi3(app):
7564
# type: (Any) -> bool
7665
"""
@@ -148,6 +137,23 @@ def __init__(
148137
else:
149138
self.__call__ = self._run_asgi2
150139

140+
def _capture_exception(self, exc):
141+
# type: (Exception) -> None
142+
event, hint = event_from_exception(
143+
exc,
144+
client_options=sentry_sdk.get_client().options,
145+
mechanism={"type": self.mechanism_type, "handled": False},
146+
)
147+
sentry_sdk.capture_event(event, hint=hint)
148+
149+
def _capture_lifespan_exception(self, exc):
150+
# type: (Exception) -> None
151+
return self._capture_exception(exc)
152+
153+
def _capture_request_exception(self, exc):
154+
# type: (Exception) -> None
155+
return self._capture_exception(exc)
156+
151157
def _run_asgi2(self, scope):
152158
# type: (Any) -> Any
153159
async def inner(receive, send):
@@ -161,7 +167,7 @@ async def _run_asgi3(self, scope, receive, send):
161167
return await self._run_app(scope, receive, send, asgi_version=3)
162168

163169
async def _run_app(self, scope, receive, send, asgi_version):
164-
# type: (Any, Any, Any, Any, int) -> Any
170+
# type: (Any, Any, Any, int) -> Any
165171
is_recursive_asgi_middleware = _asgi_middleware_applied.get(False)
166172
is_lifespan = scope["type"] == "lifespan"
167173
if is_recursive_asgi_middleware or is_lifespan:
@@ -172,7 +178,7 @@ async def _run_app(self, scope, receive, send, asgi_version):
172178
return await self.app(scope, receive, send)
173179

174180
except Exception as exc:
175-
_capture_exception(exc, mechanism_type=self.mechanism_type)
181+
self._capture_lifespan_exception(exc)
176182
raise exc from None
177183

178184
_asgi_middleware_applied.set(True)
@@ -258,7 +264,7 @@ async def _sentry_wrapped_send(event):
258264
scope, receive, _sentry_wrapped_send
259265
)
260266
except Exception as exc:
261-
_capture_exception(exc, mechanism_type=self.mechanism_type)
267+
self._capture_request_exception(exc)
262268
raise exc from None
263269
finally:
264270
_asgi_middleware_applied.set(False)

0 commit comments

Comments
 (0)