Skip to content

Prevent Falcon integration from breaking ASGI apps #2359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 12, 2023
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
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.")