Skip to content

Commit d617e54

Browse files
authored
fix: Preserve contextvars in aiohttp integration (getsentry#674)
aiohttp integration currently re-create a task to encapsulate the request handler. But: - aiohttp already does it. - contextvars created in it can't be read by aiohttp. It's an issue for users custom logger. Fix getsentry#670
1 parent f90cb06 commit d617e54

File tree

1 file changed

+35
-40
lines changed

1 file changed

+35
-40
lines changed

sentry_sdk/integrations/aiohttp.py

+35-40
Original file line numberDiff line numberDiff line change
@@ -71,46 +71,41 @@ def setup_once():
7171

7272
async def sentry_app_handle(self, request, *args, **kwargs):
7373
# type: (Any, Request, *Any, **Any) -> Any
74-
async def inner():
75-
# type: () -> Any
76-
hub = Hub.current
77-
if hub.get_integration(AioHttpIntegration) is None:
78-
return await old_handle(self, request, *args, **kwargs)
79-
80-
weak_request = weakref.ref(request)
81-
82-
with Hub(Hub.current) as hub:
83-
with hub.configure_scope() as scope:
84-
scope.clear_breadcrumbs()
85-
scope.add_event_processor(_make_request_processor(weak_request))
86-
87-
span = Span.continue_from_headers(request.headers)
88-
span.op = "http.server"
89-
# If this transaction name makes it to the UI, AIOHTTP's
90-
# URL resolver did not find a route or died trying.
91-
span.transaction = "generic AIOHTTP request"
92-
93-
with hub.start_span(span):
94-
try:
95-
response = await old_handle(self, request)
96-
except HTTPException as e:
97-
span.set_http_status(e.status_code)
98-
raise
99-
except asyncio.CancelledError:
100-
span.set_status("cancelled")
101-
raise
102-
except Exception:
103-
# This will probably map to a 500 but seems like we
104-
# have no way to tell. Do not set span status.
105-
reraise(*_capture_exception(hub))
106-
107-
span.set_http_status(response.status)
108-
return response
109-
110-
# Explicitly wrap in task such that current contextvar context is
111-
# copied. Just doing `return await inner()` will leak scope data
112-
# between requests.
113-
return await asyncio.get_event_loop().create_task(inner())
74+
hub = Hub.current
75+
if hub.get_integration(AioHttpIntegration) is None:
76+
return await old_handle(self, request, *args, **kwargs)
77+
78+
weak_request = weakref.ref(request)
79+
80+
with Hub(Hub.current) as hub:
81+
# Scope data will not leak between requests because aiohttp
82+
# create a task to wrap each request.
83+
with hub.configure_scope() as scope:
84+
scope.clear_breadcrumbs()
85+
scope.add_event_processor(_make_request_processor(weak_request))
86+
87+
span = Span.continue_from_headers(request.headers)
88+
span.op = "http.server"
89+
# If this transaction name makes it to the UI, AIOHTTP's
90+
# URL resolver did not find a route or died trying.
91+
span.transaction = "generic AIOHTTP request"
92+
93+
with hub.start_span(span):
94+
try:
95+
response = await old_handle(self, request)
96+
except HTTPException as e:
97+
span.set_http_status(e.status_code)
98+
raise
99+
except asyncio.CancelledError:
100+
span.set_status("cancelled")
101+
raise
102+
except Exception:
103+
# This will probably map to a 500 but seems like we
104+
# have no way to tell. Do not set span status.
105+
reraise(*_capture_exception(hub))
106+
107+
span.set_http_status(response.status)
108+
return response
114109

115110
Application._handle = sentry_app_handle
116111

0 commit comments

Comments
 (0)