Skip to content

Commit 33305b1

Browse files
committed
feat(sentry): setup before_send to ignore global id errors by message
KK-1395. The ignore_messages option in sentry_sdk would be great, but since the Graphene raises a general Exception from invalid global ids, we need to follow the exception message instead of class.
1 parent 3f3acc7 commit 33305b1

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

kukkuu/settings.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,27 @@
143143
except Exception:
144144
REVISION = "n/a"
145145

146+
147+
def sentry_before_send(event, hint):
148+
"""Some Graphene errors are generic Exceptions that are not useful to log.
149+
Since they are generic, we cannot use the `ignore_errors` option of
150+
`sentry_sdk.init` to ignore those."""
151+
IGNORED_ERROR_MESSAGES = [
152+
"Unable to parse global ID",
153+
]
154+
if "exc_info" in hint:
155+
exc_type, exc_value, tb = hint["exc_info"]
156+
if any(msg in exc_value.args[0] for msg in IGNORED_ERROR_MESSAGES):
157+
return None
158+
return event
159+
160+
146161
sentry_sdk.init(
147162
dsn=env.str("SENTRY_DSN"),
148163
release=REVISION,
149164
environment=env("SENTRY_ENVIRONMENT"),
150165
integrations=[DjangoIntegration()],
166+
before_send=sentry_before_send,
151167
)
152168
sentry_sdk.integrations.logging.ignore_logger("graphql.execution.utils")
153169

kukkuu/tests/test_sentry.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import pytest
2+
3+
from kukkuu.settings import sentry_before_send
4+
5+
test_case_error_message_with_ignore_status = [
6+
("Unable to parse global ID.", True),
7+
('Unable to parse global ID "some_id".', True),
8+
('Any other error exception message".', False),
9+
]
10+
11+
12+
@pytest.mark.parametrize(
13+
"error_message,should_return_none",
14+
test_case_error_message_with_ignore_status,
15+
)
16+
def test_sentry_before_send_ignores_defined_error_messages(
17+
error_message, should_return_none
18+
):
19+
exception = Exception(error_message)
20+
hint = {"exc_info": (type(exception), exception, None)}
21+
event = {"something": "test event is returned when not ignored"}
22+
23+
result = sentry_before_send(event, hint)
24+
25+
if should_return_none:
26+
assert result is None # Ensure the event is dropped
27+
else:
28+
assert result is not None # Ensure the event is not dropped

0 commit comments

Comments
 (0)