gpt4 book ai didi

python - 如何忽略 Sentry 的一些错误(不发送它们)?

转载 作者:行者123 更新时间:2023-12-05 05:38:59 27 4
gpt4 key购买 nike

我有一个基于 django (3.2.10) 和 sentry-sdk (1.16.0) 的项目这是我的 sentry-init 文件:

from os import getenv


SENTRY_URL = getenv('SENTRY_URL')

if SENTRY_URL:
from sentry_sdk import init
from sentry_sdk.integrations.django import DjangoIntegration
from sentry_sdk.integrations.redis import RedisIntegration
from sentry_sdk.integrations.celery import CeleryIntegration

init(
dsn=SENTRY_URL,
integrations=[DjangoIntegration(), RedisIntegration(), CeleryIntegration()],
traces_sample_rate=1.0,
send_default_pii=True,
debug=True,
)

我有一个继承自 Exception 的 CustomError

每次我引发 CustomError sentry-sdk 都会将其发送到 dsn-url。

我想忽略某类错误或类似的错误。我该怎么做?

最佳答案

您可以传递一个过滤要发送的错误的函数:

from os import getenv


SENTRY_URL = getenv('SENTRY_URL')

if SENTRY_URL:
from sentry_sdk import init
from sentry_sdk.integrations.django import DjangoIntegration
from sentry_sdk.integrations.redis import RedisIntegration
from sentry_sdk.integrations.celery import CeleryIntegration

def before_send(event, hint):
if 'exc_info' in hint:
exc_type, exc_value, tb = hint['exc_info']
if isinstance(exc_value, CustomError): # Replace CustomError with your custom error
return None
return event

init(
dsn=SENTRY_URL,
integrations=[DjangoIntegration(), RedisIntegration(), CeleryIntegration()],
traces_sample_rate=1.0,
send_default_pii=True,
debug=True,
before_send=before_send
)

您可以在 documentation 中找到更多信息.

关于python - 如何忽略 Sentry 的一些错误(不发送它们)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72798967/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com