Sentry integration for ASGI frameworks.
Switch branches/tags
Clone or download
Latest commit 8654c61 Nov 9, 2018


Build Status Coverage Package version

Sentry integration for ASGI frameworks.


pip install sentry-asgi


from sentry_asgi import SentryMiddleware
import sentry_sdk


app = ...
app = SentryMiddleware(app)

Here's a more complete example, using Starlette:

import sentry_sdk
from sentry_asgi import SentryMiddleware
from sentry_asgi.executor import ContextPreservingExecutor  # Python 3.7+


app = Starlette()

def homepage(request):
    raise ValueError("nope")

async def setup_executor():
    executor = ContextPreservingExecutor()
    loop = asyncio.get_event_loop()



Python version support

The Sentry SDK requires Python 3.7's contextvars support in order to properly tie messages and logging back to the request context that is added SentryMiddleware.

On 3.6 and below the SentryMiddleware will capture and log application exceptions just fine, but will not properly tie in logging, messages, or breadcrumbs for any code that runs within a threadpool executor or subtask.

ASGI frameworks should ensure that any thread pool executors preserve the contextvar context.

Endpoint information

It is recommended that frameworks populate an "endpoint" key in the ASGI scope, to indicate which view function or class should be logged by the middleware.