Settings(续)
# config/settings/sentry.py
from config.env import env
# 第一步:读取 DSN,默认为空字符串
SENTRY_DSN = env('SENTRY_DSN', default='')
# 第二步:只有在 DSN 存在时才配置 Sentry
if SENTRY_DSN:
import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration
from sentry_sdk.integrations.celery import CeleryIntegration
sentry_sdk.init(
dsn=SENTRY_DSN,
integrations=[
DjangoIntegration(),
CeleryIntegration(),
],
environment=env('SENTRY_ENVIRONMENT', default='development'),
traces_sample_rate=env.float('SENTRY_TRACES_SAMPLE_RATE', default=0.0),
send_default_pii=True,
)
完整文件参考: Styleguide-Example/config/settings/sentry.py
📝 工作流程详解
场景 1:生产环境(启用 Sentry)
# .env (生产环境)
SENTRY_DSN=https://xxx@sentry.io/xxx
SENTRY_ENVIRONMENT=production
SENTRY_TRACES_SAMPLE_RATE=1.0
执行流