kaxil commented on code in PR #67943:
URL: https://github.com/apache/airflow/pull/67943#discussion_r4710783534


##########
task-sdk/tests/task_sdk/execution_time/test_task_runner.py:
##########
@@ -6756,6 +6756,48 @@ def test_ti_finish_metric_emitted_for_terminal_states(
                 },
             )
 
+    @pytest.mark.parametrize(
+        ("task_callable", "expected_state"),
+        [
+            pytest.param(lambda: "success", "success", id="success"),
+            pytest.param(lambda: (_ for _ in 
()).throw(AirflowSkipException()), "skipped", id="skipped"),
+            pytest.param(lambda: (_ for _ in 
()).throw(AirflowFailException("fail")), "failed", id="failed"),
+        ],
+    )
+    def test_task_duration_metric_emitted_for_terminal_states(
+        self, task_callable, expected_state, create_runtime_ti, 
mock_supervisor_comms
+    ):
+        """task.duration is emitted for every terminal state — success, 
skipped, failed."""
+        task = PythonOperator(task_id="test", python_callable=task_callable)
+        ti = create_runtime_ti(task=task)
+
+        context = ti.get_template_context()
+        with 
mock.patch("airflow.sdk._shared.observability.metrics.stats._get_backend") as 
mock_get_backend:
+            backend = mock.MagicMock(spec=StatsLogger)
+            mock_get_backend.return_value = backend
+            state, _, error = run(ti, context=context, log=mock.MagicMock())

Review Comment:
   `expected_state` only ends up in the assertion message. Can you add `assert 
state == expected_state` after `run()`? Without it `[skipped]` still passes if 
the skip ever lands on a failure path, and the skip path is the one this test 
is here for.



##########
task-sdk/tests/task_sdk/execution_time/test_task_runner.py:
##########
@@ -6756,6 +6756,48 @@ def test_ti_finish_metric_emitted_for_terminal_states(
                 },
             )
 
+    @pytest.mark.parametrize(
+        ("task_callable", "expected_state"),
+        [
+            pytest.param(lambda: "success", "success", id="success"),
+            pytest.param(lambda: (_ for _ in 
()).throw(AirflowSkipException()), "skipped", id="skipped"),
+            pytest.param(lambda: (_ for _ in 
()).throw(AirflowFailException("fail")), "failed", id="failed"),
+        ],
+    )
+    def test_task_duration_metric_emitted_for_terminal_states(
+        self, task_callable, expected_state, create_runtime_ti, 
mock_supervisor_comms
+    ):
+        """task.duration is emitted for every terminal state — success, 
skipped, failed."""
+        task = PythonOperator(task_id="test", python_callable=task_callable)
+        ti = create_runtime_ti(task=task)
+
+        context = ti.get_template_context()
+        with 
mock.patch("airflow.sdk._shared.observability.metrics.stats._get_backend") as 
mock_get_backend:
+            backend = mock.MagicMock(spec=StatsLogger)
+            mock_get_backend.return_value = backend
+            state, _, error = run(ti, context=context, log=mock.MagicMock())
+            finalize(ti, state=state, context=context, log=mock.MagicMock(), 
error=error)
+
+            # verify task.duration was emitted in tagged format
+            task_duration_calls = [
+                c for c in backend.timing.call_args_list if c.args and 
c.args[0] == "task.duration"
+            ]
+            assert len(task_duration_calls) == 1, (
+                f"Expected exactly 1 task.duration emit for 
state={expected_state}, "
+                f"got {len(task_duration_calls)}"
+            )
+            assert task_duration_calls[0].kwargs.get("tags") == {
+                "dag_id": ti.dag_id,
+                "task_id": ti.task_id,
+                "run_type": "manual",
+            }
+            # verify task.duration was also emitted in legacy dotted format 
via the
+            # registry's legacy_name derivation in metrics_template.yaml. 
dag_id and
+            # task_id are consumed by the legacy name, leaving run_type as a 
tag.
+            backend.timing.assert_any_call(

Review Comment:
   This checks the count and tags but not the value, so dropping the `* 1000` 
or flipping the subtraction in `finalize()` would still pass. Taking the 
`time_machine` fixture (the module import has no `move_to`) and calling 
`time_machine.move_to(ti.start_date + timedelta(seconds=5), tick=False)` before 
`run()` would let both calls assert the exact duration in ms, and the filtered 
list could become one `== [mock.call("task.duration", expected_ms, 
tags={...})]`. Not blocking, since the regression here was the metric going 
missing rather than a wrong value.



##########
task-sdk/tests/task_sdk/execution_time/test_task_runner.py:
##########
@@ -6756,6 +6756,48 @@ def test_ti_finish_metric_emitted_for_terminal_states(
                 },
             )
 
+    @pytest.mark.parametrize(
+        ("task_callable", "expected_state"),
+        [
+            pytest.param(lambda: "success", "success", id="success"),
+            pytest.param(lambda: (_ for _ in 
()).throw(AirflowSkipException()), "skipped", id="skipped"),
+            pytest.param(lambda: (_ for _ in 
()).throw(AirflowFailException("fail")), "failed", id="failed"),
+        ],

Review Comment:
   The `ti.finish` test above also has a `zero_division` row (`lambda: 1 / 0`). 
That one goes through `except BaseException` and `_handle_current_task_failed`, 
which sets `end_date` on its own path, so could it go here too? Sharing one 
param list between the two tests would also stop them drifting apart again.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to