使哨兵在多线程环境下报5XX错误

Making Sentry report 5XX errors in multi-thread environment

我正在使用 Sentry 在我的应用程序中启动异常处理日志记录。

问题出现在以下代码片段中:

@api_view(['POST'])
def testView(request):
    a = 1/0 # This error is reported to Sentry
    TestThread().start()
    return f_response_ok()

class TestThread(threading.Thread):

    def __init__(self, *args, **kwargs):
        super(TestThread, self).__init__(*args, **kwargs)

    def run(self):
        print('Test')
        a = 1/0 # but this one is not
        return True

是否可以让 Sentry 报告并行线程中发生的错误?

还有点跑题: 如果有人就这种编程模式是否已过时提供简短评论(应该改用 RabbitMQ 之类的东西),我将不胜感激。

您可以手动将它们记录到 sentry。

https://docs.sentry.io/clients/python/#capture-an-error

假设您使用的是 django

from raven.contrib.django.raven_compat.models import client

class TestThread(threading.Thread):

    def __init__(self, *args, **kwargs):
        super(TestThread, self).__init__(*args, **kwargs)

    def run(self):
        print('Test')
        try:
            a = 1/0 # error is not reported in Sentry
        except: # I would suggest putting here expected exceptions
            client.captureException()
        return True