【问题标题】:Prevent Raven from shooting specified exceptions to Sentry防止 Raven 向 Sentry 射击指定的异常
【发布时间】:2014-09-18 16:50:26
【问题描述】:

当我的数据库出现故障时,Sentry 会立即被 psycopg2 的OperationalError: could not connect to server: Connection refused 淹没。由于OperationalError 可以在无法访问的数据库之外的其他情况下抛出,所以我不能盲目地使用RAVEN_CONFIGIGNORE_EXCEPTIONS 忽略它。

我尝试写a filter for Django logging,但它不起作用。它正确地拦截了异常,但仍然以某种方式将其冒泡。这是过滤器:

def skip_unreachable_database(record):
    """Avoid flooding Sentry when the database is down"""
    if record.exc_info:
        print '>>>', record.exc_info
        exc_type, exc_value = record.exc_info[:2]
        if isinstance(exc_value, OperationalError) and exc_value.message.lower().startswith('could not connect to server: connection refused'):
            return False
    return True

a ticket about filtering not working with Raven,但是已经关闭了。

知道如何解决这个问题吗?

【问题讨论】:

    标签: django sentry raven


    【解决方案1】:

    这是我的想法(目前):

    1/使用Raven的配置过滤掉所有OperationalError

    RAVEN_CONFIG = {
        # [...]
        'IGNORE_EXCEPTIONS': [
            'OperationalError',
        ],
    }
    

    2/ 为这些异常添加专用的过滤器、记录器和日志记录文件,这样它们就不会丢失:

    def operational_errors_only(record):
        """Only catch OperationalError exceptions"""
        if record.exc_info:
            exc_type, exc_value = record.exc_info[:2]
            if isinstance(exc_value, OperationalError):
                return True
        return False
    
    LOGGING = {
        # [...]
        'filters': {
            'operational_errors_only': {
                '()': 'django.utils.log.CallbackFilter',
                'callback': operational_errors_only,
            },
        },
        'handlers': {
            'operationalerrors': {
                'mode': 'a',
                'class': 'common_src.logutils.FallbackWatchedFileHandler',
                'filename': '/path/to/operationalerrors.log',
                'formatter': 'verbose',
                'filters': ['operational_errors_only'],
            },
        },
        'loggers': {
            '': {
                'handlers': ['console', 'sentry', 'operationalerrors'],
                'level': 'ERROR',
                'propagate': True,
            },
        },
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-07
      • 2021-10-22
      • 2019-07-16
      • 1970-01-01
      • 2017-03-13
      • 2021-12-13
      • 2017-12-31
      • 2014-11-13
      相关资源
      最近更新 更多