Javier Muñoz
Javier Muñoz

Reputation: 790

Having trouble to extract a DAG name into a JSON in Airflow

I'm trying to get the DAG name to the following JSON:

INFO - {'conf': <airflow.configuration.AutoReloadableProxy object at ... >, 'dag': <DAG: dag-name-i-want-to-get>, 'ds': '2021-07-29' ... N }

By the way, I got the JSON using the following function in Airflow:

def execute_dag_run(**kwargs):
  print(kwargs)
  dag = kwargs['dag']
  print(type(dag))
  print(dag)

get_dag_run_task = PythonOperator(
  task_id='get_dag_run', 
  python_callable=execute_dag_run, 
  dag=dag, 
  provide_context=True
)

However, I'm getting a class if I print type(dag):

INFO - <class 'airflow.models.dag.DAG'>

Do you have any idea how to get this without do a manual extraction?

Upvotes: 1

Views: 2822

Answers (1)

Elad Kalif
Elad Kalif

Reputation: 15979

You are printing the dag object if you want to get the dag name you need to get it from the dag object as:

def execute_dag_run(**kwargs):
    dag = kwargs['dag']
    print ("dag_id from dag:")
    print(dag.dag_id)   

Alternatively you can also get it from task_instance as:

def execute_dag_run(**kwargs):
  ti = kwargs['task_instance']
  print ("dag_id from task instance:")
  print (ti.dag_id)

another option is to get it from dag_run as:

def execute_dag_run(**kwargs):
  dag_run = kwargs['dag_run']
  print("dag_id from dag_run:")
  print (dag_run.dag_id)

Upvotes: 2

Related Questions