Skip to content

Commit 63fb50d

Browse files
authored
ref(falcon): Use new scopes API (#2871)
1 parent 7659554 commit 63fb50d

File tree

2 files changed

+16
-23
lines changed

2 files changed

+16
-23
lines changed

sentry_sdk/integrations/falcon.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
from sentry_sdk.hub import Hub
1+
import sentry_sdk
22
from sentry_sdk.integrations import Integration, DidNotEnable
33
from sentry_sdk.integrations._wsgi_common import RequestExtractor
44
from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
5+
from sentry_sdk.scope import Scope
56
from sentry_sdk.tracing import SOURCE_FOR_STYLE
67
from sentry_sdk.utils import (
78
capture_internal_exceptions,
@@ -100,14 +101,13 @@ class SentryFalconMiddleware:
100101

101102
def process_request(self, req, resp, *args, **kwargs):
102103
# type: (Any, Any, *Any, **Any) -> None
103-
hub = Hub.current
104-
integration = hub.get_integration(FalconIntegration)
104+
integration = sentry_sdk.get_client().get_integration(FalconIntegration)
105105
if integration is None:
106106
return
107107

108-
with hub.configure_scope() as scope:
109-
scope._name = "falcon"
110-
scope.add_event_processor(_make_request_event_processor(req, integration))
108+
scope = Scope.get_isolation_scope()
109+
scope._name = "falcon"
110+
scope.add_event_processor(_make_request_event_processor(req, integration))
111111

112112

113113
TRANSACTION_STYLE_VALUES = ("uri_template", "path")
@@ -150,8 +150,7 @@ def _patch_wsgi_app():
150150

151151
def sentry_patched_wsgi_app(self, env, start_response):
152152
# type: (falcon.API, Any, Any) -> Any
153-
hub = Hub.current
154-
integration = hub.get_integration(FalconIntegration)
153+
integration = sentry_sdk.get_client().get_integration(FalconIntegration)
155154
if integration is None:
156155
return original_wsgi_app(self, env, start_response)
157156

@@ -188,19 +187,17 @@ def sentry_patched_handle_exception(self, *args):
188187
# capture_internal_exceptions block above.
189188
return was_handled
190189

191-
hub = Hub.current
192-
integration = hub.get_integration(FalconIntegration)
190+
client = sentry_sdk.get_client()
191+
integration = client.get_integration(FalconIntegration)
193192

194193
if integration is not None and _exception_leads_to_http_5xx(ex, response):
195194
# If an integration is there, a client has to be there.
196-
client = hub.client # type: Any
197-
198195
event, hint = event_from_exception(
199196
ex,
200197
client_options=client.options,
201198
mechanism={"type": "falcon", "handled": False},
202199
)
203-
hub.capture_event(event, hint=hint)
200+
sentry_sdk.capture_event(event, hint=hint)
204201

205202
return was_handled
206203

@@ -219,8 +216,7 @@ def sentry_patched_prepare_middleware(
219216
# We don't support ASGI Falcon apps, so we don't patch anything here
220217
return original_prepare_middleware(middleware, independent_middleware, asgi)
221218

222-
hub = Hub.current
223-
integration = hub.get_integration(FalconIntegration)
219+
integration = sentry_sdk.get_client().get_integration(FalconIntegration)
224220
if integration is not None:
225221
middleware = [SentryFalconMiddleware()] + (middleware or [])
226222

tests/integrations/falcon/test_falcon.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import sentry_sdk
88
from sentry_sdk.integrations.falcon import FalconIntegration
99
from sentry_sdk.integrations.logging import LoggingIntegration
10+
from sentry_sdk.scope import Scope
1011
from sentry_sdk.utils import parse_version
1112

1213

@@ -379,20 +380,17 @@ def test_does_not_leak_scope(sentry_init, capture_events):
379380
sentry_init(integrations=[FalconIntegration()])
380381
events = capture_events()
381382

382-
with sentry_sdk.configure_scope() as scope:
383-
scope.set_tag("request_data", False)
383+
Scope.get_isolation_scope().set_tag("request_data", False)
384384

385385
app = falcon.API()
386386

387387
class Resource:
388388
def on_get(self, req, resp):
389-
with sentry_sdk.configure_scope() as scope:
390-
scope.set_tag("request_data", True)
389+
Scope.get_isolation_scope().set_tag("request_data", True)
391390

392391
def generator():
393392
for row in range(1000):
394-
with sentry_sdk.configure_scope() as scope:
395-
assert scope._tags["request_data"]
393+
assert Scope.get_isolation_scope()._tags["request_data"]
396394

397395
yield (str(row) + "\n").encode()
398396

@@ -407,8 +405,7 @@ def generator():
407405
assert response.text == expected_response
408406
assert not events
409407

410-
with sentry_sdk.configure_scope() as scope:
411-
assert not scope._tags["request_data"]
408+
not Scope.get_isolation_scope()._tags["request_data"]
412409

413410

414411
@pytest.mark.skipif(

0 commit comments

Comments
 (0)