@@ -25,6 +25,18 @@ def assert_metric(role, role_group, metric):
2525 return metric in metric_response .text
2626
2727
28+ # Check if dag run state is "success"
29+ # TODO: in future, we could wait on it.
30+ # See: https://airflow.apache.org/docs/apache-airflow/3.1.6/stable-rest-api-ref.html#operation/wait_dag_run_until_finished
31+ def assert_completion (rest_url , headers , dag_id , dag_run_id ):
32+ dag_run_response = requests .get (
33+ f"{ rest_url } /dags/{ dag_id } /dagRuns/{ dag_run_id } " , headers = headers
34+ )
35+ dag_run_state = dag_run_response .json ()["state" ]
36+ print (f"DAG RUN STATE: { dag_run_state } " )
37+ return dag_run_state == "success"
38+
39+
2840def metrics_v3 (role_group : str ) -> None :
2941 now = datetime .now (timezone .utc )
3042 ts = now .strftime ("%Y-%m-%dT%H:%M:%S.%f" ) + now .strftime ("%z" )
@@ -66,10 +78,14 @@ def metrics_v3(role_group: str) -> None:
6678 response = requests .patch (
6779 f"{ rest_url } /dags/{ dag_id } " , headers = headers , json = {"is_paused" : False }
6880 )
81+
6982 # trigger DAG
7083 response = requests .post (
7184 f"{ rest_url } /dags/{ dag_id } /dagRuns" , headers = headers , json = dag_data
7285 )
86+ dag_run_id = response .json ()["dag_run_id" ]
87+
88+ print (f"DAG RUN ID: { dag_run_id } " )
7389
7490 # Test the DAG in a loop. Each time we call the script a new job will be started: we can avoid
7591 # or minimize this by looping over the check instead.
@@ -79,24 +95,13 @@ def metrics_v3(role_group: str) -> None:
7995 assert response .status_code == 200 , "DAG run could not be triggered."
8096 # Wait for the metrics to be consumed by the statsd-exporter
8197 time .sleep (5 )
82- # (disable line-break flake checks)
98+ heartbeat_metric = "airflow_scheduler_heartbeat"
99+ dag_run_success_count_metric = f"airflow_dagrun_duration_success_{ dag_id } _count"
83100 if (
84- (assert_metric ("scheduler" , role_group , "airflow_scheduler_heartbeat" ))
85- and (
86- assert_metric (
87- "webserver" ,
88- role_group ,
89- "airflow_task_instance_created_BashOperator" ,
90- )
91- ) # noqa: W503, W504
92- and (
93- assert_metric (
94- "scheduler" ,
95- role_group ,
96- "airflow_dagrun_duration_success_example_trigger_target_dag_count" ,
97- )
98- )
99- ): # noqa: W503, W504
101+ assert_completion (rest_url , headers , dag_id , dag_run_id )
102+ and assert_metric ("scheduler" , role_group , heartbeat_metric )
103+ and assert_metric ("scheduler" , role_group , dag_run_success_count_metric )
104+ ):
100105 break
101106 time .sleep (10 )
102107 loop += 1
0 commit comments