ASGI
The ASGI middleware can be used to instrument any ASGI-compatible web framework to attach request data for your events.
pip install --upgrade 'sentry-sdk'
import sentry_sdk
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
from my_asgi_app import app
sentry_sdk.init(
dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
enable_tracing=True
)
app = SentryAsgiMiddleware(app)
The middleware supports both ASGI 2 and ASGI 3 transparently.
Trigger an error in your code and see it show up in sentry.io.
sentry_sdk.init(...) # same as above
def app(scope):
async def get_body():
return f"The number is: {1/0}" # raises an error!
async def asgi(receive, send):
await send(
{
"type": "http.response.start",
"status": 200,
"headers": [[b"content-type", b"text/plain"]],
}
)
await send({"type": "http.response.body", "body": await get_body()})
return asgi
app = SentryAsgiMiddleware(app)
Run your ASGI app with unicorn:
uvicorn main:app --port 8000
Point your browser to http://localhost:8000 to trigger the error which is then sent to Sentry.
Additionally, a transaction will show up in the "Performance" section on sentry.io.
Request data is attached to all events: HTTP method, URL, headers. Sentry excludes raw bodies and multipart file uploads. Sentry also excludes personally identifiable information (such as user ids, usernames, cookies, authorization headers, IP addresses) unless you set
send_default_pii
toTrue
.Each request has a separate scope. Changes to the scope within a view, for example setting a tag, will only apply to events sent as part of the request being handled.
The ASGI middleware does not behave like a regular integration. It is not initialized through an extra parameter to
init
and is not attached to a client. When capturing or supplementing events, it just uses the currently active hub.
- Python: 3.7+
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").
- Package:
- pypi:sentry-sdk
- Version:
- 1.45.0
- Repository:
- https://github.com/getsentry/sentry-python
- API Documentation:
- https://getsentry.github.io/sentry-python/