Skip to main content

Python

Install​

pip install "sentry-sdk[django]" # or [flask], [fastapi], [celery], or plain sentry-sdk

Initialize​

import sentry_sdk

sentry_sdk.init(
dsn=os.environ["SENTRY_DSN"],
environment=os.environ.get("ENV", "production"),
release=os.environ.get("APP_VERSION"),

traces_sample_rate=0.2, # tracing
profiles_sample_rate=1.0, # profiling (of sampled transactions)
enable_logs=True, # structured logs

send_default_pii=False,
attach_stacktrace=True,
)

The SDK auto-detects Django, Flask, FastAPI, Celery, Redis, SQLAlchemy, requests and more. Unhandled exceptions are captured with no further code.

Django​

Call sentry_sdk.init() at the bottom of settings.py (or in wsgi.py/asgi.py before the application object is built). Nothing else is required — middleware, view errors, ORM spans and template spans are instrumented automatically.

Celery​

Initialize inside a celeryd_init/worker_process_init signal handler so every forked worker gets its own SDK client:

from celery.signals import celeryd_init

@celeryd_init.connect
def init_sentry(**_kwargs):
sentry_sdk.init(dsn=..., traces_sample_rate=0.2)

Capturing manually​

with sentry_sdk.push_scope() as scope:
scope.set_tag("area", "billing")
scope.set_context("invoice", {"id": invoice.id, "total": invoice.total})
sentry_sdk.capture_exception(exc)

sentry_sdk.capture_message("Reconciliation drifted", level="warning")
sentry_sdk.set_user({"id": user.id, "username": user.username})

Logs​

import logging

logger = logging.getLogger(__name__)
logger.warning("retrying payment", extra={"order_id": order.id, "attempt": 3})

With enable_logs=True the SDK forwards standard-library log records to Errora as structured logs, keeping extra fields as typed attributes and tagging them with the active trace. See Logs.

Cron monitors​

from sentry_sdk.crons import monitor

@monitor(monitor_slug="nightly-invoices")
def nightly_invoices():
...

With Celery beat, decorate the task or use sentry_sdk.crons.capture_checkin directly. See Cron monitoring.

Metrics​

sentry_sdk.metrics.incr("orders.created", 1, tags={"plan": "pro"})
sentry_sdk.metrics.distribution("orders.total_toman", order.total, unit="none")

See Metrics.

AI monitoring​

If you use the OpenAI, Anthropic or LangChain integrations, the SDK emits gen_ai.* spans that Errora projects into AI monitoring — agent runs, per-model token usage and tool calls — with no extra configuration beyond traces_sample_rate.

Verify​

sentry_sdk.capture_exception(Exception("Errora smoke test"))