This is an automated email from the ASF dual-hosted git repository.
kaxil 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 7d9f803ea4b Emit per-task StatsD names for the `ti.awaiting_input`
gauge (#76737)
7d9f803ea4b is described below
commit 7d9f803ea4b23991b4ee471ac735b8156b2d44ce
Author: Tung, Hsiao-Yu <[email protected]>
AuthorDate: Thu Nov 26 20:57:07 2026 +0800
Emit per-task StatsD names for the `ti.awaiting_input` gauge (#76737)
* Emit per-task StatsD names for the ti.awaiting_input gauge
The scheduler emits ti.awaiting_input alongside the other task-state
gauges, but the metric was missing from the registry, so no legacy
name was exported. Plain StatsD drops tags, so every Dag and task
wrote to the same series and the value was only the last one sent.
* Check every scheduler TI gauge state has a legacy StatsD name
A state added to the scheduler's task-instance gauges without a registry
legacy_name passes the metrics prek hook but silently collapses every
Dag and task into one series on plain StatsD. Also keep the scheduler
docs in sync with the config description.
* Require per-task variables in TI gauge legacy name check
A legacy name without dag_id and task_id still merges every task into one
StatsD series, so checking only that a legacy name exists did not catch the
gap. The check reads only the metrics registry, so running it inside the DB
test class paid for a DB cleanup around every case.
---
.../docs/administration-and-deployment/scheduler.rst | 8 ++++----
airflow-core/src/airflow/config_templates/config.yml | 4 ++--
airflow-core/src/airflow/jobs/scheduler_job_runner.py | 10 +++++++---
airflow-core/tests/unit/jobs/test_scheduler_job.py | 12 ++++++++++++
.../observability/metrics/metrics_template.yaml | 6 ++++++
.../observability/tests/observability/metrics/test_stats.py | 9 +++++----
6 files changed, 36 insertions(+), 13 deletions(-)
diff --git a/airflow-core/docs/administration-and-deployment/scheduler.rst
b/airflow-core/docs/administration-and-deployment/scheduler.rst
index db329bfe5cc..1bcf7dab90b 100644
--- a/airflow-core/docs/administration-and-deployment/scheduler.rst
+++ b/airflow-core/docs/administration-and-deployment/scheduler.rst
@@ -263,10 +263,10 @@ However, you can also look at other
non-performance-related scheduler configurat
- :ref:`config:scheduler__ti_metrics_interval`
- How often (in seconds) should task instance (scheduled, queued, running and
deferred) stats be sent to StatsD
- (if statsd_on is enabled). This is a *relatively* expensive query to compute
- this, so this should be set to match the same period as your StatsD roll-up
- period.
+ How often (in seconds) should task instance (scheduled, queued, running,
deferred and
+ awaiting_input) stats be sent to StatsD (if statsd_on is enabled). This is a
*relatively*
+ expensive query to compute this, so this should be set to match the same
period as your
+ StatsD roll-up period.
- :ref:`config:scheduler__orphaned_tasks_check_interval`
diff --git a/airflow-core/src/airflow/config_templates/config.yml
b/airflow-core/src/airflow/config_templates/config.yml
index 70b0b9c75ab..1fe1c50215c 100644
--- a/airflow-core/src/airflow/config_templates/config.yml
+++ b/airflow-core/src/airflow/config_templates/config.yml
@@ -2826,8 +2826,8 @@ scheduler:
default: "5.0"
ti_metrics_interval:
description: |
- How often (in seconds) should task instance (scheduled, queued,
running and deferred) stats
- be sent to StatsD (if statsd_on is enabled)
+ How often (in seconds) should task instance (scheduled, queued,
running, deferred and
+ awaiting_input) stats be sent to StatsD (if statsd_on is enabled)
version_added: 3.0.0
type: float
example: ~
diff --git a/airflow-core/src/airflow/jobs/scheduler_job_runner.py
b/airflow-core/src/airflow/jobs/scheduler_job_runner.py
index 9f3ea9db915..b9026cfacf1 100644
--- a/airflow-core/src/airflow/jobs/scheduler_job_runner.py
+++ b/airflow-core/src/airflow/jobs/scheduler_job_runner.py
@@ -171,6 +171,11 @@ DM = DagModel
TASK_STUCK_IN_QUEUED_RESCHEDULE_EVENT = "stuck in queued reschedule"
""":meta private:"""
+TI_METRIC_STATES = frozenset(
+ {State.SCHEDULED, State.QUEUED, State.RUNNING, State.DEFERRED,
State.AWAITING_INPUT}
+)
+""":meta private:"""
+
# Per-tick cap on pending AssetPartitionDagRun rows the scheduler evaluates.
# Bounds the per-tick transaction so executor heartbeats and regular scheduling
@@ -4028,7 +4033,6 @@ class SchedulerJobRunner(BaseJobRunner, LoggingMixin):
@provide_session
def _emit_ti_metrics(self, *, session: Session = NEW_SESSION) -> None:
- metric_states = {State.SCHEDULED, State.QUEUED, State.RUNNING,
State.DEFERRED, State.AWAITING_INPUT}
stmt = (
select(
TaskInstance.state,
@@ -4037,7 +4041,7 @@ class SchedulerJobRunner(BaseJobRunner, LoggingMixin):
TaskInstance.queue,
func.count(TaskInstance.task_id).label("count"),
)
- .filter(TaskInstance.state.in_(metric_states))
+ .filter(TaskInstance.state.in_(TI_METRIC_STATES))
.group_by(TaskInstance.state, TaskInstance.dag_id,
TaskInstance.task_id, TaskInstance.queue)
)
all_states_metric = session.execute(stmt).all()
@@ -4048,7 +4052,7 @@ class SchedulerJobRunner(BaseJobRunner, LoggingMixin):
else:
dag_id_to_team_name = {}
- for state in metric_states:
+ for state in TI_METRIC_STATES:
if state not in self.previous_ti_metrics:
self.previous_ti_metrics[state] = {}
diff --git a/airflow-core/tests/unit/jobs/test_scheduler_job.py
b/airflow-core/tests/unit/jobs/test_scheduler_job.py
index 34f1857d9f6..46cf444bed6 100644
--- a/airflow-core/tests/unit/jobs/test_scheduler_job.py
+++ b/airflow-core/tests/unit/jobs/test_scheduler_job.py
@@ -47,6 +47,7 @@ from sqlalchemy.orm import joinedload
from airflow import settings
from airflow._shared.module_loading import qualname
from airflow._shared.observability.metrics.base_stats_logger import StatsLogger
+from airflow._shared.observability.metrics.metrics_registry import
MetricsRegistry
from airflow._shared.timezones import timezone
from airflow.api_fastapi.auth.tokens import JWTGenerator
from airflow.api_fastapi.execution_api.datamodels.taskinstance import
TaskInstance as TIDataModel
@@ -73,6 +74,7 @@ from airflow.jobs.job import EXECUTOR_JOB_TYPE, Job, run_job
from airflow.jobs.scheduler_job_runner import (
MAX_TI_REFILL_QUERIES_PER_LOOP,
TASK_STUCK_IN_QUEUED_RESCHEDULE_EVENT,
+ TI_METRIC_STATES,
SchedulerJobRunner,
)
from airflow.listeners.listener import get_listener_manager
@@ -13301,6 +13303,16 @@ class TestSchedulerJob:
assert ti.state != State.NONE
[email protected]_db_test_override
[email protected]("state", sorted(TI_METRIC_STATES))
+def test_ti_metric_state_has_legacy_name(state):
+ """Plain StatsD drops tags, so a gauge without a legacy name merges every
task into one series."""
+ metric = MetricsRegistry().get(f"ti.{state}")
+ assert metric is not None
+ assert metric.get("legacy_name", "-") != "-"
+ assert {"dag_id", "task_id"} <= set(metric.get("name_variables", []))
+
+
@pytest.mark.need_serialized_dag
def test_schedule_dag_run_with_upstream_skip(dag_maker, session):
"""
diff --git
a/shared/observability/src/airflow_shared/observability/metrics/metrics_template.yaml
b/shared/observability/src/airflow_shared/observability/metrics/metrics_template.yaml
index bab312f39e4..c382da9b4d3 100644
---
a/shared/observability/src/airflow_shared/observability/metrics/metrics_template.yaml
+++
b/shared/observability/src/airflow_shared/observability/metrics/metrics_template.yaml
@@ -752,6 +752,12 @@ metrics:
legacy_name: "ti.deferred.{queue}.{dag_id}.{task_id}"
name_variables: ["queue", "dag_id", "task_id"]
+ - name: "ti.awaiting_input"
+ description: "Number of tasks waiting for human input in a given Dag."
+ type: "gauge"
+ legacy_name: "ti.awaiting_input.{queue}.{dag_id}.{task_id}"
+ name_variables: ["queue", "dag_id", "task_id"]
+
- name: "ol.event.size"
description: "Size in bytes of an OpenLineage event by event type and
operator."
type: "gauge"
diff --git a/shared/observability/tests/observability/metrics/test_stats.py
b/shared/observability/tests/observability/metrics/test_stats.py
index d9fe1ee4504..1cd68d17f00 100644
--- a/shared/observability/tests/observability/metrics/test_stats.py
+++ b/shared/observability/tests/observability/metrics/test_stats.py
@@ -750,16 +750,17 @@ class TestLegacyExport:
tags={"operator_name": "EmptyOperator", "dag_id": "test_dag"},
)
- def test_gauge_emits_both_when_legacy_name_exists(self, mock_backend):
+ @pytest.mark.parametrize("stat", ["ti.scheduled", "ti.awaiting_input"])
+ def test_gauge_emits_both_when_legacy_name_exists(self, mock_backend,
stat):
airflow_shared.observability.metrics.stats.gauge(
- "ti.scheduled",
+ stat,
42,
tags={"queue": "default", "dag_id": "test_dag", "task_id":
"test_task"},
)
assert mock_backend.gauge.call_count == 2
-
mock_backend.gauge.assert_any_call("ti.scheduled.default.test_dag.test_task",
42)
+
mock_backend.gauge.assert_any_call(f"{stat}.default.test_dag.test_task", 42)
mock_backend.gauge.assert_any_call(
- "ti.scheduled",
+ stat,
42,
tags={"queue": "default", "dag_id": "test_dag", "task_id":
"test_task"},
)