Airflow's ti.awaiting_input wasn't registered, so every task overwrote the same number
My second merged Apache Airflow PR. The ti.awaiting_input gauge had no per-task StatsD name, so with plain StatsD every Dag and task wrote to one series. The fix was six lines of YAML; review turned my one-state test into a check on every state.
The gauge
Every 30 seconds by default ([scheduler] ti_metrics_interval), the Airflow scheduler counts task
instances by state, Dag, task and queue, and sends each count as a gauge: ti.scheduled,
ti.queued, ti.running, ti.deferred. Since
#68028 added a human-in-the-loop state,
it also sends ti.awaiting_input: how many tasks are waiting for a person to respond.
This is the same metrics area as my first Airflow PR, and the bug is the same shape: nothing errors, the number is just wrong.
Why every task wrote to the same series
Airflow sends a metric two ways when the registry knows its legacy name. The new name carries the details as tags:
ti.awaiting_input with dag_id, task_id and queue. The legacy name puts them into
the metric name itself: ti.awaiting_input.default.my_dag.my_task. Legacy names aren’t
built from a pattern in code. They’re looked up in a metrics registry
(metrics_template.yaml), and if a metric has no entry, no legacy name is sent.
ti.awaiting_input had no entry. That only matters for one kind of backend, but it’s a
common one: plain StatsD, which drops tags, and which the Helm chart uses by default. There,
the only thing that arrived was the bare ti.awaiting_input. Dag A sends 3, Dag B sends 1,
and the gauge reads 1. Its value was whichever count was sent last.
I checked this by recording what a plain StatsD logger actually receives for two Dags.
Before the fix: only ti.awaiting_input, with 3.0 overwritten by 1.0. After: the bare name
plus ti.awaiting_input.default.dag_a.task_1 and ti.awaiting_input.default.dag_b.task_2.
Backends that keep tags were never affected.
The fix, and why the check missed it
The fix is one registry entry, copied from ti.deferred:
The more interesting question was why nothing caught it. Airflow has a prek hook,
check-metrics-synced-with-registry, that is supposed to fail when code sends a metric the
registry doesn’t know. But the scheduler builds this name at run time, f"ti.{state}", and
the hook matched that pattern against the existing ti.* entries. ti.scheduled was in
the registry, so the pattern counted as covered. The check passed for a reason that had
nothing to do with awaiting_input.
Review: test the list, not the case
My first test added ti.awaiting_input to an existing gauge test, parametrizing it over both names. It failed without
the registry entry and passed with it, which is what I usually check. kaxil, the PMC member
who also reviewed #67592, pointed out that it only pinned this one state. The next state
added to the scheduler’s list would hit the same gap, for the same reason the hook missed
this one.
So the set of states moved out of _emit_ti_metrics into a module-level constant,
TI_METRIC_STATES, and the test now walks that constant:
@pytest.mark.non_db_test_override
@pytest.mark.parametrize("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", []))
The second round tightened it twice more. The last assertion was his suggestion: a legacy
name without dag_id and task_id would still merge every task into one series, which is
exactly the bug, so having a legacy name isn’t enough. And the test first lived inside
TestSchedulerJob, which cleans the database before and after every case. It never touches
the database, so it became a module-level function marked non_db_test_override.
He also noticed the ti_metrics_interval description was copied by hand into
scheduler.rst and still listed only four states, so both now say awaiting_input too.
One thing is still open: the Helm chart’s StatsD exporter needs a mapping for the new legacy name before it shows up properly in Prometheus. That’s a separate PR.
What I’ll remember next time
Find out why it passed, not just that it did. Here the check matched a dynamic name
against a wildcard, so any ti.* entry made every state look registered.
Test the list. A test that pins the one case I fixed passes today and says nothing about the next item someone adds.
Assert the property that prevents the bug (per-task variables in the name), not a proxy for it (a legacy name exists).
I wrote this PR with Claude Code (Opus 5.5) and disclosed it with Generated-by: in the
PR, as Airflow’s guidelines ask.