我可以将 Luigi 管道错误路由到 Sentry 吗?

Can I route Luigi pipeline errors to Sentry?

我的团队使用 Sentry 来跟踪错误,因此我宁愿不使用 Luigi 的内置电子邮件功能将我们的所有报告保存在一个地方。

这就是我目前的设置方式,它似乎完全跳过了 Sentry:

if __name__ == '__main__':
    try:
        luigi.run()
    except Exception as e:
        client = Client(
            ***
        )
        client.captureException(tags={
            sys.argv[0]
        })
        logger.critical('Error occurred: {e}'.format(e=e))
        raise

我认为如果你声明一个 callback to the failure event 并在那里做哨兵跟踪应该是可能的:

import luigi

@luigi.Task.event_handler(luigi.Event.FAILURE)
def mourn_failure(task, exception):
    client = Client(
        ***
    )
    # we also include extra context for the current task
    client.captureException(
       (type(e), e.message, e.traceback),
       extra=dict(task=repr(task))
    )
    logger.critical('Error occurred: {e}'.format(e=exception))


if __name__ == '__main__':
    luigi.run()

注意 e.traceback 仅适用于 python 3+ as explained in this answer.