-1

It would be really great if exceptions in sentry would contain info like git blame does.

If every line of source code which I see in an exception in sentry would have a prefix like git blame (date, commit hash, author) you could find the relevant commit faster.

AFAIK sentry can't do this out of the box. Where and how could I hook into sentry to get this?

Please leave a comment why you down-vote this question. I am curious and willing to learn.

Just for the records. The sentry team is working on something like this. Not exactly, but it does solve the same use case: https://github.com/getsentry/sentry/issues/6547

guettli
  • 25,042
  • 81
  • 346
  • 663

2 Answers2

1

I'd suggest you to try Guthub Plugin, which doesn't show git blame, but integrates with Sentry Releases and show an appropriate commit and author with a link to Github.

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

Shmygol
  • 913
  • 7
  • 16
  • Nice to know that such a plugin exists. Thank you. In my current context the source code is not on github, but in a private (non-github) repo. – guettli Nov 14 '17 at 15:43
1

A traceback consists of multiple lines of code. You can extract info from git blame for each of these lines using the GitPython library:

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)

Then, it's up to you how you want to send this information to Sentry. One of the possible solutions is choosing one commit of the list above and using the extra keyword and let your logger do the job for you:

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

Also, you can use your own logger which would add this extra parameter to all .exception(), .error() and .critical() calls.

Overall, it's quite vague what behaviour do you want to achieve, and everything is possible. But calling git blame is expensive and may hurt the performance of your application a lot.

gukoff
  • 2,112
  • 3
  • 18
  • 30
  • Yes, doing this on the application host would hurt the performance. Is there a way to do this inside sentry (after the data was transferred from the application host to sentry)? – guettli Nov 14 '17 at 15:45
  • I believe you can make changes to the Sentry database manually or create a custom plugin, like here: https://docs.sentry.io/server/plugins/#rd-party-plugins. For example, fork and alter the Github plugin @Trilliput told about. There are bitbucket repos, too. – gukoff Nov 14 '17 at 20:56