如何防止sentry在Django本地服务器上记录错误?

rbl8hiat  于 2023-04-22  发布在  Go
关注(0)|答案(4)|浏览(166)

我正在尝试阻止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
    )
um6iljoc

um6iljoc1#

有两种方法可以做到这一点:
第一个选项是import sys并检查 runserver(参考:https://stackoverflow.com/a/49874564/15205504

import sys

if (len(sys.argv) >= 2 and sys.argv[1] != 'runserver'):
    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
)

第二个选项是在settings.py中指定环境类型。例如,如果您的生产服务器是Heroku,则可以在Heroku或.env文件中创建env_type变量并将其设置为'HEROKU',然后像这样使用它:

env_type = os.environ.get('env_type', 'LOCAL')

if env_type == 'HEROKU':
    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
)
3xiyfsfu

3xiyfsfu2#

dsn设置为None将禁止所有SDK操作,因此您的配置应该可以工作,除非您还在本地环境中设置SENTRY_DSN变量。

yfwxisqw

yfwxisqw3#

我也使用了类似的方法,但我更喜欢len(sys.argv) > 1而不是len(sys.argv) >= 2。使用len(sys.argv) >= 2可能会在apache生产中引发“超出范围”错误。

INSTANCE_RUNNING_ON_LOCALHOST = False
if len(sys.argv) > 1: # this check is needed to prevent 'out of range' error in apache production.
    INSTANCE_RUNNING_ON_LOCALHOST = (sys.argv[1] == 'runserver') # Determine if Django is running under the development server. #Ref: https://stackoverflow.com/questions/12027545/determine-if-django-is-running-under-the-development-server/12028260 

if not INSTANCE_RUNNING_ON_LOCALHOST:
    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
    )
g0czyy6m

g0czyy6m4#

这样试试:

if not DEBUG:
    sentry_sdk.init(
        dsn="SENTRY_DSN",
        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
    )

相关问题