Guides

Python

For Django, Flask, FastAPI, Celery, RQ and plain scripts, use the official sentry-sdk package. It finds the frameworks you use on its own, so setup is one call.

1. Install the SDK

Terminal
pip install sentry-sdk

2. Initialize it at startup

Call sentry_sdk.init as early as you can: in Django's settings.py, or before you create your Flask or FastAPI app.

Python
import os
import sentry_sdk

sentry_sdk.init(
    dsn=os.environ["SENTRY_DSN"],  # https://<key>@uncaught.thistinct.tools/<project-id>
    environment=os.environ.get("APP_ENV", "production"),
    release=os.environ.get("GIT_SHA"),
    send_default_pii=True,  # include the user and request headers
    traces_sample_rate=0.1,  # time one in ten requests for Performance
)

Errors in Django views, Flask and FastAPI routes, and Celery tasks are reported from then on, with each frame's local variables. Turn those off with include_local_variables=False if they might hold secrets.

3. Catch exceptions

Unhandled exceptions are reported on their own. Anywhere else, report what you catch:

Python
try:
    sync_inventory()
except Exception:
    sentry_sdk.capture_exception()
    raise

Add context

Python
sentry_sdk.set_user({"id": user.id, "email": user.email})
sentry_sdk.set_tag("queue", "imports")

The SDK compresses events with brotli when the brotli package is installed, and with gzip otherwise. Uncaught reads both.