From 0df95e908852828deabdf44b9fc5ec7f235ae90e Mon Sep 17 00:00:00 2001 From: Prowler Bot Date: Tue, 17 Jun 2025 09:56:02 +0200 Subject: [PATCH] chore(sentry): handle exceptions ignores not based in ClassNames (#8038) Co-authored-by: Andoni Alonso <14891798+andoniaf@users.noreply.github.com> --- api/src/backend/api/tests/test_sentry.py | 80 +++++++++++++++++++++++ api/src/backend/config/settings/sentry.py | 13 +++- 2 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 api/src/backend/api/tests/test_sentry.py diff --git a/api/src/backend/api/tests/test_sentry.py b/api/src/backend/api/tests/test_sentry.py new file mode 100644 index 0000000000..cf71593469 --- /dev/null +++ b/api/src/backend/api/tests/test_sentry.py @@ -0,0 +1,80 @@ +import logging +from unittest.mock import MagicMock + +from config.settings.sentry import before_send + + +def test_before_send_ignores_log_with_ignored_exception(): + """Test that before_send ignores logs containing ignored exceptions.""" + log_record = MagicMock() + log_record.msg = "Provider kubernetes is not connected" + log_record.levelno = logging.ERROR # 40 + + hint = {"log_record": log_record} + + event = MagicMock() + + result = before_send(event, hint) + + # Assert that the event was dropped (None returned) + assert result is None + + +def test_before_send_ignores_exception_with_ignored_exception(): + """Test that before_send ignores exceptions containing ignored exceptions.""" + exc_info = (Exception, Exception("Provider kubernetes is not connected"), None) + + hint = {"exc_info": exc_info} + + event = MagicMock() + + result = before_send(event, hint) + + # Assert that the event was dropped (None returned) + assert result is None + + +def test_before_send_passes_through_non_ignored_log(): + """Test that before_send passes through logs that don't contain ignored exceptions.""" + log_record = MagicMock() + log_record.msg = "Some other error message" + log_record.levelno = logging.ERROR # 40 + + hint = {"log_record": log_record} + + event = MagicMock() + + result = before_send(event, hint) + + # Assert that the event was passed through + assert result == event + + +def test_before_send_passes_through_non_ignored_exception(): + """Test that before_send passes through exceptions that don't contain ignored exceptions.""" + exc_info = (Exception, Exception("Some other error message"), None) + + hint = {"exc_info": exc_info} + + event = MagicMock() + + result = before_send(event, hint) + + # Assert that the event was passed through + assert result == event + + +def test_before_send_handles_warning_level(): + """Test that before_send handles warning level logs.""" + log_record = MagicMock() + log_record.msg = "Provider kubernetes is not connected" + log_record.levelno = logging.WARNING # 30 + + hint = {"log_record": log_record} + + event = MagicMock() + + result = before_send(event, hint) + + # Assert that the event was dropped (None returned) + assert result is None diff --git a/api/src/backend/config/settings/sentry.py b/api/src/backend/config/settings/sentry.py index 87d08bd6ee..648324707f 100644 --- a/api/src/backend/config/settings/sentry.py +++ b/api/src/backend/config/settings/sentry.py @@ -79,9 +79,16 @@ def before_send(event, hint): log_msg = hint["log_record"].msg log_lvl = hint["log_record"].levelno - # Handle Error events and discard the rest - if log_lvl == 40 and any(ignored in log_msg for ignored in IGNORED_EXCEPTIONS): - return + # Handle Error and Critical events and discard the rest + if log_lvl <= 40 and any(ignored in log_msg for ignored in IGNORED_EXCEPTIONS): + return None # Explicitly return None to drop the event + + # Ignore exceptions with the ignored_exceptions + if "exc_info" in hint and hint["exc_info"]: + exc_value = str(hint["exc_info"][1]) + if any(ignored in exc_value for ignored in IGNORED_EXCEPTIONS): + return None # Explicitly return None to drop the event + return event