Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions airflow-core/newsfragments/67765.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add the ``run_type`` tag to the ``dagrun.duration.failed`` metric emitted when a Dag run times out, making it consistent with the same metric emitted on normal Dag run completion.
4 changes: 3 additions & 1 deletion airflow-core/src/airflow/jobs/scheduler_job_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2457,7 +2457,9 @@ def _schedule_dag_run(
stats.timing(
"dagrun.duration.failed",
duration,
tags={"dag_id": dag_run.dag_id},
# Include ``run_type`` (via stats_tags) so the timeout path matches
# DagRun._emit_duration_stats_for_finished_state; see #64765.
tags=dag_run.stats_tags,
)
return callback_to_execute

Expand Down
39 changes: 39 additions & 0 deletions airflow-core/tests/unit/jobs/test_scheduler_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -3835,6 +3835,45 @@ def test_dagrun_timeout_fails_run(self, dag_maker):
session.rollback()
session.close()

@mock.patch("airflow._shared.observability.metrics.stats._get_backend")
def test_dagrun_timeout_duration_metric_has_run_type(self, mock_get_backend, dag_maker):
"""
The ``dagrun.duration.failed`` metric emitted when a Dag run times out must carry the
``run_type`` tag, matching the metric emitted on normal Dag run completion via
``DagRun._emit_duration_stats_for_finished_state``. Regression test for #64765.
"""
mock_stats = mock.MagicMock(spec=StatsLogger)
mock_get_backend.return_value = mock_stats

session = settings.Session()
with dag_maker(
dag_id="test_dagrun_timeout_duration_metric",
dagrun_timeout=datetime.timedelta(seconds=60),
session=session,
):
EmptyOperator(task_id="dummy")

dr = dag_maker.create_dagrun(start_date=timezone.utcnow() - datetime.timedelta(days=1))

scheduler_job = Job()
self.job_runner = SchedulerJobRunner(job=scheduler_job)

self.job_runner._schedule_dag_run(dr, session)
session.flush()

session.refresh(dr)
assert dr.state == State.FAILED

# The timing call must include both dag_id and run_type (dag_run.stats_tags), not just dag_id.
mock_stats.timing.assert_any_call(
"dagrun.duration.failed",
mock.ANY,
tags={"dag_id": dr.dag_id, "run_type": dr.run_type},
)

session.rollback()
session.close()

def test_dagrun_timeout_fails_run_and_update_next_dagrun(self, dag_maker):
"""
Test that dagrun timeout fails run and update the next dagrun
Expand Down
Loading