Skip to content

Commit 24b03ca

Browse files
committed
feat(sentry): ignore authentication expired errors in Sentry
KK-1396. Sentry should ignore errors related to expired authentications, since it is not a system error, but a feature related to JWT usage.
1 parent 3f3acc7 commit 24b03ca

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

kukkuu/settings.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
from datetime import datetime
44

55
import environ
6+
from jose import ExpiredSignatureError
67
import sentry_sdk
78
from django.core.exceptions import ImproperlyConfigured
89
from django.utils.translation import gettext_lazy as _
910
from sentry_sdk.integrations.django import DjangoIntegration
1011

1112
from kukkuu.consts import CSP
13+
from kukkuu.exceptions import AuthenticationExpiredError
1214
from kukkuu.tests.utils.jwt_utils import is_valid_256_bit_key
1315

1416
checkout_dir = environ.Path(__file__) - 2
@@ -143,11 +145,29 @@
143145
except Exception:
144146
REVISION = "n/a"
145147

148+
149+
def sentry_before_send(event, hint):
150+
"""
151+
Ignore certain errors from being sent to Sentry.
152+
NOTE: The `ignore_errors` option is actually deprecated,
153+
nevertheless it's included in the init configuration.
154+
See https://github.com/getsentry/sentry-python/issues/149
155+
"""
156+
157+
IGNORED_ERRORS_CLASSES = (ExpiredSignatureError, AuthenticationExpiredError)
158+
if "exc_info" in hint:
159+
exc_type, exc_value, tb = hint["exc_info"]
160+
if isinstance(exc_value, IGNORED_ERRORS_CLASSES):
161+
return None
162+
return event
163+
164+
146165
sentry_sdk.init(
147166
dsn=env.str("SENTRY_DSN"),
148167
release=REVISION,
149168
environment=env("SENTRY_ENVIRONMENT"),
150169
integrations=[DjangoIntegration()],
170+
before_send=sentry_before_send,
151171
)
152172
sentry_sdk.integrations.logging.ignore_logger("graphql.execution.utils")
153173

kukkuu/tests/test_sentry.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import pytest
2+
from jose import ExpiredSignatureError
3+
4+
from kukkuu.exceptions import AuthenticationExpiredError
5+
from kukkuu.settings import sentry_before_send
6+
7+
test_cases = [
8+
(ExpiredSignatureError("Expired signature"), True),
9+
(AuthenticationExpiredError("Authentication expired"), True),
10+
(Exception("Some other error"), False),
11+
]
12+
13+
14+
@pytest.mark.parametrize(
15+
"exception,should_return_none",
16+
test_cases,
17+
)
18+
def test_sentry_before_send_ignores_defined_exceptions(exception, should_return_none):
19+
hint = {"exc_info": (type(exception), exception, None)}
20+
event = {"something": "test event is returned when not ignored"}
21+
22+
result = sentry_before_send(event, hint)
23+
24+
if should_return_none:
25+
assert result is None # Ensure the event is dropped
26+
else:
27+
assert result is not None # Ensure the event is not dropped

0 commit comments

Comments
 (0)