【问题标题】:Making Sentry report 5XX errors in multi-thread environment让Sentry在多线程环境下报5XX错误
【发布时间】:2018-06-05 14:29:54
【问题描述】:

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

问题出现在以下代码sn-p中:

@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 之类的东西),我将不胜感激。

【问题讨论】:

  • 您希望发生特定的异常还是什么?
  • 我已经更新了问题的主题。我的意思是,5XX 错误 - 也就是说,任何东西
  • 也许您使用的是 Django 的默认 500 处理程序而不是 Sentry 的处理程序。您介意分享相关的settings.py 条目吗?
  • 嗯,在主线程中,报错了。查看更新后的代码 sn -p
  • @EdgarNavasardyan 这个问题有点老了,但看起来你真的想要芹菜或类似的东西。我无法想象在 WSGI 应用程序中生成线程会顺利进行。 Raven 集成了 Celery。

标签: django sentry


【解决方案1】:

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

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

【讨论】:

  • 这是非常有用的提示,Sardorbek。你为我解决了另一个相关的问题。谢谢 !!!我会等一会儿。除非我得到更通用的答案,否则我会接受这个作为解决方案
猜你喜欢
  • 2014-08-31
  • 2014-10-10
  • 2020-08-28
  • 2011-08-09
  • 1970-01-01
  • 2015-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多