airflow icon indicating copy to clipboard operation
airflow copied to clipboard

Enhancement: Support Dynamic dag_id Resolution in TriggerDagRunOperator for Clickable Links

Open pauloenrique071 opened this issue 1 year ago • 10 comments

Body

Currently, when using TriggerDagRunOperator with a dynamically determined dag_id (e.g., from XCom or templated fields), the Airflow web UI cannot generate a proper link to the triggered DAG at parse time. This results in a broken or invalid link (e.g., {{ task_instance.xcom_pull(...) }}) or a “DAG not found” error, even though the triggered DAG actually runs successfully.

Use Case / Motivation

We have pipelines that need to trigger different DAGs dynamically, depending on runtime conditions (e.g., data or metadata that arrives in real-time).
While the operator itself correctly triggers the desired DAG, the UI link to the triggered DAG is not usable.
Users rely on the UI link to quickly navigate to the triggered DAG’s run details, so a broken link complicates monitoring and debugging.

Proposed Solution

Allow the Airflow UI to update or determine the dag_id link after the task is executed, possibly by reading from XCom or storing the resolved dag_id in the metadata DB.
Provide a mechanism (e.g., an extra link or a new operator parameter) to supply the final dag_id to the UI once it’s known at runtime.
Alternatively, enhance TriggerDagRunOperator to set the correct link in the UI if dag_id is determined during the operator’s execution.
def decide_which_dag(**kwargs):
    # Some dynamic logic here
    return "some_dynamic_dag_id"

trigger_dynamic = TriggerDagRunOperator(
    task_id="trigger_dynamic_dag",
    trigger_dag_id="{{ python_callable_returned_id }}",  # or from XCom
    wait_for_completion=True,
    dag=dag,
)

in the above scenario, the DAG is triggered correctly, but the link displayed in the UI is not valid.

pauloenrique071 avatar Feb 03 '25 23:02 pauloenrique071

Thanks for opening your first issue here! Be sure to follow the issue template! If you are willing to raise PR to address this issue please do so, no need to wait for approval.

boring-cyborg[bot] avatar Feb 03 '25 23:02 boring-cyborg[bot]

Can I work on this?

alectric-tr avatar Feb 11 '25 01:02 alectric-tr

Can I work on this?

Sure! Feel free to work on it. Let me know if you need any help or clarification!

pauloenrique071 avatar Feb 11 '25 01:02 pauloenrique071

Hello is this still being worked on, if it is available can I work on this ?

aarochuk avatar Mar 07 '25 17:03 aarochuk

Hello is this still being worked on, if it is available can I work on this ?

Yes!

pauloenrique071 avatar Mar 10 '25 14:03 pauloenrique071

Hi @pauloenrique071 , I already explored and reproduced the issue and interested in working on this, so can I proceed?

s-vasu avatar Apr 04 '25 04:04 s-vasu

Hi @pauloenrique071 , I already explored and reproduced the issue and interested in working on this, so can I proceed?

Sure, feel free!

pauloenrique071 avatar Apr 04 '25 04:04 pauloenrique071

Hey, just wanted to check -- is this still being worked on?

AnishSJai avatar Sep 10 '25 14:09 AnishSJai

@pauloenrique071 Could you please review my PR when you get chance? Thanks!

hwang-cadent avatar Oct 24 '25 13:10 hwang-cadent

Hi @potiuk @pauloenrique071,

I'd like to pick this up and submit a fresh, minimal PR.

I reviewed the previous iteration and review feedback on #56973. My proposed implementation keeps the scope strictly focused on resolving the dynamic DAG link:

  1. Airflow 3 Execution Time (task_runner.py): Push trigger_dag_id alongside trigger_run_id in _handle_trigger_dag_run only after a successful trigger response. This ensures failure-mode state stays consistent across both XComs and prevents ghost values.
  2. Operator Link (trigger_dagrun.py): In TriggerDagRunLink.get_link(), check XCom.get_value(ti_key=ti_key, key=XCOM_DAG_ID) using an explicit is None identity check, falling back to operator.trigger_dag_id if absent. Also push XCOM_DAG_ID in _trigger_dag_af_2 for Airflow 2 compatibility.
  3. Tests: Update test_trigger_dagrun.py and test_task_runner.py while strictly preserving ordered call assertions (assert_has_calls) rather than loose matching.

I will open a focused PR shortly!

SachinyadavAug20 avatar Nov 16 '26 06:11 SachinyadavAug20