我正在尝试阻止Sentry在我的本地服务器(即http://127.0.0.1:8000/)上工作时将错误记录到我的Sentry Jmeter 板。我希望Sentry将错误记录到 Jmeter 板的唯一时间是当我的代码处于生产环境中时。我如何才能做到这一点?我已经尝试了以下方法,但它不起作用:
if DEBUG == True
sentry_sdk.init(
dsn=os.environ.get('SENTRY_DSN', None),
integrations=[DjangoIntegration()],
# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for performance monitoring.
# We recommend adjusting this value in production.
traces_sample_rate=1.0,
# If you wish to associate users to errors (assuming you are using
# django.contrib.auth) you may enable sending PII data.
send_default_pii=True
)
4条答案
按热度按时间um6iljoc1#
有两种方法可以做到这一点:
第一个选项是
import sys
并检查 runserver(参考:https://stackoverflow.com/a/49874564/15205504)第二个选项是在
settings.py
中指定环境类型。例如,如果您的生产服务器是Heroku,则可以在Heroku或.env
文件中创建env_type变量并将其设置为'HEROKU'
,然后像这样使用它:3xiyfsfu2#
将
dsn
设置为None
将禁止所有SDK操作,因此您的配置应该可以工作,除非您还在本地环境中设置SENTRY_DSN
变量。yfwxisqw3#
我也使用了类似的方法,但我更喜欢
len(sys.argv) > 1
而不是len(sys.argv) >= 2
。使用len(sys.argv) >= 2
可能会在apache生产中引发“超出范围”错误。g0czyy6m4#
这样试试: