diff --git a/serpens/sentry.py b/serpens/sentry.py index 5f93eef..fea3db8 100644 --- a/serpens/sentry.py +++ b/serpens/sentry.py @@ -1,11 +1,18 @@ -import os import logging +import os import sentry_sdk logger = logging.getLogger(__name__) +def before_send(event, hint): + if "exc_info" in hint and isinstance(hint["exc_info"][1], FilteredEvent): + return None + + return event + + def setup() -> None: if "SENTRY_DSN" not in os.environ: return @@ -22,5 +29,14 @@ def setup() -> None: environment=environment, release=release, debug=os.getenv("DEBUG", "False").lower() in ("yes", "true", "1"), + before_send=before_send, ) logger.info("Sentry's SDK initialized") + + +class FilteredEvent(Exception): + """ + Base class for exceptions that shouldn't be sent to Sentry + """ + + pass diff --git a/tests/test_sentry.py b/tests/test_sentry.py new file mode 100644 index 0000000..75ec0a4 --- /dev/null +++ b/tests/test_sentry.py @@ -0,0 +1,31 @@ +import unittest + +from serpens import sentry + + +class TestSentry(unittest.TestCase): + def test_before_send_with_exc_info(self): + event = "foo" + hint = {"exc_info": ["exception", "foo", "bar"]} + + response = sentry.before_send(event, hint) + + self.assertIsNotNone(response) + self.assertEqual(response, event) + + def test_before_send_without_exc_info(self): + event = "foo" + hint = "bar" + + response = sentry.before_send(event, hint) + + self.assertIsNotNone(response) + self.assertEqual(response, event) + + def test_before_send_filtering_event(self): + event = sentry.FilteredEvent("foo") + hint = {"exc_info": ["exception", event, "bar"]} + + response = sentry.before_send(event, hint) + + self.assertIsNone(response)