Repository: getsentry/sentry-python

Issue:
sentry-sdk stopped working with falcon 3+ asgi app
### How do you use Sentry?

Sentry Saas (sentry.io)

### Version

1.16.0

### Steps to Reproduce

I created a test repo where the bug can be reproduced -> https://github.com/detoyz/sentry_falcon_asgi_bug . There are also details on how to setup project & reproduce the error.

However a small summary here:
1. Install sentry-sdk 1.16.0
2. Install falcon
3. Init `falcon.asgi.App()` and try to use it via [falcon-inspect-app](https://falcon.readthedocs.io/en/stable/api/inspect.html)
4. **See error from sentry's falcon patching middleware**
5. Downgrade sentry sdk to 1.15.0
6. Your `falcon.asgi.App()` and `falcon-inspect-app` are working as expected


### Expected Result

By using sentry-sdk anywhere in the code it should not break during runtime because of any `falcon.asgi.App()` isntances

### Actual Result

Runtime errors while using `falcon.asgi.App()` isntances with sentry-sdk client
```
TypeError: _patch_prepare_middleware.<locals>.sentry_patched_prepare_middleware() takes from 0 to 2 positional arguments but 3 were given
```

Discussion:

Patch:
diff --git a/sentry_sdk/integrations/falcon.py b/sentry_sdk/integrations/falcon.py
index 1bb79428..9b3cc40c 100644
--- a/sentry_sdk/integrations/falcon.py
+++ b/sentry_sdk/integrations/falcon.py
@@ -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


Test patch:
diff --git a/tests/integrations/falcon/test_falcon.py b/tests/integrations/falcon/test_falcon.py
index dd7aa80d..764b81f1 100644
--- a/tests/integrations/falcon/test_falcon.py
+++ b/tests/integrations/falcon/test_falcon.py
@@ -13,6 +13,14 @@ from sentry_sdk.integrations.falcon import FalconIntegration
 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 test_does_not_leak_scope(sentry_init, capture_events):
 
     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.")