Skip to content

Commit

Permalink
Prevent Falcon integration from breaking ASGI apps (#2359)
Browse files Browse the repository at this point in the history
* Prevent Falcon integration from breaking ASGI apps

* Remove trailing comma
  • Loading branch information
szokeasaurusrex authored Sep 12, 2023
1 parent 44ba734 commit 90c64ca
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
11 changes: 9 additions & 2 deletions sentry_sdk/integrations/falcon.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,20 @@ def _patch_prepare_middleware():
original_prepare_middleware = falcon_helpers.prepare_middleware

def sentry_patched_prepare_middleware(
middleware=None, independent_middleware=False
middleware=None, independent_middleware=False, asgi=False
):
# type: (Any, Any) -> Any
# type: (Any, Any, bool) -> Any
if asgi:
# 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)
if integration is not None:
middleware = [SentryFalconMiddleware()] + (middleware or [])

# We intentionally omit the asgi argument here, since the default is False anyways,
# and this way, we remain backwards-compatible with pre-3.0.0 Falcon versions.
return original_prepare_middleware(middleware, independent_middleware)

falcon_helpers.prepare_middleware = sentry_patched_prepare_middleware
Expand Down
29 changes: 29 additions & 0 deletions tests/integrations/falcon/test_falcon.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
from sentry_sdk.integrations.logging import LoggingIntegration


try:
import falcon.asgi
except ImportError:
pass
else:
import falcon.inspect # We only need this module for the ASGI test


@pytest.fixture
def make_app(sentry_init):
def inner():
Expand Down Expand Up @@ -391,3 +399,24 @@ def generator():

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


@pytest.mark.skipif(
not hasattr(falcon, "asgi"), reason="This Falcon version lacks ASGI support."
)
def test_falcon_not_breaking_asgi(sentry_init):
"""
This test simply verifies that the Falcon integration does not break ASGI
Falcon apps.
The test does not verify ASGI Falcon support, since our Falcon integration
currently lacks support for ASGI Falcon apps.
"""
sentry_init(integrations=[FalconIntegration()])

asgi_app = falcon.asgi.App()

try:
falcon.inspect.inspect_app(asgi_app)
except TypeError:
pytest.fail("Falcon integration causing errors in ASGI apps.")

0 comments on commit 90c64ca

Please sign in to comment.