Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions sentry_sdk/integrations/falcon.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from sentry_sdk.hub import Hub
import sentry_sdk
from sentry_sdk.integrations import Integration, DidNotEnable
from sentry_sdk.integrations._wsgi_common import RequestExtractor
from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
from sentry_sdk.scope import Scope
from sentry_sdk.tracing import SOURCE_FOR_STYLE
from sentry_sdk.utils import (
capture_internal_exceptions,
Expand Down Expand Up @@ -100,14 +101,13 @@ class SentryFalconMiddleware:

def process_request(self, req, resp, *args, **kwargs):
# type: (Any, Any, *Any, **Any) -> None
hub = Hub.current
integration = hub.get_integration(FalconIntegration)
integration = sentry_sdk.get_client().get_integration(FalconIntegration)
if integration is None:
return

with hub.configure_scope() as scope:
scope._name = "falcon"
scope.add_event_processor(_make_request_event_processor(req, integration))
scope = Scope.get_isolation_scope()
scope._name = "falcon"
scope.add_event_processor(_make_request_event_processor(req, integration))


TRANSACTION_STYLE_VALUES = ("uri_template", "path")
Expand Down Expand Up @@ -150,8 +150,7 @@ def _patch_wsgi_app():

def sentry_patched_wsgi_app(self, env, start_response):
# type: (falcon.API, Any, Any) -> Any
hub = Hub.current
integration = hub.get_integration(FalconIntegration)
integration = sentry_sdk.get_client().get_integration(FalconIntegration)
if integration is None:
return original_wsgi_app(self, env, start_response)

Expand Down Expand Up @@ -188,19 +187,17 @@ def sentry_patched_handle_exception(self, *args):
# capture_internal_exceptions block above.
return was_handled

hub = Hub.current
integration = hub.get_integration(FalconIntegration)
client = sentry_sdk.get_client()
integration = client.get_integration(FalconIntegration)

if integration is not None and _exception_leads_to_http_5xx(ex, response):
# If an integration is there, a client has to be there.
client = hub.client # type: Any

event, hint = event_from_exception(
ex,
client_options=client.options,
mechanism={"type": "falcon", "handled": False},
)
hub.capture_event(event, hint=hint)
sentry_sdk.capture_event(event, hint=hint)

return was_handled

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

hub = Hub.current
integration = hub.get_integration(FalconIntegration)
integration = sentry_sdk.get_client().get_integration(FalconIntegration)
if integration is not None:
middleware = [SentryFalconMiddleware()] + (middleware or [])

Expand Down
13 changes: 5 additions & 8 deletions tests/integrations/falcon/test_falcon.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import sentry_sdk
from sentry_sdk.integrations.falcon import FalconIntegration
from sentry_sdk.integrations.logging import LoggingIntegration
from sentry_sdk.scope import Scope
from sentry_sdk.utils import parse_version


Expand Down Expand Up @@ -379,20 +380,17 @@ def test_does_not_leak_scope(sentry_init, capture_events):
sentry_init(integrations=[FalconIntegration()])
events = capture_events()

with sentry_sdk.configure_scope() as scope:
scope.set_tag("request_data", False)
Scope.get_isolation_scope().set_tag("request_data", False)

app = falcon.API()

class Resource:
def on_get(self, req, resp):
with sentry_sdk.configure_scope() as scope:
scope.set_tag("request_data", True)
Scope.get_isolation_scope().set_tag("request_data", True)

def generator():
for row in range(1000):
with sentry_sdk.configure_scope() as scope:
assert scope._tags["request_data"]
assert Scope.get_isolation_scope()._tags["request_data"]

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

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

with sentry_sdk.configure_scope() as scope:
assert not scope._tags["request_data"]
not Scope.get_isolation_scope()._tags["request_data"]


@pytest.mark.skipif(
Expand Down