Skip to content

Commit

Permalink
Sentry improvements (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviacastro authored Apr 1, 2022
1 parent b840f60 commit 4d89252
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
18 changes: 17 additions & 1 deletion serpens/sentry.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
31 changes: 31 additions & 0 deletions tests/test_sentry.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 4d89252

Please sign in to comment.