Skip to content

Prevent malformed Elasticsearch log entries from crashing task log fetch - #69306

Merged
jason810496 merged 7 commits into
apache:mainfrom
hkc-8010:fix/es-log-handler-malformed-log-entry
Jul 15, 2026
Merged

Prevent malformed Elasticsearch log entries from crashing task log fetch#69306
jason810496 merged 7 commits into
apache:mainfrom
hkc-8010:fix/es-log-handler-malformed-log-entry

Conversation

@hkc-8010

@hkc-8010 hkc-8010 commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Summary

  • 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 change 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.

Changes

  • providers/elasticsearch/src/airflow/providers/elasticsearch/log/es_task_handler.py:
    wrap StructuredLogMessage construction in a try/except ValidationError with a
    stringified-event fallback and a warning log.
  • providers/elasticsearch/tests/unit/elasticsearch/log/test_es_task_handler.py: add
    coverage for malformed event handling and for read() surviving one bad hit among
    otherwise-valid hits.

PR Checklist

  • My PR is targeted at the main branch
  • Tests added/updated
  • prek hooks all pass
  • Unit tests pass locally

closes: #69304

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
hkc-8010 and others added 3 commits July 3, 2026 22:41
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.
@Vamsi-klu

Copy link
Copy Markdown
Contributor

Could the fallback avoid reusing the original timestamp when validation failed? Right now any ValidationError falls into the except block, but then it builds StructuredLogMessage(..., timestamp=fields.get("timestamp")). If the malformed field was timestamp rather than event, the fallback can raise the same validation error again and still fail the whole log fetch.


Drafted-by: Codex (GPT-5); reviewed by @Vamsi-klu before posting

@jason810496 jason810496 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM except the logging level, thanks.

@potiuk

potiuk commented Jul 11, 2026

Copy link
Copy Markdown
Member

@hkc-8010 — Some review feedback from @jason810496 is waiting on you: 1 unresolved review thread(s). Could you either push a fix or reply in each thread explaining why the feedback doesn't apply? When you believe the feedback is addressed, please mark the threads as resolved and ping the reviewer (@jason810496) for a final look. Thanks!


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.
@hkc-8010

Copy link
Copy Markdown
Contributor Author

@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 ElasticsearchJSONFormatter.formatTime() (OpenSearch uses the same pattern) from record.created, so it is always a valid ISO-8601 string. The only field a user can put arbitrary values in is event, for example logging a list or dict, which is what the fallback and the test cover. A timestamp that fails validation would have to come from documents written into the index by something outside Airflow's own formatter. That is out of scope for this fix, so I would rather keep the fallback narrow. If we want to harden against externally ingested log docs, I am happy to do that as a follow-up.

@hkc-8010
hkc-8010 requested a review from jason810496 July 13, 2026 12:49
@jason810496
jason810496 merged commit 3986d82 into apache:main Jul 15, 2026
97 checks passed
joshuabvarghese pushed a commit to joshuabvarghese/airflow that referenced this pull request Jul 16, 2026
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Malformed stored log entry crashes task log fetch API with unhandled ValidationError

4 participants