sentry-python-setup by getsentry/sentry-agent-skills
npx skills add https://github.com/getsentry/sentry-agent-skills --skill sentry-python-setup在 Python 项目中安装和配置 Sentry。
重要提示: 下面的配置选项和代码示例仅供参考。在实施前,请务必查阅 docs.sentry.io 进行核实,因为 API 和默认值可能已发生变化。
pip install sentry-sdk
在您的应用程序中尽可能早地初始化:
import sentry_sdk
sentry_sdk.init(
dsn="YOUR_SENTRY_DSN",
send_default_pii=True,
# 追踪
traces_sample_rate=1.0,
# 性能剖析
profile_session_sample_rate=1.0,
profile_lifecycle="trace",
# 日志
enable_logs=True,
)
对于异步应用,请在异步函数内部初始化:
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,
)
# ... 应用程序其余部分
asyncio.run(main())
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
使用上面所示的相同 sentry_sdk.init() 调用。将其放置在应用程序启动前运行的位置:
| 框架 | 初始化位置 | 备注 |
|---|---|---|
| Django | settings.py 文件顶部 | 自动检测 Django,无需额外安装 |
| Flask | 在 app = Flask(__name__) 之前 | 自动检测 Flask |
| FastAPI | 在 app = FastAPI() 之前 | 自动检测 FastAPI |
| Celery | 在 Celery worker 配置中 | 自动检测 Celery |
| AIOHTTP | 在应用创建之前 | 自动检测 AIOHTTP |
| 选项 | 描述 | 默认值 | 最低 SDK 版本 |
|---|---|---|---|
dsn | Sentry DSN | None (没有它 SDK 将不执行操作) | — |
send_default_pii | 包含用户数据 | None | — |
traces_sample_rate | 被追踪事务的百分比 | None (追踪已禁用) | — |
profile_session_sample_rate | 被剖析会话的百分比 | None (性能剖析已禁用) | 2.24.1+ |
profile_lifecycle | 性能剖析模式 ("trace" 或 "manual") | "manual" | 2.24.1+ |
enable_logs | 将日志发送到 Sentry | False | 2.35.0+ |
environment | 环境名称 | "production" (或 SENTRY_ENVIRONMENT 环境变量) | — |
release | 发布版本 | 自动检测 | — |
SDK 会自动读取以下变量:
SENTRY_DSN=https://xxx@o123.ingest.sentry.io/456
SENTRY_ENVIRONMENT=production
SENTRY_RELEASE=1.0.0
对于 sentry-cli(源映射、发布),还需设置:
SENTRY_AUTH_TOKEN=sntrys_xxx
SENTRY_ORG=my-org
SENTRY_PROJECT=my-project
或者在代码中传递 DSN:
import os
import sentry_sdk
sentry_sdk.init(
dsn=os.environ.get("SENTRY_DSN"),
# ...
)
# 故意制造错误以进行测试
division_by_zero = 1 / 0
或者手动捕获:
sentry_sdk.capture_message("来自 Python 的测试消息")
| 问题 | 解决方案 |
|---|---|
| 错误未出现 | 确保 init() 被尽早调用,检查 DSN |
| 没有追踪数据 | 设置 traces_sample_rate > 0 |
| IPython 错误未被捕获 | 从文件运行,而非交互式 shell |
| 异步错误缺失 | 在异步函数内部初始化 |
每周安装量
102
仓库
GitHub 星标数
19
首次出现
2026 年 1 月 21 日
安全审计
安装于
opencode85
codex82
claude-code82
gemini-cli78
github-copilot73
cursor72
Install and configure Sentry in Python projects.
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.
pip install sentry-sdk
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,
)
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())
Use the same sentry_sdk.init() call shown above. Place it where it runs before your app starts:
| Framework | Where to Init | Notes |
|---|---|---|
| Django | Top of settings.py | Auto-detects Django, no extra install |
| Flask | Before app = Flask(__name__) | Auto-detects Flask |
| FastAPI | Before app = FastAPI() | Auto-detects FastAPI |
| Celery | In Celery worker config | Auto-detects Celery |
| AIOHTTP | Before app creation | Auto-detects AIOHTTP |
| Option | Description | Default | Min SDK |
|---|---|---|---|
dsn | Sentry DSN | None (SDK no-ops without it) | — |
send_default_pii | Include user data | None | — |
traces_sample_rate | % of transactions traced | None (tracing disabled) |
The SDK auto-reads these:
SENTRY_DSN=https://xxx@o123.ingest.sentry.io/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"),
# ...
)
# Intentional error to test
division_by_zero = 1 / 0
Or capture manually:
sentry_sdk.capture_message("Test message from Python")
| Issue | Solution |
|---|---|
| Errors not appearing | Ensure init() is called early, check DSN |
| No traces | Set traces_sample_rate > 0 |
| IPython errors not captured | Run from file, not interactive shell |
| Async errors missing | Initialize inside async function |
Weekly Installs
102
Repository
GitHub Stars
19
First Seen
Jan 21, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode85
codex82
claude-code82
gemini-cli78
github-copilot73
cursor72
| — |
profile_session_sample_rate | % of sessions profiled | None (profiling disabled) | 2.24.1+ |
profile_lifecycle | Profiling mode ("trace" or "manual") | "manual" | 2.24.1+ |
enable_logs | Send logs to Sentry | False | 2.35.0+ |
environment | Environment name | "production" (or SENTRY_ENVIRONMENT env var) | — |
release | Release version | Auto-detected | — |