sentry-python-setup

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Sentry Python Setup

Sentry Python 配置指南

Install and configure Sentry in Python projects.
在Python项目中安装并配置Sentry。

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)
  • 用户要求“在Python中添加Sentry”或在Python应用中“安装Sentry”
  • 用户需要在Python中实现错误监控、日志记录或追踪功能
  • 用户提到“sentry-sdk”或Python框架(Django、Flask、FastAPI)

Install

安装

bash
pip install sentry-sdk
bash
pip install sentry-sdk

Configure

配置

Initialize as early as possible in your application:
python
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,
)
在应用尽可能早的位置初始化:
python
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,
)

Async Applications

异步应用

For async apps, initialize inside an async function:
python
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())
对于异步应用,需在异步函数内初始化:
python
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())

Framework Integrations

框架集成

Django

Django

python
undefined
python
undefined

settings.py

settings.py

import sentry_sdk
sentry_sdk.init( dsn="YOUR_SENTRY_DSN", send_default_pii=True, traces_sample_rate=1.0, enable_logs=True, )
undefined
import sentry_sdk
sentry_sdk.init( dsn="YOUR_SENTRY_DSN", send_default_pii=True, traces_sample_rate=1.0, enable_logs=True, )
undefined

Flask

Flask

python
from flask import Flask
import sentry_sdk

sentry_sdk.init(
    dsn="YOUR_SENTRY_DSN",
    send_default_pii=True,
    traces_sample_rate=1.0,
    enable_logs=True,
)

app = Flask(__name__)
python
from flask import Flask
import sentry_sdk

sentry_sdk.init(
    dsn="YOUR_SENTRY_DSN",
    send_default_pii=True,
    traces_sample_rate=1.0,
    enable_logs=True,
)

app = Flask(__name__)

FastAPI

FastAPI

python
from fastapi import FastAPI
import sentry_sdk

sentry_sdk.init(
    dsn="YOUR_SENTRY_DSN",
    send_default_pii=True,
    traces_sample_rate=1.0,
    enable_logs=True,
)

app = FastAPI()
python
from fastapi import FastAPI
import sentry_sdk

sentry_sdk.init(
    dsn="YOUR_SENTRY_DSN",
    send_default_pii=True,
    traces_sample_rate=1.0,
    enable_logs=True,
)

app = FastAPI()

Configuration Options

配置选项

OptionDescriptionDefault
dsn
Sentry DSNRequired
send_default_pii
Include user data
False
traces_sample_rate
% of transactions traced
0
profile_session_sample_rate
% of sessions profiled
0
enable_logs
Send logs to Sentry
False
environment
Environment nameAuto-detected
release
Release versionAuto-detected
选项描述默认值
dsn
Sentry DSN必填
send_default_pii
是否包含用户数据
False
traces_sample_rate
被追踪的事务比例
0
profile_session_sample_rate
被分析的会话比例
0
enable_logs
是否将日志发送至Sentry
False
environment
环境名称自动检测
release
版本号自动检测

Environment Variables

环境变量

bash
SENTRY_DSN=https://xxx@o123.ingest.sentry.io/456
SENTRY_AUTH_TOKEN=sntrys_xxx
SENTRY_ORG=my-org
SENTRY_PROJECT=my-project
Or use in code:
python
import os
import sentry_sdk

sentry_sdk.init(
    dsn=os.environ.get("SENTRY_DSN"),
    # ...
)
bash
SENTRY_DSN=https://xxx@o123.ingest.sentry.io/456
SENTRY_AUTH_TOKEN=sntrys_xxx
SENTRY_ORG=my-org
SENTRY_PROJECT=my-project
或在代码中使用:
python
import os
import sentry_sdk

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

Verification

验证

python
undefined
python
undefined

Intentional error to test

触发故意错误以测试

division_by_zero = 1 / 0

Or capture manually:

```python
sentry_sdk.capture_message("Test message from Python")
division_by_zero = 1 / 0

或手动捕获:

```python
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
问题解决方案
错误未显示确保
init()
被尽早调用,检查DSN是否正确
无追踪数据设置
traces_sample_rate
大于0
IPython中的错误未被捕获从文件运行代码,而非交互式shell
异步错误缺失在异步函数内初始化Sentry