Skip to content

Commit f232111

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 ee125c0 commit f232111

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

kukkuu/settings.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
import sentry_sdk
77
from django.core.exceptions import ImproperlyConfigured
88
from django.utils.translation import gettext_lazy as _
9+
from jose import ExpiredSignatureError
910
from sentry_sdk.integrations.django import DjangoIntegration
11+
from sentry_sdk.types import Event, Hint
1012

1113
from kukkuu.consts import CSP
14+
from kukkuu.exceptions import AuthenticationExpiredError
1215
from kukkuu.tests.utils.jwt_utils import is_valid_256_bit_key
1316

1417
checkout_dir = environ.Path(__file__) - 2
@@ -143,11 +146,40 @@
143146
except Exception:
144147
REVISION = "n/a"
145148

149+
150+
def sentry_before_send(event: Event, hint: Hint):
151+
"""
152+
Filter out uninformative errors from being sent to Sentry.
153+
154+
Some Graphene errors are generic Exceptions with unhelpful messages.
155+
This function filters them out to reduce noise in Sentry.
156+
157+
NOTE: The `ignore_errors` option is actually deprecated,
158+
nevertheless it's included in the init configuration.
159+
See https://github.com/getsentry/sentry-python/issues/149
160+
161+
Args:
162+
event: The event that is being sent to Sentry.
163+
hint: A dictionary containing additional information about the event.
164+
165+
Returns:
166+
The event if it should be sent to Sentry, or None if it should be ignored.
167+
"""
168+
169+
IGNORED_ERRORS_CLASSES = (ExpiredSignatureError, AuthenticationExpiredError)
170+
if "exc_info" in hint:
171+
exc_type, exc_value, traceback = hint["exc_info"]
172+
if isinstance(exc_value, IGNORED_ERRORS_CLASSES):
173+
return None
174+
return event
175+
176+
146177
sentry_sdk.init(
147178
dsn=env.str("SENTRY_DSN"),
148179
release=REVISION,
149180
environment=env("SENTRY_ENVIRONMENT"),
150181
integrations=[DjangoIntegration()],
182+
before_send=sentry_before_send,
151183
)
152184
sentry_sdk.integrations.logging.ignore_logger("graphql.execution.utils")
153185

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)