From e8ecd4fb555fd36ae17f7d8260a895d21b553be2 Mon Sep 17 00:00:00 2001 From: Denys Pidlisnyi Date: Thu, 8 Sep 2022 23:22:04 +0300 Subject: [PATCH 1/2] Add session for aiohttp integration --- sentry_sdk/integrations/aiohttp.py | 66 +++++++++++++++--------------- 1 file changed, 34 insertions(+), 32 deletions(-) diff --git a/sentry_sdk/integrations/aiohttp.py b/sentry_sdk/integrations/aiohttp.py index f07790173d..56fdc87872 100644 --- a/sentry_sdk/integrations/aiohttp.py +++ b/sentry_sdk/integrations/aiohttp.py @@ -5,6 +5,7 @@ from sentry_sdk.hub import Hub from sentry_sdk.integrations import Integration, DidNotEnable from sentry_sdk.integrations.logging import ignore_logger +from sentry_sdk.sessions import auto_session_tracking from sentry_sdk.integrations._wsgi_common import ( _filter_headers, request_body_within_bounds, @@ -91,38 +92,39 @@ async def sentry_app_handle(self, request, *args, **kwargs): weak_request = weakref.ref(request) with Hub(hub) as hub: - # Scope data will not leak between requests because aiohttp - # create a task to wrap each request. - with hub.configure_scope() as scope: - scope.clear_breadcrumbs() - scope.add_event_processor(_make_request_processor(weak_request)) - - transaction = Transaction.continue_from_headers( - request.headers, - op="http.server", - # If this transaction name makes it to the UI, AIOHTTP's - # URL resolver did not find a route or died trying. - name="generic AIOHTTP request", - source=TRANSACTION_SOURCE_ROUTE, - ) - with hub.start_transaction( - transaction, custom_sampling_context={"aiohttp_request": request} - ): - try: - response = await old_handle(self, request) - except HTTPException as e: - transaction.set_http_status(e.status_code) - raise - except (asyncio.CancelledError, ConnectionResetError): - transaction.set_status("cancelled") - raise - except Exception: - # This will probably map to a 500 but seems like we - # have no way to tell. Do not set span status. - reraise(*_capture_exception(hub)) - - transaction.set_http_status(response.status) - return response + with auto_session_tracking(hub, session_mode="request"): + # Scope data will not leak between requests because aiohttp + # create a task to wrap each request. + with hub.configure_scope() as scope: + scope.clear_breadcrumbs() + scope.add_event_processor(_make_request_processor(weak_request)) + + transaction = Transaction.continue_from_headers( + request.headers, + op="http.server", + # If this transaction name makes it to the UI, AIOHTTP's + # URL resolver did not find a route or died trying. + name="generic AIOHTTP request", + source=TRANSACTION_SOURCE_ROUTE, + ) + with hub.start_transaction( + transaction, custom_sampling_context={"aiohttp_request": request} + ): + try: + response = await old_handle(self, request) + except HTTPException as e: + transaction.set_http_status(e.status_code) + raise + except (asyncio.CancelledError, ConnectionResetError): + transaction.set_status("cancelled") + raise + except Exception: + # This will probably map to a 500 but seems like we + # have no way to tell. Do not set span status. + reraise(*_capture_exception(hub)) + + transaction.set_http_status(response.status) + return response Application._handle = sentry_app_handle From f483226867b2645f83944ed183da17a1d617faf0 Mon Sep 17 00:00:00 2001 From: Neel Shah Date: Mon, 10 Oct 2022 13:34:32 +0200 Subject: [PATCH 2/2] Lint --- sentry_sdk/integrations/aiohttp.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sentry_sdk/integrations/aiohttp.py b/sentry_sdk/integrations/aiohttp.py index fc0cf01ff6..8db3f11afa 100644 --- a/sentry_sdk/integrations/aiohttp.py +++ b/sentry_sdk/integrations/aiohttp.py @@ -108,7 +108,8 @@ async def sentry_app_handle(self, request, *args, **kwargs): source=TRANSACTION_SOURCE_ROUTE, ) with hub.start_transaction( - transaction, custom_sampling_context={"aiohttp_request": request} + transaction, + custom_sampling_context={"aiohttp_request": request}, ): try: response = await old_handle(self, request)