Skip to main content

Sentry integration for ASGI frameworks.

Project description

sentry-asgi

Build Status Coverage Package version

Sentry integration for ASGI frameworks.

Installation:

pip install sentry-asgi

Usage:

from sentry_asgi import SentryMiddleware
import sentry_sdk


sentry_sdk.init(dsn=...)

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+

sentry_sdk.init(dsn=...)

app = Starlette()

@app.route("/")
def homepage(request):
    raise ValueError("nope")

@app.on_event("startup")
async def setup_executor():
    executor = ContextPreservingExecutor()
    loop = asyncio.get_event_loop()
    loop.set_default_executor(executor)

app.add_middleware(SentryMiddleware)

Notes

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.

For further context see here.

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sentry-asgi-0.1.4.tar.gz (3.6 kB view details)

Uploaded Source

File details

Details for the file sentry-asgi-0.1.4.tar.gz.

File metadata

  • Download URL: sentry-asgi-0.1.4.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.0

File hashes

Hashes for sentry-asgi-0.1.4.tar.gz
Algorithm Hash digest
SHA256 087489da6e3aa37ed926606d28cf68f20f11ad8b7aa0744f32816e39ce61d5b6
MD5 b4937636a68e815cb1dffc745473e987
BLAKE2b-256 e491faa2ec11931c8afd9f0cb9191c6cbb51c1c18b697a47ae740615a64445bc

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page