This is an automated email from the ASF dual-hosted git repository.
shahar1 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git
The following commit(s) were added to refs/heads/main by this push:
new 3a524716f84 Use standard iso format for dates in the Kafka
EventProducer plugin messages (#76052)
3a524716f84 is described below
commit 3a524716f84251d6643dc9fa195c9abc85c60254
Author: Christos Bisias <[email protected]>
AuthorDate: Tue Nov 3 18:58:00 2026 +0200
Use standard iso format for dates in the Kafka EventProducer plugin
messages (#76052)
---
providers/apache/kafka/docs/changelog.rst | 8 +++++++
.../apache/kafka/plugins/event_producer.py | 13 +++++++----
.../apache/kafka/plugins/test_event_producer.py | 26 +++++++++++++++++++---
3 files changed, 40 insertions(+), 7 deletions(-)
diff --git a/providers/apache/kafka/docs/changelog.rst
b/providers/apache/kafka/docs/changelog.rst
index 985902100b1..b1c7030833f 100644
--- a/providers/apache/kafka/docs/changelog.rst
+++ b/providers/apache/kafka/docs/changelog.rst
@@ -27,6 +27,14 @@
Changelog
---------
+.. note::
+ The Kafka event producer plugin now publishes messages with
``schema_version`` 2. The ``logical_date``,
+ ``start_date`` and ``end_date`` fields of Dag run events are formatted with
``datetime.isoformat()``,
+ for example ``2026-10-30T14:40:05.123456+00:00``, instead of ``str()``,
which separated the date and
+ the time with a space. A missing date is now published as ``null`` instead
of an empty string. Update
+ consumers that parse these fields with a fixed pattern or check for ``""``.
A JSON Schema definition
+ that accepts the new values is ``{"type": ["string", "null"], "format":
"date-time"}``.
+
2.1.0
.....
diff --git
a/providers/apache/kafka/src/airflow/providers/apache/kafka/plugins/event_producer.py
b/providers/apache/kafka/src/airflow/providers/apache/kafka/plugins/event_producer.py
index fdf1b2065bd..d3177ebddb2 100644
---
a/providers/apache/kafka/src/airflow/providers/apache/kafka/plugins/event_producer.py
+++
b/providers/apache/kafka/src/airflow/providers/apache/kafka/plugins/event_producer.py
@@ -41,7 +41,7 @@ if TYPE_CHECKING:
log = logging.getLogger(__name__)
CONFIG_SECTION = "kafka_event_producer"
-SCHEMA_VERSION = 1
+SCHEMA_VERSION = 2
@lru_cache(maxsize=1)
@@ -278,6 +278,11 @@ def _now_iso() -> str:
return datetime.now(UTC).isoformat()
+def _format_datetime(value: datetime | None) -> str | None:
+ # isoformat() keeps the RFC 3339 "T" form so consumers can validate the
field as a standard date-time.
+ return value.isoformat() if value else None
+
+
# DagRun
def _produce_dr_message(event: str, dag_run: DagRun, msg: str) -> None:
dag_id = dag_run.dag_id
@@ -299,9 +304,9 @@ def _get_dr_payload(dag_run, msg) -> dict[str, Any]:
"dag_id": getattr(dag_run, "dag_id", None),
"run_id": getattr(dag_run, "run_id", None),
"run_type": str(getattr(dag_run, "run_type", "") or ""),
- "logical_date": str(getattr(dag_run, "logical_date", "") or ""),
- "start_date": str(getattr(dag_run, "start_date", "") or ""),
- "end_date": str(getattr(dag_run, "end_date", "") or ""),
+ "logical_date": _format_datetime(getattr(dag_run, "logical_date",
None)),
+ "start_date": _format_datetime(getattr(dag_run, "start_date", None)),
+ "end_date": _format_datetime(getattr(dag_run, "end_date", None)),
"msg": msg or "",
}
diff --git
a/providers/apache/kafka/tests/unit/apache/kafka/plugins/test_event_producer.py
b/providers/apache/kafka/tests/unit/apache/kafka/plugins/test_event_producer.py
index 8e4ed0fcf4e..99de98afda2 100644
---
a/providers/apache/kafka/tests/unit/apache/kafka/plugins/test_event_producer.py
+++
b/providers/apache/kafka/tests/unit/apache/kafka/plugins/test_event_producer.py
@@ -18,6 +18,7 @@ from __future__ import annotations
import json
from collections.abc import Callable
+from datetime import UTC, datetime
from unittest.mock import MagicMock, patch
import pytest
@@ -202,12 +203,31 @@ def test_produce_dr_message(
body = _assert_common_message_fields(kafka_producer_mock, expected_event)
assert body["run_type"] == _RUN_TYPE
- assert body["logical_date"] == ""
- assert body["start_date"] == ""
- assert body["end_date"] == ""
+ assert body["logical_date"] is None
+ assert body["start_date"] is None
+ assert body["end_date"] is None
assert body["msg"] == ""
+@conf_vars(
+ {
+ ("kafka_event_producer", "dag_run_events_enabled"): "True",
+ ("kafka_event_producer", "topic"): _KAFKA_TOPIC,
+ ("kafka_event_producer", "source"): _SOURCE,
+ }
+)
+def test_produce_dr_message_formats_dates_as_iso8601(dr_mock,
kafka_producer_mock):
+ dr_mock.logical_date = None
+ dr_mock.start_date = datetime(2026, 10, 30, 14, 40, 5, 123456, tzinfo=UTC)
+
+ DagRunListener().on_dag_run_running(dag_run=dr_mock, msg="")
+
+ body = _assert_common_message_fields(kafka_producer_mock,
"dag_run.running")
+ assert body["logical_date"] is None
+ assert body["start_date"] == "2026-10-30T14:40:05.123456+00:00"
+ assert body["end_date"] is None
+
+
@pytest.mark.parametrize(
("ti_event_function", "expected_event", "extra_kwargs"),
[