Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR-6309 Fix possible recursion error caused by mandible logging config #32

Merged
merged 2 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ jobs:

steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: 3.9
- uses: TrueBrain/actions-flake8@v2
with:
flake8_version: 6.0.0
Expand Down
51 changes: 32 additions & 19 deletions mandible/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,22 @@
from contextlib import contextmanager


class LogRecordFactory:
def __init__(self, wrapped_factory: Callable, extra: dict):
self.wrapped_factory = wrapped_factory
self.extra = extra

def __call__(self, *args, **kwargs):
record = self.wrapped_factory(*args, **kwargs)
for key, value in self.extra.items():
setattr(record, key, value)
return record


def _build_cumulus_extras_from_cma(event: dict) -> dict:
event = event.get("cma", {}).get("event", {})
if "cma" in event:
event = event["cma"].get("event") or {}

return {
"cirrus_daac_version": os.getenv("DAAC_VERSION"),
"cirrus_core_version": os.getenv("CORE_VERSION"),
Expand All @@ -19,28 +33,27 @@ def init_custom_log_record_factory(
event: dict,
record_builder: Callable[[dict], dict] = _build_cumulus_extras_from_cma,
) -> None:
"""
configures the logging record factory and can be overwritten by providing a function that takes the event dict
as an input and returns a dict of log records.
Relies on the JSON formatter setting provided by AWS.
By default the callable returns:
{
"cirrus_daac_version": os.getenv("DAAC_VERSION"),
"cirrus_core_version": os.getenv("CORE_VERSION"),
"cumulus_version": event.get("cumulus_meta", {}).get("cumulus_version"),
"granule_name": event.get("payload", {}).get("granules", [{}])[0].get("granuleId"),
"workflow_execution_name": event.get("cumulus_meta", {}).get("execution_name"),
}
"""Configures the logging record factory and can be overwritten by providing
a function that takes the event dict as an input and returns a dict of log
records. Relies on the JSON formatter setting provided by AWS.

By default the callable returns:
{
"cirrus_daac_version": os.getenv("DAAC_VERSION"),
"cirrus_core_version": os.getenv("CORE_VERSION"),
"cumulus_version": event.get("cumulus_meta", {}).get("cumulus_version"),
"granule_name": event.get("payload", {}).get("granules", [{}])[0].get("granuleId"),
"workflow_execution_name": event.get("cumulus_meta", {}).get("execution_name"),
}
"""
extra = record_builder(event)
original_factory = logging.getLogRecordFactory()

def record_factory(*args, **kwargs):
record = original_factory(*args, **kwargs)
for key, value in extra.items():
setattr(record, key, value)
return record
logging.setLogRecordFactory(record_factory)
if isinstance(original_factory, LogRecordFactory):
original_factory.extra = extra
else:
record_factory = LogRecordFactory(original_factory, extra)
logging.setLogRecordFactory(record_factory)


def init_root_logger():
Expand Down
39 changes: 39 additions & 0 deletions tests/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,45 @@ def event_config():
}


def test_init_custom_log_record_factory_called_multiple_times(caplog):
for _ in range(1100):
init_custom_log_record_factory({})

with caplog.at_level(logging.INFO):
log = logging.getLogger(__name__)
log.info("TEST MESSAGE")
assert "TEST MESSAGE" in caplog.text


def test_init_custom_log_record_factory_update(caplog):
init_custom_log_record_factory({})

with caplog.at_level(logging.INFO):
log = logging.getLogger(__name__)
log.info("TEST 1")
assert caplog.records[0].cirrus_daac_version is None
assert caplog.records[0].cirrus_core_version is None
assert caplog.records[0].cumulus_version is None
assert caplog.records[0].granule_name is None
assert caplog.records[0].workflow_execution_name is None

init_custom_log_record_factory({
"cumulus_meta": {
"cumulus_version": "v0.0.0",
},
})

caplog.clear()
with caplog.at_level(logging.INFO):
log = logging.getLogger(__name__)
log.info("TEST 2")
assert caplog.records[0].cirrus_daac_version is None
assert caplog.records[0].cirrus_core_version is None
assert caplog.records[0].cumulus_version == "v0.0.0"
assert caplog.records[0].granule_name is None
assert caplog.records[0].workflow_execution_name is None


def test_custom_log_record_factory(caplog, monkeypatch, event_config):
monkeypatch.setenv("DAAC_VERSION", "TEST_DAAC")
monkeypatch.setenv("CORE_VERSION", "TEST_CORE")
Expand Down
Loading