diff --git a/sentry_sdk/integrations/tornado.py b/sentry_sdk/integrations/tornado.py index 7eb36f81d4..1dc495ad45 100644 --- a/sentry_sdk/integrations/tornado.py +++ b/sentry_sdk/integrations/tornado.py @@ -1,5 +1,6 @@ import sys import weakref +from inspect import iscoroutinefunction from sentry_sdk.hub import Hub, _should_send_default_pii from sentry_sdk.utils import ( @@ -42,20 +43,38 @@ def setup_once(): old_execute = RequestHandler._execute - @coroutine - def sentry_execute_request_handler(self, *args, **kwargs): - hub = Hub.current - integration = hub.get_integration(TornadoIntegration) - if integration is None: - return old_execute(self, *args, **kwargs) - - weak_handler = weakref.ref(self) - - with Hub(hub) as hub: - with hub.configure_scope() as scope: - scope.add_event_processor(_make_event_processor(weak_handler)) - result = yield from old_execute(self, *args, **kwargs) - return result + awaitable = iscoroutinefunction(old_execute) + + if awaitable: + # Starting Tornado 6 RequestHandler._execute method is a standard Python coroutine (async/await) + # In that case our method should be a coroutine function too + async def sentry_execute_request_handler(self, *args, **kwargs): + hub = Hub.current + integration = hub.get_integration(TornadoIntegration) + if integration is None: + return await old_execute(self, *args, **kwargs) + + weak_handler = weakref.ref(self) + + with Hub(hub) as hub: + with hub.configure_scope() as scope: + scope.add_event_processor(_make_event_processor(weak_handler)) + return await old_execute(self, *args, **kwargs) + else: + @coroutine + def sentry_execute_request_handler(self, *args, **kwargs): + hub = Hub.current + integration = hub.get_integration(TornadoIntegration) + if integration is None: + return old_execute(self, *args, **kwargs) + + weak_handler = weakref.ref(self) + + with Hub(hub) as hub: + with hub.configure_scope() as scope: + scope.add_event_processor(_make_event_processor(weak_handler)) + result = yield from old_execute(self, *args, **kwargs) + return result RequestHandler._execute = sentry_execute_request_handler diff --git a/tox.ini b/tox.ini index 0139dffa4f..bd4407e794 100644 --- a/tox.ini +++ b/tox.ini @@ -34,7 +34,7 @@ envlist = {pypy,py2.7,py3.5,py3.6,py3.7,py3.8}-rq-0.12 py3.7-aiohttp - {py3.7,py3.8}-tornado + {py3.7,py3.8}-tornado-{5,6} [testenv] deps = @@ -90,7 +90,8 @@ deps = aiohttp: aiohttp>=3.4.0,<3.5.0 aiohttp: pytest-aiohttp - tornado: tornado>=5,<6 + tornado-5: tornado>=5,<6 + tornado-6: tornado>=6.0a1 linters: black linters: flake8