Skip to content

Commit c06bf06

Browse files
authored
ref(tornado): Use new scopes API (#2907)
1 parent 19d00c8 commit c06bf06

File tree

4 files changed

+19
-23
lines changed

4 files changed

+19
-23
lines changed

MIGRATION_GUIDE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Looking to upgrade from Sentry SDK 1.x to 2.x? Here's a comprehensive list of wh
6262
- Removed support for Django 1.8, 1.9, 1.10.
6363
- Removed support for Flask 0.\*.
6464
- Removed support for gRPC < 1.39.
65+
- Removed support for Tornado < 6.
6566
- Removed `last_event_id()` top level API. The last event ID is still returned by `capture_event()`, `capture_exception()` and `capture_message()` but the top level API `sentry_sdk.last_event_id()` has been removed.
6667
- Removed support for sending events to the `/store` endpoint. Everything is now sent to the `/envelope` endpoint. If you're on SaaS you don't have to worry about this, but if you're running Sentry yourself you'll need version `20.6.0` or higher of self-hosted Sentry.
6768
- The deprecated `with_locals` configuration option was removed. Use `include_local_variables` instead. See https://docs.sentry.io/platforms/python/configuration/options/#include-local-variables.

sentry_sdk/integrations/tornado.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22
import contextlib
33
from inspect import iscoroutinefunction
44

5+
import sentry_sdk
56
from sentry_sdk.api import continue_trace
67
from sentry_sdk.consts import OP
7-
from sentry_sdk.hub import Hub, _should_send_default_pii
8+
from sentry_sdk.scope import should_send_default_pii
89
from sentry_sdk.tracing import (
910
TRANSACTION_SOURCE_COMPONENT,
1011
TRANSACTION_SOURCE_ROUTE,
1112
)
1213
from sentry_sdk.utils import (
1314
HAS_REAL_CONTEXTVARS,
1415
CONTEXTVARS_ERROR_MESSAGE,
16+
ensure_integration_enabled,
1517
event_from_exception,
1618
capture_internal_exceptions,
1719
transaction_from_function,
@@ -49,8 +51,8 @@ class TornadoIntegration(Integration):
4951
@staticmethod
5052
def setup_once():
5153
# type: () -> None
52-
if TORNADO_VERSION < (5, 0):
53-
raise DidNotEnable("Tornado 5+ required")
54+
if TORNADO_VERSION < (6, 0):
55+
raise DidNotEnable("Tornado 6.0+ required")
5456

5557
if not HAS_REAL_CONTEXTVARS:
5658
# Tornado is async. We better have contextvars or we're going to leak
@@ -98,21 +100,19 @@ def sentry_log_exception(self, ty, value, tb, *args, **kwargs):
98100
@contextlib.contextmanager
99101
def _handle_request_impl(self):
100102
# type: (RequestHandler) -> Generator[None, None, None]
101-
hub = Hub.current
102-
integration = hub.get_integration(TornadoIntegration)
103+
integration = sentry_sdk.get_client().get_integration(TornadoIntegration)
103104

104105
if integration is None:
105106
yield
106107

107108
weak_handler = weakref.ref(self)
108109

109-
with Hub(hub) as hub:
110+
with sentry_sdk.isolation_scope() as scope:
110111
headers = self.request.headers
111112

112-
with hub.configure_scope() as scope:
113-
scope.clear_breadcrumbs()
114-
processor = _make_event_processor(weak_handler)
115-
scope.add_event_processor(processor)
113+
scope.clear_breadcrumbs()
114+
processor = _make_event_processor(weak_handler)
115+
scope.add_event_processor(processor)
116116

117117
transaction = continue_trace(
118118
headers,
@@ -125,30 +125,25 @@ def _handle_request_impl(self):
125125
source=TRANSACTION_SOURCE_ROUTE,
126126
)
127127

128-
with hub.start_transaction(
128+
with sentry_sdk.start_transaction(
129129
transaction, custom_sampling_context={"tornado_request": self.request}
130130
):
131131
yield
132132

133133

134+
@ensure_integration_enabled(TornadoIntegration)
134135
def _capture_exception(ty, value, tb):
135136
# type: (type, BaseException, Any) -> None
136-
hub = Hub.current
137-
if hub.get_integration(TornadoIntegration) is None:
138-
return
139137
if isinstance(value, HTTPError):
140138
return
141139

142-
# If an integration is there, a client has to be there.
143-
client = hub.client # type: Any
144-
145140
event, hint = event_from_exception(
146141
(ty, value, tb),
147-
client_options=client.options,
142+
client_options=sentry_sdk.get_client().options,
148143
mechanism={"type": "tornado", "handled": False},
149144
)
150145

151-
hub.capture_event(event, hint=hint)
146+
sentry_sdk.capture_event(event, hint=hint)
152147

153148

154149
def _make_event_processor(weak_handler):
@@ -184,7 +179,7 @@ def tornado_processor(event, hint):
184179
request_info["headers"] = _filter_headers(dict(request.headers))
185180

186181
with capture_internal_exceptions():
187-
if handler.current_user and _should_send_default_pii():
182+
if handler.current_user and should_send_default_pii():
188183
event.setdefault("user", {}).setdefault("is_authenticated", True)
189184

190185
return event

tests/integrations/tornado/test_tornado.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def test_basic(tornado_testcase, sentry_init, capture_events):
112112
],
113113
)
114114
def test_transactions(tornado_testcase, sentry_init, capture_events, handler, code):
115-
sentry_init(integrations=[TornadoIntegration()], traces_sample_rate=1.0, debug=True)
115+
sentry_init(integrations=[TornadoIntegration()], traces_sample_rate=1.0)
116116
events = capture_events()
117117
client = tornado_testcase(Application([(r"/hi", handler)]))
118118

tox.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ envlist =
225225
{py3.8,py3.11,py3.12}-strawberry-latest
226226

227227
# Tornado
228-
{py3.7,py3.9}-tornado-v{5}
228+
{py3.8,py3.11,py3.12}-tornado-v{6.0}
229229
{py3.8,py3.11,py3.12}-tornado-v{6}
230230
{py3.8,py3.11,py3.12}-tornado-latest
231231

@@ -562,7 +562,7 @@ deps =
562562
strawberry-latest: strawberry-graphql[fastapi,flask]
563563

564564
# Tornado
565-
tornado-v5: tornado~=5.0
565+
tornado-v6.0: tornado~=6.0.0
566566
tornado-v6: tornado~=6.0
567567
tornado-latest: tornado
568568

0 commit comments

Comments
 (0)