1010from requests .packages .urllib3 .util .retry import Retry
1111from requests import Session
1212
13+ from .metric_query import MetricLabelQuery , query_to_str
1314from .exceptions import PrometheusApiClientException
1415
1516# set up logging
@@ -229,14 +230,14 @@ def get_label_values(self, label_name: str, params: dict = None):
229230 return labels
230231
231232 def get_current_metric_value (
232- self , metric_name : str , label_config : dict = None , params : dict = None
233+ self , metric_name : str , label_config : MetricLabelQuery = None , params : dict = None
233234 ):
234235 r"""
235236 Get the current metric value for the specified metric and label configuration.
236237
237238 :param metric_name: (str) The name of the metric
238- :param label_config: (dict ) A dictionary that specifies metric labels and their
239- values
239+ :param label_config: (MetricLabelQuery ) A dictionary specifying metric labels and their
240+ values, with optional operator (default is equality).
240241 :param params: (dict) Optional dictionary containing GET parameters to be sent
241242 along with the API request, such as "time"
242243 :returns: (list) A list of current metric values for the specified metric
@@ -249,17 +250,13 @@ def get_current_metric_value(
249250
250251 prom = PrometheusConnect()
251252
252- my_label_config = {'cluster': 'my_cluster_id', 'label_2': 'label_2_value' }
253+ my_label_config = {'cluster': 'my_cluster_id', 'label_2': ('=~','label_2_.*') }
253254
254255 prom.get_current_metric_value(metric_name='up', label_config=my_label_config)
255256 """
256257 params = params or {}
257258 data = []
258- if label_config :
259- label_list = [str (key + "=" + "'" + label_config [key ] + "'" ) for key in label_config ]
260- query = metric_name + "{" + "," .join (label_list ) + "}"
261- else :
262- query = metric_name
259+ query = query_to_str (metric_name , label_query = label_config )
263260
264261 # using the query API to get raw data
265262 response = self ._session .request (
@@ -284,7 +281,7 @@ def get_current_metric_value(
284281 def get_metric_range_data (
285282 self ,
286283 metric_name : str ,
287- label_config : dict = None ,
284+ label_config : MetricLabelQuery = None ,
288285 start_time : datetime = (datetime .now () - timedelta (minutes = 10 )),
289286 end_time : datetime = datetime .now (),
290287 chunk_size : timedelta = None ,
@@ -295,8 +292,8 @@ def get_metric_range_data(
295292 Get the current metric value for the specified metric and label configuration.
296293
297294 :param metric_name: (str) The name of the metric.
298- :param label_config: (dict ) A dictionary specifying metric labels and their
299- values.
295+ :param label_config: (MetricLabelQuery ) A dictionary specifying metric labels and their
296+ values, with optional operator (default is equality) .
300297 :param start_time: (datetime) A datetime object that specifies the metric range start time.
301298 :param end_time: (datetime) A datetime object that specifies the metric range end time.
302299 :param chunk_size: (timedelta) Duration of metric data downloaded in one request. For
@@ -338,11 +335,7 @@ def get_metric_range_data(
338335 raise ValueError ("specified chunk_size is too big" )
339336 chunk_seconds = round (chunk_size .total_seconds ())
340337
341- if label_config :
342- label_list = [str (key + "=" + "'" + label_config [key ] + "'" ) for key in label_config ]
343- query = metric_name + "{" + "," .join (label_list ) + "}"
344- else :
345- query = metric_name
338+ query = query_to_str (metric_name , label_query = label_config )
346339 _LOGGER .debug ("Prometheus Query: %s" , query )
347340
348341 while start < end :
0 commit comments