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


##########
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:
   Done in 55a31b4242: both tests now share `TERMINAL_STATE_CASES`, which 
includes `zero_division`.



##########
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:
   Done in 55a31b4242: added `assert state == expected_state` after `run()`.



##########
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:
   Done in 55a31b4242: time is frozen at `start_date + 5s` and both emits 
assert `5000.0`; dropping the `* 1000` fails the test.



-- 
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