Sentry for Python:为每个源代码行添加类似前缀"git blame"



如果哨兵中的异常像git blame一样包含信息,那就太好了。

如果我在哨兵的异常中看到的每一行源代码都有一个前缀,如git blame(日期、提交哈希、作者),你可以更快地找到相关的提交。

AFAIK哨兵不能开箱即用。我在哪里以及如何钩上哨兵来获得这个?

请发表评论,为什么你对这个问题投了反对票。我很好奇,愿意学习。

仅供记录。哨兵团队正在研究这样的事情。不完全是,但它确实解决了相同的用例:https://github.com/getsentry/sentry/issues/6547

我建议您尝试Guthub插件,它不显示git blame,但与Sentry Release集成,并显示适当的提交和作者以及指向Github的链接。

https://sentry.io/integrations/github/

回溯由多行代码组成。您可以使用 GitPython 库从git blame中提取以下每一行的信息:

import sys
import traceback

from git import Repo

def commit_info(file_path, line_number):
    for commit, lines in Repo().blame('HEAD', file_path):
        line_number -= len(lines)
        if line_number <= 0:
            return commit.hexsha, commit.committed_datetime, commit.author.name

try:
    raise Exception('error')
except Exception:
    for filename, line_number, _, _ in traceback.extract_tb(sys.exc_info()[2]):
        print filename, line_number, commit_info(filename, line_number)

然后,由您决定如何将此信息发送给哨兵。可能的解决方案之一是选择上面列表中的一个提交并使用 extra 关键字,让您的记录器为您完成这项工作:

try:
    raise Exception
except Exception:
    commit = choose_one_commit()
    logger.exception('Error', extra={'author': author.name, 'sha': commit.hexsha}) 

此外,您可以使用自己的记录器,它将此extra参数添加到所有.exception().error().critical()调用中。

总的来说,你想要实现什么行为是相当模糊的,一切皆有可能。但是调用git blame成本很高,并且可能会严重损害应用程序的性能。

最新更新