|
| 1 | +import os |
| 2 | + |
| 3 | +import sentry_sdk |
| 4 | +from django.core.exceptions import PermissionDenied |
| 5 | +from celery.exceptions import Retry as CeleryRetry |
| 6 | +from sentry_sdk.integrations.logging import ignore_logger |
| 7 | +from sentry_sdk.integrations.celery import CeleryIntegration |
| 8 | +from sentry_sdk.integrations.django import DjangoIntegration |
| 9 | +from sentry_sdk.integrations.redis import RedisIntegration |
| 10 | + |
| 11 | +# Celery Terminated Exception: The worker processing a job has been terminated by user request. |
| 12 | +from billiard.exceptions import Terminated |
| 13 | + |
| 14 | +IGNORED_ERRORS = [ |
| 15 | + Terminated, |
| 16 | + PermissionDenied, |
| 17 | + CeleryRetry, |
| 18 | +] |
| 19 | +IGNORED_LOGGERS = [ |
| 20 | + 'django.core.exceptions.ObjectDoesNotExist', |
| 21 | +] |
| 22 | + |
| 23 | +for _logger in IGNORED_LOGGERS: |
| 24 | + ignore_logger(_logger) |
| 25 | + |
| 26 | + |
| 27 | +class InvalidGitRepository(Exception): |
| 28 | + pass |
| 29 | + |
| 30 | + |
| 31 | +def fetch_git_sha(path, head=None): |
| 32 | + """ |
| 33 | + Source: https://github.com/getsentry/raven-python/blob/03559bb05fd963e2be96372ae89fb0bce751d26d/raven/versioning.py |
| 34 | + >>> fetch_git_sha(os.path.dirname(__file__)) |
| 35 | + """ |
| 36 | + if not head: |
| 37 | + head_path = os.path.join(path, '.git', 'HEAD') |
| 38 | + if not os.path.exists(head_path): |
| 39 | + raise InvalidGitRepository( |
| 40 | + 'Cannot identify HEAD for git repository at %s' % (path,)) |
| 41 | + |
| 42 | + with open(head_path, 'r') as fp: |
| 43 | + head = str(fp.read()).strip() |
| 44 | + |
| 45 | + if head.startswith('ref: '): |
| 46 | + head = head[5:] |
| 47 | + revision_file = os.path.join( |
| 48 | + path, '.git', *head.split('/') |
| 49 | + ) |
| 50 | + else: |
| 51 | + return head |
| 52 | + else: |
| 53 | + revision_file = os.path.join(path, '.git', 'refs', 'heads', head) |
| 54 | + |
| 55 | + if not os.path.exists(revision_file): |
| 56 | + if not os.path.exists(os.path.join(path, '.git')): |
| 57 | + raise InvalidGitRepository( |
| 58 | + '%s does not seem to be the root of a git repository' % (path,)) |
| 59 | + |
| 60 | + # Check for our .git/packed-refs' file since a `git gc` may have run |
| 61 | + # https://git-scm.com/book/en/v2/Git-Internals-Maintenance-and-Data-Recovery |
| 62 | + packed_file = os.path.join(path, '.git', 'packed-refs') |
| 63 | + if os.path.exists(packed_file): |
| 64 | + with open(packed_file) as fh: |
| 65 | + for line in fh: |
| 66 | + line = line.rstrip() |
| 67 | + if line and line[:1] not in ('#', '^'): |
| 68 | + try: |
| 69 | + revision, ref = line.split(' ', 1) |
| 70 | + except ValueError: |
| 71 | + continue |
| 72 | + if ref == head: |
| 73 | + return str(revision) |
| 74 | + |
| 75 | + raise InvalidGitRepository( |
| 76 | + 'Unable to find ref to head "%s" in repository' % (head,)) |
| 77 | + |
| 78 | + with open(revision_file) as fh: |
| 79 | + return str(fh.read()).strip() |
| 80 | + |
| 81 | + |
| 82 | +def init_sentry(app_type, tags={}, **config): |
| 83 | + integrations = [ |
| 84 | + CeleryIntegration(), |
| 85 | + DjangoIntegration(), |
| 86 | + RedisIntegration(), |
| 87 | + ] |
| 88 | + sentry_sdk.init( |
| 89 | + **config, |
| 90 | + ignore_errors=IGNORED_ERRORS, |
| 91 | + integrations=integrations, |
| 92 | + ) |
| 93 | + with sentry_sdk.configure_scope() as scope: |
| 94 | + scope.set_tag('app_type', app_type) |
| 95 | + for tag, value in tags.items(): |
| 96 | + scope.set_tag(tag, value) |
0 commit comments