Is there a way to get the name of a Dag that triggered another dag?

I'm looking to get access to the name of a Dag that triggers another Dag from the triggered Dag, for logging purposes. I have been looking in the airflow documentation for DagRun and context but have yet to find a way.


Solution 1:

You could add the "triggering" DAG id to the TriggerDagRunOperator's conf using a templated string:

TriggerDagRunOperator(conf={"triggering_dag_id": "{{ dag.dag_id }}"}, ...)

And fetch the value from the triggered DAGRun conf. For example:

def do_something(**context):
    print(context["dag_run"].conf["triggering_dag_id"])

print_triggering_dag = PythonOperator(python_callable=do_something, ...)

If you don't want to print the conf, you call also inspect the conf in the UI under Browse -> DAG Runs -> rightmost column.