mcpField
S
sentry-python-setup

sentry-python-setup

github.com/getsentryDevelopment25122 installsUpdated 35w ago
Repository

About sentry-python-setup

A setup skill for adding Sentry to Python projects, aimed at developers configuring error monitoring, logs, or tracing. It covers common frameworks, async applications, configuration, and verification.

Key features

  1. 01Install sentry-sdk
  2. 02Initialize and configure Sentry
  3. 03Cover Django, Flask, FastAPI, and more
  4. 04Set environment variables
  5. 05Verify events and troubleshoot

Use cases

  1. 01Add error monitoring to a Python app
  2. 02Configure Sentry in an async app
  3. 03Troubleshoot missing events or traces

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/getsentry/sentry-agent-skills --skill sentry-python-setup

After installing, type/sentry-python-setupin Claude Code to confirm it loaded.

Details

From the project README

Sentry Python Setup

Install and configure Sentry in Python projects.

Invoke This Skill When

  • User asks to "add Sentry to Python" or "install Sentry" in a Python app
  • User wants error monitoring, logging, or tracing in Python
  • User mentions "sentry-sdk" or Python frameworks (Django, Flask, FastAPI)

Important: The configuration options and code samples below are examples. Always verify against docs.sentry.io before implementing, as APIs and defaults may have changed.

Install

pip install sentry-sdk

Configure

Initialize as early as possible in your application:

import sentry_sdk

sentry_sdk.init(
    dsn="YOUR_SENTRY_DSN",
    send_default_pii=True,
    
    # Tracing
    traces_sample_rate=1.0,
    
    # Profiling
    profile_session_sample_rate=1.0,
    profile_lifecycle="trace",
    
    # Logs
    enable_logs=True,
)
Async Applications

For async apps, initialize inside an async function:

import asyncio
import sentry_sdk

async def main():
    sentry_sdk.init(
        dsn="YOUR_SENTRY_DSN",
        send_default_pii=True,
        traces_sample_rate=1.0,
        enable_logs=True,
    )
    # ... rest of app

asyncio.run(main())

Framework Integrations

Use the same sentry_sdk.init() call shown above. Place it where it runs before your app starts:

FrameworkWhere to InitNotes
DjangoTop of settings.pyAuto-detects Django, no extra install
FlaskBefore app = Flask(__name__)Auto-detects Flask
FastAPIBefore app = FastAPI()Auto-detects FastAPI
CeleryIn Celery worker configAuto-detects Celery
AIOHTTPBefore app creationAuto-detects AIOHTTP

Configuration Options

OptionDescriptionDefaultMin SDK
dsnSentry DSNNone (SDK no-ops without it)—
send_default_piiInclude user dataNone—
traces_sample_rate% of transactions tracedNone (tracing disabled)—
profile_session_sample_rate% of sessions profiledNone (profiling disabled)2.24.1+
profile_lifecycleProfiling mode ("trace" or "manual")"manual"2.24.1+
enable_logsSend logs to SentryFalse2.35.0+
environmentEnvironment name"production" (or SENTRY_ENVIRONMENT env var)—
releaseRelease versionAuto-detected—

Environment Variables

The SDK auto-reads these:

SENTRY_DSN=https://[email protected]/456
SENTRY_ENVIRONMENT=production
SENTRY_RELEASE=1.0.0

For sentry-cli (source maps, releases), also set:

SENTRY_AUTH_TOKEN=sntrys_xxx
SENTRY_ORG=my-org
SENTRY_PROJECT=my-project

Or pass DSN in code:

import os
import sentry_sdk

sentry_sdk.init(
    dsn=os.environ.get("SENTRY_DSN"),
    # ...
)

Verification

# Intentional error to test
division_by_zero = 1 / 0

Or capture manually:

sentry_sdk.capture_message("Test message from Python")

Troubleshooting

IssueSolution
Errors not appearingEnsure init() is called early, check DSN
No tracesSet traces_sample_rate > 0
IPython errors not capturedRun from file, not interactive shell
Async errors missingInitialize inside async function

Comments

Markdown supported