@@ -33,6 +33,20 @@ def test_connection(self):
3333 logger .error (f"Exception occurred while fetching grafana data sources with error: { e } " )
3434 raise e
3535
36+ def check_api_health (self ):
37+ try :
38+ url = '{}/api/health' .format (self .__host )
39+ response = requests .get (url , headers = self .headers , verify = self .__ssl_verify , timeout = 20 )
40+ if response and response .status_code == 200 :
41+ return response .json ()
42+ else :
43+ status_code = response .status_code if response else None
44+ raise Exception (
45+ f"Failed to connect with Grafana. Status Code: { status_code } . Response Text: { response .text } " )
46+ except Exception as e :
47+ logger .error (f"Exception occurred while checking grafana api health with error: { e } " )
48+ raise e
49+
3650 def fetch_data_sources (self ):
3751 try :
3852 url = '{}/api/datasources' .format (self .__host )
@@ -112,6 +126,19 @@ def fetch_alert_rules(self):
112126 logger .error (f"Exception occurred while fetching grafana alert rules with error: { e } " )
113127 raise e
114128
129+ def fetch_dashboard_variable_label_values (self , promql_datasource_uid , label_name ):
130+ try :
131+ url = f'{ self .__host } /api/datasources/proxy/uid/{ promql_datasource_uid } /api/v1/label/{ label_name } /values'
132+ response = requests .get (url , headers = self .headers , verify = self .__ssl_verify )
133+ if response and response .status_code == 200 :
134+ return response .json ().get ('data' , [])
135+ else :
136+ logger .error (f"Failed to fetch label values for { label_name } . Status: { response .status_code } , Body: { response .text } " )
137+ return []
138+ except Exception as e :
139+ logger .error (f"Exception occurred while fetching promql metric labels for { label_name } with error: { e } " )
140+ return []
141+
115142 def panel_query_datasource_api (self , tr : TimeRange , queries , interval_ms = 300000 ):
116143 try :
117144 if not queries or len (queries ) == 0 :
@@ -132,15 +159,17 @@ def panel_query_datasource_api(self, tr: TimeRange, queries, interval_ms=300000)
132159 }
133160
134161 response = requests .post (url , headers = self .headers , json = payload )
162+
135163 if response .status_code == 429 :
136164 logger .info ("Grafana query API responded with 429 (rate limited). Headers: %s" , response .headers )
137165 return None
138- elif response .status_code == 200 :
139- return response .json ()
140- else :
141- logger .error ("Grafana query API error: status code %s, response: %s" , response .status_code ,
142- response .text )
143- return response .json ()
166+
167+ response .raise_for_status ()
168+
169+ return response .json ()
170+ except requests .exceptions .HTTPError as e :
171+ logger .error ("Grafana query API error: status code %s, response: %s" , e .response .status_code , e .response .text )
172+ raise e
144173 except Exception as e :
145174 logger .error ("Exception occurred while querying Grafana datasource: %s" , e )
146175 raise e
0 commit comments