Skip to content

Commit a7c2a59

Browse files
glibinuntitaker
authored andcommitted
Add tornado 6 support (getsentry#246)
* Add tornado 6 support * add branch comment about Tornado version
1 parent a61cf7c commit a7c2a59

File tree

2 files changed

+36
-16
lines changed

2 files changed

+36
-16
lines changed

sentry_sdk/integrations/tornado.py

+33-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import sys
22
import weakref
3+
from inspect import iscoroutinefunction
34

45
from sentry_sdk.hub import Hub, _should_send_default_pii
56
from sentry_sdk.utils import (
@@ -42,20 +43,38 @@ def setup_once():
4243

4344
old_execute = RequestHandler._execute
4445

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
5978

6079
RequestHandler._execute = sentry_execute_request_handler
6180

tox.ini

+3-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ envlist =
3434
{pypy,py2.7,py3.5,py3.6,py3.7,py3.8}-rq-0.12
3535

3636
py3.7-aiohttp
37-
{py3.7,py3.8}-tornado
37+
{py3.7,py3.8}-tornado-{5,6}
3838

3939
[testenv]
4040
deps =
@@ -90,7 +90,8 @@ deps =
9090
aiohttp: aiohttp>=3.4.0,<3.5.0
9191
aiohttp: pytest-aiohttp
9292

93-
tornado: tornado>=5,<6
93+
tornado-5: tornado>=5,<6
94+
tornado-6: tornado>=6.0a1
9495

9596
linters: black
9697
linters: flake8

0 commit comments

Comments
 (0)