Bin
2025-12-16 9e0b2ba2c317b1a86212f24cbae3195ad1f3dbfa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from django.conf import settings
 
 
def event_processor(event, hint):
    # skip all transactions without exceptions, unless it's a log record
    if 'exc_info' not in hint:
        # special flag inside of logger.error(..., extra={'sentry_force': True}) to force sentry to log the error
        if 'log_record' in hint and event.get('extra', {}).get('sentry_force', False):
            return event
 
        return None
 
    # skip specified exceptions
    exceptions = event.get('exception', {}).get('values', [{}])
    last = exceptions[-1]
    if last.get('type') in settings.SENTRY_IGNORED_EXCEPTIONS:
        return None
 
    # sentry ignored factory class
    if 'SentryIgnored' in last.get('type'):
        return None
 
    if last.get('type') == 'OperationalError':
        value = last.get('value')
        messages = [
            'sorry, too many clients already',
            'Name or service not known',
            'could not connect to server',
            'the database system is shutting down',
            'remaining connection slots are reserved for non-replication superuser connections',
            'unable to open database file',
        ]
        for message in messages:
            if message in value:
                return None
 
    if last.get('type') == 'OSError':
        value = last.get('value')
        messages = [
            'Too many open files: ',
        ]
        for message in messages:
            if message in value:
                return None
 
    # special flag inside of logger.error(..., extra={'sentry_skip': True}) to skip error message
    if event.get('extra', {}).get('sentry_skip', False):
        return None
 
    # skip transactions by urls
    if event.get('transaction') in [
        '/static/{path}',
        '/dm/{path}',
        '/react-app/{path}',
        '/label-studio-frontend/{path}',
        '/favicon.ico',
        '/health',
    ]:
        return None
 
    return event  # to return all other events
 
 
def init_sentry(release_name, release_version):
    if settings.SENTRY_DSN:
        import sentry_sdk
        from sentry_sdk.integrations.django import DjangoIntegration
 
        if settings.SENTRY_REDIS_ENABLED:
            from sentry_sdk.integrations.redis import RedisIntegration
            from sentry_sdk.integrations.rq import RqIntegration
 
            advanced = [RedisIntegration(), RqIntegration()]
        else:
            advanced = []
 
        # define the event processor, this runs before before_send if enabled
        sentry_sdk.scope.add_global_event_processor(event_processor)
 
        sentry_sdk.init(
            dsn=settings.SENTRY_DSN,
            integrations=[DjangoIntegration()] + advanced,
            traces_sample_rate=settings.SENTRY_RATE,
            send_default_pii=True,
            environment=settings.SENTRY_ENVIRONMENT,
            release=release_name + '@' + str(release_version),
        )