@@ -54,8 +54,8 @@ def check_status(query):
5454 return None
5555
5656def polling_loop (interval , config ):
57+ global metrics_data
5758 while True :
58- global metrics_data
5959 metrics_data = []
6060 for hosts in config :
6161 service_name = hosts ["job-id" ]
@@ -83,20 +83,22 @@ def process_up_query(query, service_name):
8383 return
8484
8585 for metric in result :
86- job_name = metric . get ( ' metric' ,{}) .get ('job' , "" ) #for later use in dataclass
87- time_stamp = metric .get ('value' , [])[ 0 ] #for later use in dataclass
86+ instance = metric [ " metric" ] .get ("instance" , "unknown" )
87+ job_name = metric [ "metric" ] .get ("job" , "unknown" ) #for later use in dataclass
8888 value = metric .get ('value' , [])[1 ]
8989 # last_active = datetime.now(pacific_tz).strftime("%Y-%m-%d %H:%M:%S %Z")
9090 status = "Healthy" if float (value ) > 0 else "Unhealthy"
9191 if status == "Unhealthy" :
9292 current = get_first_match_time (prom = prom , prom_query = "up" , match_value = 0 , hours = up_hours )
9393 metrics_data .append ({
94- "instance" : service_name ,
94+ "instance" : instance ,
95+ "job" :job_name ,
9596 "status" : current
9697 })
9798 else :
9899 metrics_data .append ({
99- "instance" : service_name ,
100+ "instance" : instance ,
101+ "job" : job_name ,
100102 "status" : "Healthy"
101103 })
102104 except Exception as e :
@@ -112,11 +114,13 @@ def process_time_query(query, service_name):
112114 try :
113115 result = prom .custom_query (query = query )
114116 if result and len (result ) > 0 :
115- first_result = result [0 ]
116- uptime_seconds = float (first_result ["value" ][1 ])
117- up_hours = int (uptime_seconds / 3600 )
118- if up_hours == 0 :
119- up_hours = 1
117+ for metric in result :
118+ instance = metric ["metric" ].get ("instance" , "unknown" )
119+ job_name = metric ["metric" ].get ("job" , "unknown" )
120+ uptime_seconds = float (metric ["value" ][1 ])
121+ up_hours = int (uptime_seconds / 3600 )
122+ if up_hours == 0 :
123+ up_hours = 1
120124 except Exception as e :
121125 print (f"Error processing time query '{ query } ': { e } " )
122126
0 commit comments