Skip to content

Commit 1f9f999

Browse files
authored
Auto-enable Starlette and FastAPI (#1533)
* Auto enable Starlette/FastAPI * Raise error when SentryASGIMIddleware is used manually in combination with Starlette/FastAPI. If you use Starlette/FastAPI you do not need to use SentryASGIMIddleware anymore, the SDK is setting up everything automatically. * Fixed Starlette/FastAPI tests. * Deactivated ASGI middleware tests, because they need to be rewritten without Starlette.
1 parent fa4f5b0 commit 1f9f999

File tree

8 files changed

+46
-477
lines changed

8 files changed

+46
-477
lines changed

sentry_sdk/integrations/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ def iter_default_integrations(with_auto_enabling_integrations):
5454
_AUTO_ENABLING_INTEGRATIONS = (
5555
"sentry_sdk.integrations.django.DjangoIntegration",
5656
"sentry_sdk.integrations.flask.FlaskIntegration",
57+
"sentry_sdk.integrations.starlette.StarletteIntegration",
58+
"sentry_sdk.integrations.fastapi.FastApiIntegration",
5759
"sentry_sdk.integrations.bottle.BottleIntegration",
5860
"sentry_sdk.integrations.falcon.FalconIntegration",
5961
"sentry_sdk.integrations.sanic.SanicIntegration",

sentry_sdk/integrations/asgi.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from sentry_sdk._types import MYPY
1313
from sentry_sdk.hub import Hub, _should_send_default_pii
1414
from sentry_sdk.integrations._wsgi_common import _filter_headers
15+
from sentry_sdk.integrations.modules import _get_installed_modules
1516
from sentry_sdk.sessions import auto_session_tracking
1617
from sentry_sdk.tracing import (
1718
SOURCE_FOR_STYLE,
@@ -91,7 +92,6 @@ def __init__(
9192
9293
:param unsafe_context_data: Disable errors when a proper contextvars installation could not be found. We do not recommend changing this from the default.
9394
"""
94-
9595
if not unsafe_context_data and not HAS_REAL_CONTEXTVARS:
9696
# We better have contextvars or we're going to leak state between
9797
# requests.
@@ -108,6 +108,16 @@ def __init__(
108108
self.mechanism_type = mechanism_type
109109
self.app = app
110110

111+
asgi_middleware_while_using_starlette_or_fastapi = (
112+
"starlette" in _get_installed_modules() and self.mechanism_type == "asgi"
113+
)
114+
if asgi_middleware_while_using_starlette_or_fastapi:
115+
raise RuntimeError(
116+
"The Sentry Python SDK can now automatically support ASGI frameworks like Starlette and FastAPI. "
117+
"Please remove 'SentryAsgiMiddleware' from your project. "
118+
"See https://docs.sentry.io/platforms/python/guides/asgi/ for more information."
119+
)
120+
111121
if _looks_like_asgi3(app):
112122
self.__call__ = self._run_asgi3 # type: Callable[..., Any]
113123
else:

tests/integrations/asgi/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +0,0 @@
1-
import pytest
2-
3-
pytest.importorskip("starlette")

0 commit comments

Comments
 (0)