Loading...
Loading...
Full Sentry SDK setup for Python. Use when asked to "add Sentry to Python", "install sentry-sdk", "setup Sentry in Python", or configure error monitoring, tracing, profiling, logging, metrics, crons, or AI monitoring for Python applications. Supports Django, Flask, FastAPI, Celery, Starlette, AIOHTTP, Tornado, and more.
npx skill4agent add getsentry/sentry-agent-skills sentry-python-sdksentry-sdksentry_sdkNote: SDK versions and APIs below reflect Sentry docs at time of writing (sentry-sdk 2.x). Always verify against docs.sentry.io/platforms/python/ before implementing.
# Check existing Sentry
grep -i sentry requirements.txt pyproject.toml setup.cfg setup.py 2>/dev/null
# Detect web framework
grep -rE "django|flask|fastapi|starlette|aiohttp|tornado|quart|falcon|sanic|bottle" \
requirements.txt pyproject.toml 2>/dev/null
# Detect task queues
grep -rE "celery|rq|huey|arq|dramatiq" requirements.txt pyproject.toml 2>/dev/null
# Detect logging libraries
grep -E "loguru" requirements.txt pyproject.toml 2>/dev/null
# Detect AI libraries
grep -rE "openai|anthropic|langchain|huggingface|google-genai|pydantic-ai|litellm" \
requirements.txt pyproject.toml 2>/dev/null
# Detect schedulers / crons
grep -rE "celery|apscheduler|schedule|crontab" requirements.txt pyproject.toml 2>/dev/null
# Check for companion frontend
ls frontend/ web/ client/ ui/ static/ templates/ 2>/dev/nullsentry-sdksentry_sdk.init()sentry_sdk.init()ExceptionGrouplogging| Feature | Recommend when... | Reference |
|---|---|---|
| Error Monitoring | Always — non-negotiable baseline | |
| Tracing | Django/Flask/FastAPI/AIOHTTP/etc. detected | |
| Profiling | Production + performance-sensitive workload | |
| Logging | Always (stdlib); enhanced for Loguru | |
| Metrics | Business events or SLO tracking needed | |
| Crons | Celery Beat, APScheduler, or cron patterns | |
| AI Monitoring | OpenAI/Anthropic/LangChain/etc. detected | |
# Core SDK (always required)
pip install sentry-sdk
# Optional extras (install only what matches detected framework):
pip install "sentry-sdk[django]"
pip install "sentry-sdk[flask]"
pip install "sentry-sdk[fastapi]"
pip install "sentry-sdk[celery]"
pip install "sentry-sdk[aiohttp]"
pip install "sentry-sdk[tornado]"
# Multiple extras:
pip install "sentry-sdk[django,celery]"Extras are optional — plainworks for all frameworks. Extras install complementary packages.sentry-sdk
import sentry_sdk
sentry_sdk.init(
dsn=os.environ["SENTRY_DSN"],
environment=os.environ.get("SENTRY_ENVIRONMENT", "production"),
release=os.environ.get("SENTRY_RELEASE"), # e.g. "myapp@1.0.0"
send_default_pii=True,
# Tracing (lower to 0.1–0.2 in high-traffic production)
traces_sample_rate=1.0,
# Profiling — continuous, tied to active spans
profile_session_sample_rate=1.0,
profile_lifecycle="trace",
# Structured logs (SDK ≥ 2.35.0)
enable_logs=True,
)| Framework | Where to call | Notes |
|---|---|---|
| Django | Top of | No middleware needed — Sentry patches Django internally |
| Flask | Before | Must precede app creation |
| FastAPI | Before | |
| Starlette | Before | Same auto-integration as FastAPI |
| AIOHTTP | Module level, before | |
| Tornado | Module level, before app setup | No integration class needed |
| Quart | Before | |
| Falcon | Module level, before | |
| Sanic | Inside | Sanic's lifecycle requires async init |
| Celery | | Dual-process init required |
| RQ | | |
| ARQ | Both worker module and enqueuing process |
settings.pyimport sentry_sdk
sentry_sdk.init(
dsn=os.environ["SENTRY_DSN"],
send_default_pii=True,
traces_sample_rate=1.0,
profile_session_sample_rate=1.0,
profile_lifecycle="trace",
enable_logs=True,
)
# rest of Django settings...
INSTALLED_APPS = [...]main.pyimport sentry_sdk
sentry_sdk.init(
dsn=os.environ["SENTRY_DSN"],
send_default_pii=True,
traces_sample_rate=1.0,
profile_session_sample_rate=1.0,
profile_lifecycle="trace",
enable_logs=True,
)
from fastapi import FastAPI
app = FastAPI()integrations=[...]| Auto-enabled | Explicit required |
|---|---|
| Django, Flask, FastAPI, Starlette, AIOHTTP, Tornado, Quart, Falcon, Sanic, Bottle | |
| Celery, RQ, Huey, ARQ | |
| SQLAlchemy, Redis, asyncpg, pymongo | |
| Requests, HTTPX, aiohttp-client | |
| OpenAI, Anthropic, LangChain, Pydantic AI, MCP | |
Python | |
| Feature | Reference file | Load when... |
|---|---|---|
| Error Monitoring | | Always (baseline) |
| Tracing | | HTTP handlers / distributed tracing |
| Profiling | | Performance-sensitive production |
| Logging | | Always; enhanced for Loguru |
| Metrics | | Business KPIs / SLO tracking |
| Crons | | Scheduler / cron patterns detected |
| AI Monitoring | | AI library detected |
Read ${SKILL_ROOT}/references/<feature>.mdsentry_sdk.init()| Option | Type | Default | Purpose |
|---|---|---|---|
| | | SDK disabled if empty; env: |
| | | e.g., |
| | | e.g., |
| | | Include IP, headers, cookies, auth user |
| | | Transaction sample rate; |
| | | Custom per-transaction sampling (overrides rate) |
| | | Continuous profiling session rate |
| | | |
| | | Transaction-based profiling rate |
| | | Send logs to Sentry (SDK ≥ 2.35.0) |
| | | Error event sample rate |
| | | Stack traces on |
| | | Max breadcrumbs per event |
| | | Verbose SDK debug output |
| | | Hook to mutate/drop error events |
| | | Hook to mutate/drop transaction events |
| | | Exception types or regex patterns to suppress |
| | | Set |
| Variable | Maps to | Notes |
|---|---|---|
| | |
| | Also auto-detected from git SHA, Heroku, CircleCI, CodeBuild, GAE |
| | |
| |
# Trigger a real error event — check dashboard within seconds
division_by_zero = 1 / 0sentry_sdk.capture_message("Sentry Python SDK test")debug=Truesentry_sdk.init()SENTRY_DSNls frontend/ web/ client/ ui/ 2>/dev/null
cat frontend/package.json web/package.json client/package.json 2>/dev/null \
| grep -E '"react"|"svelte"|"vue"|"next"|"nuxt"'| Frontend detected | Suggest skill |
|---|---|
| React / Next.js | |
| Svelte / SvelteKit | |
| Vue / Nuxt | Use |
| Other JS/TS | |
| Issue | Solution |
|---|---|
| Events not appearing | Set |
| Malformed DSN error | Format: |
| Django exceptions not captured | Ensure |
| Flask exceptions not captured | Init must happen before |
| FastAPI exceptions not captured | Init before |
| Celery task errors not captured | Must call |
| Sanic init not working | Init must be inside |
| uWSGI not capturing | Add |
| No traces appearing | Verify |
| Profiling not starting | Requires |
| Requires SDK ≥ 2.35.0; for direct structured logs use |
| Too many transactions | Lower |
| Cross-request data leaking | Don't use |
| RQ worker not reporting | Pass |