Skip to content

Commit a0be43d

Browse files
kacpermudaclaude
andauthored
Link task instances of task loops to their own page (#75458)
A task instance's log link ignored its loop iteration: the one Airflow builds put the iteration where the UI expects a map index, pointing at a page that doesn't exist, and the one the Task SDK builds couldn't tell the iterations of a loop apart. Failure emails, notifiers and OpenLineage events carried these links. Co-authored-by: Claude <[email protected]>
1 parent 99bdeab commit a0be43d

4 files changed

Lines changed: 64 additions & 13 deletions

File tree

‎airflow-core/src/airflow/models/taskinstance.py‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from collections.abc import Callable, Collection, Iterable, Mapping, Sequence
2828
from datetime import datetime, timedelta
2929
from typing import TYPE_CHECKING, Any, NamedTuple
30-
from urllib.parse import quote
30+
from urllib.parse import quote, urlencode
3131
from uuid import UUID
3232

3333
import attrs
@@ -1428,11 +1428,20 @@ def log_url(self) -> str:
14281428
"""Log URL for TaskInstance."""
14291429
run_id = quote(self.run_id)
14301430
base_url = conf.get("api", "base_url", fallback="http://localhost:8080/")
1431-
map_index = f"/mapped/{self.region_index}" if self.region_index >= 0 else ""
1432-
try_number = f"?try_number={self.try_number}" if self.try_number > 0 else ""
1433-
_log_uri = f"{base_url.rstrip('/')}/dags/{self.dag_id}/runs/{run_id}/tasks/{self.task_id}{map_index}{try_number}"
1434-
1435-
return _log_uri
1431+
_log_uri = f"{base_url.rstrip('/')}/dags/{self.dag_id}/runs/{run_id}/tasks/{self.task_id}"
1432+
query: dict[str, Any] = {}
1433+
if self.region_id == SENTINEL_REGION_ID:
1434+
if self.region_index >= 0:
1435+
_log_uri += f"/mapped/{self.region_index}"
1436+
else:
1437+
# In a region, region_index is a map index only for a mapped task (in a loop it is the iteration
1438+
# otherwise), and the UI selects the task instance by its region, which tells loop iterations apart
1439+
if (task := getattr(self, "task", None)) is not None and task.get_needs_expansion():
1440+
_log_uri += f"/mapped/{self.region_index}"
1441+
query.update(region_id=self.region_id, region_index=self.region_index)
1442+
if self.try_number > 0:
1443+
query["try_number"] = self.try_number
1444+
return f"{_log_uri}?{urlencode(query)}" if query else _log_uri
14361445

14371446
@property
14381447
def mark_success_url(self) -> str:

‎airflow-core/tests/unit/models/test_taskinstance.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1974,6 +1974,30 @@ def test_log_url(self, create_task_instance):
19741974
expected_url = "http://localhost:8080/dags/my_dag/runs/test/tasks/op/mapped/1?try_number=2"
19751975
assert ti.log_url == expected_url
19761976

1977+
@pytest.mark.parametrize(
1978+
("mapped", "mapped_path"),
1979+
[
1980+
pytest.param(False, "", id="loop_iteration"),
1981+
pytest.param(True, "/mapped/1", id="mapped"),
1982+
pytest.param(None, "", id="task_not_attached"),
1983+
],
1984+
)
1985+
def test_log_url_selects_a_task_instance_in_a_region_by_its_region(self, dag_maker, mapped, mapped_path):
1986+
with dag_maker("my_dag", serialized=True):
1987+
if mapped:
1988+
BashOperator.partial(task_id="op").expand(bash_command=["echo 1", "echo 2"])
1989+
else:
1990+
EmptyOperator(task_id="op")
1991+
ti = dag_maker.create_dagrun(run_id="test").task_instances[0]
1992+
ti.region_id, ti.region_index, ti.try_number = uuid6.uuid7(), 1, 2
1993+
if mapped is None:
1994+
ti.task = None
1995+
1996+
assert ti.log_url == (
1997+
f"http://localhost:8080/dags/my_dag/runs/test/tasks/op{mapped_path}"
1998+
f"?region_id={ti.region_id}&region_index=1&try_number=2"
1999+
)
2000+
19772001
@pytest.mark.parametrize(
19782002
"kwargs",
19792003
[

‎task-sdk/src/airflow/sdk/execution_time/task_runner.py‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from itertools import product
3333
from pathlib import Path
3434
from typing import TYPE_CHECKING, Annotated, Any, Literal, NoReturn, cast
35-
from urllib.parse import quote
35+
from urllib.parse import quote, urlencode
3636
from uuid import UUID
3737

3838
import attrs
@@ -964,12 +964,14 @@ def log_url(self) -> str:
964964
map_index = (
965965
f"/mapped/{map_index_value}" if map_index_value is not None and map_index_value >= 0 else ""
966966
)
967-
try_number_value = self.try_number
968-
try_number = (
969-
f"?try_number={try_number_value}" if try_number_value is not None and try_number_value > 0 else ""
970-
)
971-
_log_uri = f"{base_url.rstrip('/')}/dags/{self.dag_id}/runs/{run_id}/tasks/{self.task_id}{map_index}{try_number}"
972-
return _log_uri
967+
query: dict[str, Any] = {}
968+
# The UI selects a task instance in a region by its region, which tells loop iterations apart
969+
if self.region_id is not None and self.region_id.int != 0:
970+
query.update(region_id=self.region_id, region_index=self.region_index)
971+
if self.try_number is not None and self.try_number > 0:
972+
query["try_number"] = self.try_number
973+
_log_uri = f"{base_url.rstrip('/')}/dags/{self.dag_id}/runs/{run_id}/tasks/{self.task_id}{map_index}"
974+
return f"{_log_uri}?{urlencode(query)}" if query else _log_uri
973975

974976
@property
975977
def mark_success_url(self) -> str:

‎task-sdk/tests/task_sdk/execution_time/test_task_runner.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5895,6 +5895,22 @@ def test_runtime_task_instance_log_url_property(self, create_runtime_ti, base_ur
58955895
log_url = runtime_ti.log_url
58965896
assert log_url == expected_url
58975897

5898+
@pytest.mark.parametrize(("map_index", "mapped_path"), [(-1, ""), (1, "/mapped/1")])
5899+
def test_runtime_task_instance_log_url_selects_a_task_instance_in_a_region_by_its_region(
5900+
self, create_runtime_ti, map_index, mapped_path
5901+
):
5902+
task = BaseOperator(task_id="test_task")
5903+
runtime_ti = create_runtime_ti(
5904+
task=task, dag_id="test_dag", run_id="test_run", try_number=2, map_index=map_index
5905+
)
5906+
runtime_ti.region_id, runtime_ti.region_index = uuid7(), 1
5907+
5908+
with conf_vars({("api", "base_url"): "http://localhost:8080/"}):
5909+
assert runtime_ti.log_url == (
5910+
f"http://localhost:8080/dags/test_dag/runs/test_run/tasks/test_task{mapped_path}"
5911+
f"?region_id={runtime_ti.region_id}&region_index=1&try_number=2"
5912+
)
5913+
58985914
def test_task_runner_on_failure_callback_context(self, create_runtime_ti):
58995915
"""Test that on_failure_callback context has end_date and duration."""
59005916

0 commit comments

Comments
 (0)