From 0a01355b5bc42b8bacea31486230664a4e390716 Mon Sep 17 00:00:00 2001 From: Vitaly Glibin Date: Tue, 29 Jan 2019 13:18:08 +0300 Subject: [PATCH 1/2] Add tornado 6 support --- sentry_sdk/integrations/tornado.py | 45 ++++++++++++++++++++---------- tox.ini | 5 ++-- 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/sentry_sdk/integrations/tornado.py b/sentry_sdk/integrations/tornado.py index 7eb36f81d4..2edddf5ed0 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,36 @@ 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: + 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 From 21f9614eabda20b24f04dc68b35f639edac38271 Mon Sep 17 00:00:00 2001 From: Vitaly Glibin Date: Tue, 29 Jan 2019 14:00:06 +0300 Subject: [PATCH 2/2] add branch comment about Tornado version --- sentry_sdk/integrations/tornado.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sentry_sdk/integrations/tornado.py b/sentry_sdk/integrations/tornado.py index 2edddf5ed0..1dc495ad45 100644 --- a/sentry_sdk/integrations/tornado.py +++ b/sentry_sdk/integrations/tornado.py @@ -46,6 +46,8 @@ def setup_once(): 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)