From 52ec0df580263748f667e074566e582abe098c8e Mon Sep 17 00:00:00 2001 From: Daniel Szoke Date: Fri, 8 Sep 2023 13:37:00 +0200 Subject: [PATCH 1/4] Prevent Falcon integration from breaking ASGI apps --- sentry_sdk/integrations/falcon.py | 14 ++++++++---- tests/integrations/falcon/test_falcon.py | 29 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/sentry_sdk/integrations/falcon.py b/sentry_sdk/integrations/falcon.py index 1bb79428f1..80df81d5bc 100644 --- a/sentry_sdk/integrations/falcon.py +++ b/sentry_sdk/integrations/falcon.py @@ -206,14 +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, + *args, # args and kwargs for future compatibility + **kwargs, ): - # type: (Any, Any) -> Any + # type: (Any, Any, bool, *Any, **Any) -> Any hub = Hub.current integration = hub.get_integration(FalconIntegration) - if integration is not None: + if integration is not None and not asgi: middleware = [SentryFalconMiddleware()] + (middleware or []) - return original_prepare_middleware(middleware, independent_middleware) + return original_prepare_middleware( + middleware, independent_middleware, asgi, *args, **kwargs + ) falcon_helpers.prepare_middleware = sentry_patched_prepare_middleware diff --git a/tests/integrations/falcon/test_falcon.py b/tests/integrations/falcon/test_falcon.py index dd7aa80dfe..764b81f172 100644 --- a/tests/integrations/falcon/test_falcon.py +++ b/tests/integrations/falcon/test_falcon.py @@ -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(): @@ -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.") From b2b10cd5a8a279cebe9e0ff51956b1f1970e9aa6 Mon Sep 17 00:00:00 2001 From: Daniel Szoke Date: Fri, 8 Sep 2023 14:36:12 +0200 Subject: [PATCH 2/4] Remove trailing comma --- sentry_sdk/integrations/falcon.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sentry_sdk/integrations/falcon.py b/sentry_sdk/integrations/falcon.py index 80df81d5bc..5ff9667db2 100644 --- a/sentry_sdk/integrations/falcon.py +++ b/sentry_sdk/integrations/falcon.py @@ -210,7 +210,7 @@ def sentry_patched_prepare_middleware( independent_middleware=False, asgi=False, *args, # args and kwargs for future compatibility - **kwargs, + **kwargs ): # type: (Any, Any, bool, *Any, **Any) -> Any hub = Hub.current From 34a59d6fba43cc7ceba127d1f23ed277155ec919 Mon Sep 17 00:00:00 2001 From: Daniel Szoke Date: Fri, 8 Sep 2023 14:58:29 +0200 Subject: [PATCH 3/4] Remain backwards compatible with older Falcon --- sentry_sdk/integrations/falcon.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/sentry_sdk/integrations/falcon.py b/sentry_sdk/integrations/falcon.py index 5ff9667db2..ae036da5a9 100644 --- a/sentry_sdk/integrations/falcon.py +++ b/sentry_sdk/integrations/falcon.py @@ -213,12 +213,21 @@ def sentry_patched_prepare_middleware( **kwargs ): # type: (Any, Any, bool, *Any, **Any) -> 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, *args, **kwargs + ) + hub = Hub.current integration = hub.get_integration(FalconIntegration) - if integration is not None and not asgi: + 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, asgi, *args, **kwargs + middleware, independent_middleware, *args, **kwargs ) falcon_helpers.prepare_middleware = sentry_patched_prepare_middleware From 3eaabcba8637b26ce0a1cbea9b31271cd95eb13d Mon Sep 17 00:00:00 2001 From: Daniel Szoke Date: Mon, 11 Sep 2023 10:23:51 +0200 Subject: [PATCH 4/4] Remove args and kwargs --- sentry_sdk/integrations/falcon.py | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/sentry_sdk/integrations/falcon.py b/sentry_sdk/integrations/falcon.py index ae036da5a9..9b3cc40cd6 100644 --- a/sentry_sdk/integrations/falcon.py +++ b/sentry_sdk/integrations/falcon.py @@ -206,18 +206,12 @@ def _patch_prepare_middleware(): original_prepare_middleware = falcon_helpers.prepare_middleware def sentry_patched_prepare_middleware( - middleware=None, - independent_middleware=False, - asgi=False, - *args, # args and kwargs for future compatibility - **kwargs + middleware=None, independent_middleware=False, asgi=False ): - # type: (Any, Any, bool, *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, *args, **kwargs - ) + return original_prepare_middleware(middleware, independent_middleware, asgi) hub = Hub.current integration = hub.get_integration(FalconIntegration) @@ -226,9 +220,7 @@ def sentry_patched_prepare_middleware( # 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, *args, **kwargs - ) + return original_prepare_middleware(middleware, independent_middleware) falcon_helpers.prepare_middleware = sentry_patched_prepare_middleware