-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6f077c1
commit af93d79
Showing
5 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import logging | ||
|
||
LOG = logging.getLogger(__name__) | ||
|
||
|
||
class SentryClient: | ||
def __init__(self, config): | ||
self.sentry = None | ||
if config is None: | ||
LOG.info("Sentry configuration not found, skipping setup") | ||
return | ||
dsn = config.get("dsn") | ||
if dsn is None: | ||
LOG.info("Sentry DSN not found, skipping setup") | ||
return | ||
try: | ||
import sentry_sdk | ||
from sentry_sdk.integrations.logging import LoggingIntegration | ||
except ImportError: | ||
LOG.info("Sentry SDK not found, skipping setup") | ||
return | ||
self.sentry = sentry_sdk | ||
sentry_logging = LoggingIntegration( | ||
level=logging.INFO, | ||
event_level=logging.CRITICAL, | ||
) | ||
tags = config.pop("tags", {}) | ||
sentry_sdk.init(**config, integrations=[sentry_logging]) | ||
for key, value in tags.items(): | ||
sentry_sdk.set_tag(key, value) | ||
|
||
def gauge(self, metric, value, tags=None): | ||
pass | ||
|
||
def increase(self, metric, inc_value=1, tags=None): | ||
pass | ||
|
||
def unexpected_exception(self, ex, where, tags=None): | ||
if not self.sentry: | ||
return | ||
|
||
with self.sentry.push_scope() as scope: | ||
scope.set_tag("where", where) | ||
if tags and isinstance(tags, dict): | ||
for key, value in tags.items(): | ||
scope.set_tag(key, value) | ||
self.sentry.capture_exception(ex) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import contextlib | ||
import logging | ||
|
||
import sentry_sdk | ||
|
||
from pghoard.monitoring.sentry import SentryClient | ||
|
||
|
||
@contextlib.contextmanager | ||
def patch_sentry_init(events): | ||
original_init = sentry_sdk.init | ||
|
||
def patched_init(*args, **kwargs): | ||
kwargs["transport"] = events.append | ||
kwargs.pop("dsn", None) | ||
return original_init(*args, **kwargs) | ||
|
||
sentry_sdk.init = patched_init | ||
try: | ||
yield | ||
finally: | ||
sentry_sdk.init = original_init | ||
|
||
|
||
def test_missing_config(): | ||
client = SentryClient(config=None) | ||
client.unexpected_exception(ValueError("hello !"), where="tests") | ||
client.gauge("something", 123.456) | ||
client.increase("something") | ||
|
||
|
||
def test_exception_send(): | ||
events = [] | ||
with patch_sentry_init(events): | ||
client = SentryClient(config={"dsn": "http://localhost:9000", "tags": {"foo": "bar"}}) | ||
client.unexpected_exception(ValueError("hello !"), where="tests") | ||
assert len(events) == 1 | ||
|
||
|
||
def test_logging_integration(): | ||
events = [] | ||
with patch_sentry_init(events): | ||
SentryClient(config={"dsn": "http://localhost:9000", "tags": {"foo": "bar"}}) | ||
|
||
logging.warning("Info") | ||
assert len(events) == 0 | ||
logging.error("Error") | ||
assert len(events) == 0 | ||
logging.critical("Critical") | ||
assert len(events) == 1 |