Skip to content

Commit ec98b3e

Browse files
Add session for aiohttp integration (getsentry#1605)
1 parent c05bcf5 commit ec98b3e

1 file changed

Lines changed: 35 additions & 32 deletions

File tree

sentry_sdk/integrations/aiohttp.py

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from sentry_sdk.hub import Hub
66
from sentry_sdk.integrations import Integration, DidNotEnable
77
from sentry_sdk.integrations.logging import ignore_logger
8+
from sentry_sdk.sessions import auto_session_tracking
89
from sentry_sdk.integrations._wsgi_common import (
910
_filter_headers,
1011
request_body_within_bounds,
@@ -91,38 +92,40 @@ async def sentry_app_handle(self, request, *args, **kwargs):
9192
weak_request = weakref.ref(request)
9293

9394
with Hub(hub) as hub:
94-
# Scope data will not leak between requests because aiohttp
95-
# create a task to wrap each request.
96-
with hub.configure_scope() as scope:
97-
scope.clear_breadcrumbs()
98-
scope.add_event_processor(_make_request_processor(weak_request))
99-
100-
transaction = Transaction.continue_from_headers(
101-
request.headers,
102-
op="http.server",
103-
# If this transaction name makes it to the UI, AIOHTTP's
104-
# URL resolver did not find a route or died trying.
105-
name="generic AIOHTTP request",
106-
source=TRANSACTION_SOURCE_ROUTE,
107-
)
108-
with hub.start_transaction(
109-
transaction, custom_sampling_context={"aiohttp_request": request}
110-
):
111-
try:
112-
response = await old_handle(self, request)
113-
except HTTPException as e:
114-
transaction.set_http_status(e.status_code)
115-
raise
116-
except (asyncio.CancelledError, ConnectionResetError):
117-
transaction.set_status("cancelled")
118-
raise
119-
except Exception:
120-
# This will probably map to a 500 but seems like we
121-
# have no way to tell. Do not set span status.
122-
reraise(*_capture_exception(hub))
123-
124-
transaction.set_http_status(response.status)
125-
return response
95+
with auto_session_tracking(hub, session_mode="request"):
96+
# Scope data will not leak between requests because aiohttp
97+
# create a task to wrap each request.
98+
with hub.configure_scope() as scope:
99+
scope.clear_breadcrumbs()
100+
scope.add_event_processor(_make_request_processor(weak_request))
101+
102+
transaction = Transaction.continue_from_headers(
103+
request.headers,
104+
op="http.server",
105+
# If this transaction name makes it to the UI, AIOHTTP's
106+
# URL resolver did not find a route or died trying.
107+
name="generic AIOHTTP request",
108+
source=TRANSACTION_SOURCE_ROUTE,
109+
)
110+
with hub.start_transaction(
111+
transaction,
112+
custom_sampling_context={"aiohttp_request": request},
113+
):
114+
try:
115+
response = await old_handle(self, request)
116+
except HTTPException as e:
117+
transaction.set_http_status(e.status_code)
118+
raise
119+
except (asyncio.CancelledError, ConnectionResetError):
120+
transaction.set_status("cancelled")
121+
raise
122+
except Exception:
123+
# This will probably map to a 500 but seems like we
124+
# have no way to tell. Do not set span status.
125+
reraise(*_capture_exception(hub))
126+
127+
transaction.set_http_status(response.status)
128+
return response
126129

127130
Application._handle = sentry_app_handle
128131

0 commit comments

Comments
 (0)