Docs · Python

SDKs

Python

Bugzor works with the open-source sentry-sdk package. Tested with sentry-sdk 2.75 on Python 3.12.

Install#

Terminal
pip install --upgrade sentry-sdk

Configure#

Initialize the SDK as early as possible, before your app is created:

Python
import sentry_sdk

sentry_sdk.init(
    dsn="https://<public-key>@www.bugzor.com/<project-id>",
    environment="production",
    release="my-app@1.0.0",
    # Send the user's IP and request data (needed for "users affected").
    send_default_pii=True,
)

The SDK detects Django, Flask, FastAPI, Starlette, Celery, RQ, SQLAlchemy and more, and reports their unhandled exceptions with the request and breadcrumbs. You don’t have to list the integrations.

For Django, put sentry_sdk.init(...) at the top of settings.py. For FastAPI or Flask, call it before creating the app.

Capture errors and messages#

Python
try:
    import_orders()
except Exception as e:
    sentry_sdk.capture_exception(e)

sentry_sdk.capture_message("Payment provider responded slowly", level="warning")

Chained exceptions (raise OrderImportError(...) from e) show every cause, and the raised exception names the issue.

Add context#

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

with sentry_sdk.new_scope() as scope:
    scope.set_tag("component", "checkout")
    scope.set_context("cart", {"items": 3, "total_cents": 4599})
    sentry_sdk.capture_message("Cart total mismatch")

sentry_sdk.add_breadcrumb(category="auth", message="Signed in", level="info")

Filter events#

Python
def before_send(event, hint):
    if event.get("tags", {}).get("healthcheck"):
        return None  # dropped: never sent, never counted
    return event

sentry_sdk.init(dsn="...", before_send=before_send)

Scripts, jobs and serverless#

The SDK sends from a background thread and flushes at interpreter exit. If your process can be killed before that (AWS Lambda, os._exit, some job runners), flush explicitly:

Python
sentry_sdk.flush(timeout=2)

Test it#

Terminal
SENTRY_DSN="https://<public-key>@www.bugzor.com/<project-id>" \
  python -c 'import sentry_sdk; sentry_sdk.init(); sentry_sdk.capture_message("Hello from Python")'

sentry_sdk.init() with no dsn reads SENTRY_DSN. Turn on debug=True to see each request and its response code.

Something unclear or out of date? Tell us.