def test_sentry_log_specific_keys_as_tags(mocker: Any, level: SentryLevel) -> None: m_capture_event = mocker.patch("kodiak.logging.capture_event") event_data = {"event": level + " message", "info1": "info1", "required": True} tag_keys = ["info1", "required", "some non existing key"] sentry_event_data = event_data.copy() processor = SentryProcessor( level=getattr(logging, level.upper()), tag_keys=tag_keys ) processor(None, level, event_data) m_capture_event.assert_called_once_with( { "level": level, "message": event_data["event"], "extra": sentry_event_data, "tags": { k: sentry_event_data[k] for k in tag_keys if k in sentry_event_data }, }, hint={}, ) processor_only_errors = SentryProcessor(level=logging.ERROR) event_dict = processor_only_errors(None, level, {"event": level + " message"}) assert "sentry_id" not in event_dict
def test_sentry_log_failure(mocker: Any, level: SentryLevel) -> None: m_capture_event = mocker.patch("kodiak.logging.capture_event") mocker.patch( "kodiak.logging.event_from_exception", return_value=({ "exception": mocker.sentinel.exception }, mocker.sentinel.hint), ) event_data = {"event": level + " message"} sentry_event_data = event_data.copy() processor = SentryProcessor(level=getattr(logging, level.upper())) try: 1 / 0 except ZeroDivisionError: processor(None, level, event_data) m_capture_event.assert_called_once_with( { "level": level, "message": event_data["event"], "exception": mocker.sentinel.exception, "extra": sentry_event_data, }, hint=mocker.sentinel.hint, )
def test_sentry_log(mocker: Any, level: SentryLevel) -> None: m_capture_event = mocker.patch("kodiak.logging.capture_event") event_data = {"event": level + " message"} sentry_event_data = event_data.copy() processor = SentryProcessor(level=getattr(logging, level.upper())) processor(None, level, event_data) m_capture_event.assert_called_once_with( {"level": level, "message": event_data["event"], "extra": sentry_event_data}, hint={}, ) processor_only_errors = SentryProcessor(level=logging.ERROR) event_dict = processor_only_errors(None, level, {"event": level + " message"}) assert "sentry_id" not in event_dict
# TODO(sbdchd): waiting on https://github.com/getsentry/sentry-python/pull/444 # to be merged & released to remove `# type: ignore` sentry_sdk.init( integrations=[LoggingIntegration(level=None, event_level=None) ] # type: ignore ) structlog.configure( processors=[ structlog.stdlib.filter_by_level, structlog.stdlib.PositionalArgumentsFormatter(), structlog.processors.StackInfoRenderer(), structlog.processors.format_exc_info, structlog.processors.UnicodeDecoder(), add_request_info_processor, SentryProcessor(level=logging.WARNING), structlog.processors.KeyValueRenderer(key_order=["event"], sort_keys=True), ], context_class=dict, logger_factory=structlog.stdlib.LoggerFactory(), wrapper_class=structlog.stdlib.BoundLogger, cache_logger_on_first_use=True, ) logger = structlog.get_logger() # we query for both organization repositories and user repositories because we # do not know of the installation is a user or an organization. We filter to # private repositories only because we may not have access to all public # repositories and we'll only be able to see private repositories we can access.
def test_sentry_sent() -> None: processor = SentryProcessor() event_dict = processor(None, "error", {}) assert "sentry_id" in event_dict