Install the sentry Python SDK:
pip install sentry-sdk
Initialize the SDK and wrap your ASGI app with SentryAsgiMiddleware:
import sentry_sdk
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
sentry_sdk.init(
dsn="YOUR_DSN",
traces_sample_rate=0.01,
auto_session_tracking=False,
)
async def app(scope, receive, send):
# Your ASGI application
pass
app = SentryAsgiMiddleware(app)
The ASGI middleware captures unhandled exceptions and request data from any ASGI-compatible framework.