Python乌鸦。发送部分帧

时间:2014-03-19 12:09:38

标签: python logging sentry raven

在我们的项目中,raven花费了很多时间来创建和发送消息给Sentry。我们检查了创建有关帧的数据的许多时间成本,但我们不需要经常提供有关所有帧的数据。是否有可能以及如何在异常或错误的帧之前创建关于并非所有帧的数据,并将其发送到哨兵。或者可能在消息级别创建依赖性完全堆栈/部分堆栈?即在严重或异常或错误级别发送完整堆栈,并在调试或警告级别只发送一部分帧。

1 个答案:

答案 0 :(得分:0)

将此处理程序用于Django:

from raven.contrib.django.handlers import SentryHandler as BaseSentryHandler
from raven.utils.stacks import iter_stack_frames


class SentryHandler(BaseSentryHandler):

    def emit(self, record):
        if not getattr(record, 'stack', None):
            record.stack = self._get_stack(record)

        return super(SentryHandler, self).emit(record)

    def _get_targetted_stack(self, stack):
        return stack

    def _get_stack(self, record):
        """Remove non-interesting parts of the stack.
        """
        stack = iter_stack_frames()

        frames = []

        for item in stack:
            if isinstance(item, (list, tuple)):
                frame, lineno = item
            else:
                frame, lineno = item, item.f_lineno

            frames.append((frame, lineno))

        # remove parts after the `logging` module was entered
        for frame_no in reversed(xrange(len(frames))):
            frame, line_no = frames[frame_no]
            module_name = getattr(frame, 'f_globals', {}).get('__name__', '')
            if module_name.startswith('logging'):
                del frames[:frame_no + 1]
                break
        else:
            # the entry point to `logging` module was not found
            return frames

        return frames