Prevent malformed Elasticsearch log entries from crashing task log fetch - #69306
Conversation
A single stored log entry with a non-string `event` field (for example, a task that logs a list or dict as the sole message argument) currently crashes the entire task-log-fetch request with an unhandled pydantic.ValidationError, instead of degrading gracefully. _read() built StructuredLogMessage objects from stored Elasticsearch hits without catching validation failures. This catches ValidationError per hit and falls back to a stringified event, matching the existing fallback pattern in file_task_handler.py's _log_stream_to_parsed_log_stream. closes: apache#69304
The new StructuredLogMessage fallback tests exercise an Airflow 3-only type, but the provider compatibility matrix still runs this provider against Airflow 2.11.1. Guarding those tests the same way as the runtime path keeps the compatibility job focused on behavior that actually exists in that release line.
The malformed log-entry regression test was unintentionally exercising Airflow 3's live-log delegation path because the test task instance was still running. Marking the task instance successful keeps the test on the Elasticsearch-read path it is intended to cover, which matches the provider compatibility and lowest-dependencies jobs.
|
Could the fallback avoid reusing the original Drafted-by: Codex (GPT-5); reviewed by @Vamsi-klu before posting |
jason810496
left a comment
There was a problem hiding this comment.
LGTM except the logging level, thanks.
|
@hkc-8010 — Some review feedback from Note: This comment was drafted by an AI-assisted triage tool and may contain mistakes. Once you have addressed the points above, an Apache Airflow maintainer — a real person — will take the next look at your PR. We use this two-stage triage process so that our maintainers' limited time is spent where it matters most: the conversation with you. |
_safe_build_structured_log_message runs once per stored hit inside the _read comprehension, so logging the fallback at warning level fires once per malformed line within a single log-fetch request. Drop it to debug so a request with many malformed entries does not flood the logs.
|
@Vamsi-klu Good catch on the code path. I left the timestamp as-is on purpose here: in both handlers the timestamp is not user data. It is written by |
…tch (apache#69306) * Prevent malformed Elasticsearch log entries from crashing task log fetch A single stored log entry with a non-string `event` field (for example, a task that logs a list or dict as the sole message argument) currently crashes the entire task-log-fetch request with an unhandled pydantic.ValidationError, instead of degrading gracefully. _read() built StructuredLogMessage objects from stored Elasticsearch hits without catching validation failures. This catches ValidationError per hit and falls back to a stringified event, matching the existing fallback pattern in file_task_handler.py's _log_stream_to_parsed_log_stream. closes: apache#69304 * Fix Elasticsearch compat tests on Airflow 2.11 The new StructuredLogMessage fallback tests exercise an Airflow 3-only type, but the provider compatibility matrix still runs this provider against Airflow 2.11.1. Guarding those tests the same way as the runtime path keeps the compatibility job focused on behavior that actually exists in that release line. * Fix Elasticsearch compat test for finished tasks The malformed log-entry regression test was unintentionally exercising Airflow 3's live-log delegation path because the test task instance was still running. Marking the task instance successful keeps the test on the Elasticsearch-read path it is intended to cover, which matches the provider compatibility and lowest-dependencies jobs. * Lower malformed-log fallback logging to debug level _safe_build_structured_log_message runs once per stored hit inside the _read comprehension, so logging the fallback at warning level fires once per malformed line within a single log-fetch request. Drop it to debug so a request with many malformed entries does not flood the logs.
Summary
eventfield (for example, a task thatlogs a list or dict as the sole message argument) currently crashes the entire
task-log-fetch request with an unhandled pydantic.ValidationError, instead of
degrading gracefully.
_read()builtStructuredLogMessageobjects from stored Elasticsearch hits withoutcatching validation failures. This change catches
ValidationErrorper hit and fallsback to a stringified event, matching the existing fallback pattern in
file_task_handler.py's_log_stream_to_parsed_log_stream.Changes
providers/elasticsearch/src/airflow/providers/elasticsearch/log/es_task_handler.py:wrap
StructuredLogMessageconstruction in a try/exceptValidationErrorwith astringified-event fallback and a warning log.
providers/elasticsearch/tests/unit/elasticsearch/log/test_es_task_handler.py: addcoverage for malformed
eventhandling and forread()surviving one bad hit amongotherwise-valid hits.
PR Checklist
mainbranchprekhooks all passcloses: #69304