|
1 | 1 | import sys
|
2 | 2 | import weakref
|
| 3 | +from inspect import iscoroutinefunction |
3 | 4 |
|
4 | 5 | from sentry_sdk.hub import Hub, _should_send_default_pii
|
5 | 6 | from sentry_sdk.utils import (
|
@@ -42,20 +43,38 @@ def setup_once():
|
42 | 43 |
|
43 | 44 | old_execute = RequestHandler._execute
|
44 | 45 |
|
45 |
| - @coroutine |
46 |
| - def sentry_execute_request_handler(self, *args, **kwargs): |
47 |
| - hub = Hub.current |
48 |
| - integration = hub.get_integration(TornadoIntegration) |
49 |
| - if integration is None: |
50 |
| - return old_execute(self, *args, **kwargs) |
51 |
| - |
52 |
| - weak_handler = weakref.ref(self) |
53 |
| - |
54 |
| - with Hub(hub) as hub: |
55 |
| - with hub.configure_scope() as scope: |
56 |
| - scope.add_event_processor(_make_event_processor(weak_handler)) |
57 |
| - result = yield from old_execute(self, *args, **kwargs) |
58 |
| - return result |
| 46 | + awaitable = iscoroutinefunction(old_execute) |
| 47 | + |
| 48 | + if awaitable: |
| 49 | + # Starting Tornado 6 RequestHandler._execute method is a standard Python coroutine (async/await) |
| 50 | + # In that case our method should be a coroutine function too |
| 51 | + async def sentry_execute_request_handler(self, *args, **kwargs): |
| 52 | + hub = Hub.current |
| 53 | + integration = hub.get_integration(TornadoIntegration) |
| 54 | + if integration is None: |
| 55 | + return await old_execute(self, *args, **kwargs) |
| 56 | + |
| 57 | + weak_handler = weakref.ref(self) |
| 58 | + |
| 59 | + with Hub(hub) as hub: |
| 60 | + with hub.configure_scope() as scope: |
| 61 | + scope.add_event_processor(_make_event_processor(weak_handler)) |
| 62 | + return await old_execute(self, *args, **kwargs) |
| 63 | + else: |
| 64 | + @coroutine |
| 65 | + def sentry_execute_request_handler(self, *args, **kwargs): |
| 66 | + hub = Hub.current |
| 67 | + integration = hub.get_integration(TornadoIntegration) |
| 68 | + if integration is None: |
| 69 | + return old_execute(self, *args, **kwargs) |
| 70 | + |
| 71 | + weak_handler = weakref.ref(self) |
| 72 | + |
| 73 | + with Hub(hub) as hub: |
| 74 | + with hub.configure_scope() as scope: |
| 75 | + scope.add_event_processor(_make_event_processor(weak_handler)) |
| 76 | + result = yield from old_execute(self, *args, **kwargs) |
| 77 | + return result |
59 | 78 |
|
60 | 79 | RequestHandler._execute = sentry_execute_request_handler
|
61 | 80 |
|
|
0 commit comments