@@ -561,3 +561,141 @@ def get_metric_aggregation(
561561 else :
562562 raise TypeError ("Invalid operation: " + operation )
563563 return aggregated_values
564+
565+
566+ def get_scrape_pools (self ) -> list [str ]:
567+ """
568+ Get a list of all scrape pools in activeTargets.
569+ """
570+ scrape_pools = []
571+ for target in self .get_targets ()['activeTargets' ]:
572+ scrape_pools .append (target ['scrapePool' ])
573+ return list (set (scrape_pools ))
574+
575+ def get_targets (self , state : str = None , scrape_pool : str = None ):
576+ """
577+ Get a list of all targets from Prometheus.
578+
579+ :param state: (str) Optional filter for target state ('active', 'dropped', 'any').
580+ If None, returns both active and dropped targets.
581+ :param scrape_pool: (str) Optional filter by scrape pool name
582+ :returns: (dict) A dictionary containing active and dropped targets
583+ :raises:
584+ (RequestException) Raises an exception in case of a connection error
585+ (PrometheusApiClientException) Raises in case of non 200 response status code
586+ """
587+ params = {}
588+ if state :
589+ params ['state' ] = state
590+ if scrape_pool :
591+ params ['scrapePool' ] = scrape_pool
592+
593+ response = self ._session .get (
594+ "{0}/api/v1/targets" .format (self .url ),
595+ verify = self ._session .verify ,
596+ headers = self .headers ,
597+ params = params ,
598+ auth = self .auth ,
599+ cert = self ._session .cert ,
600+ timeout = self ._timeout ,
601+ )
602+
603+ if response .status_code == 200 :
604+ return response .json ()["data" ]
605+ else :
606+ raise PrometheusApiClientException (
607+ "HTTP Status Code {} ({!r})" .format (
608+ response .status_code , response .content )
609+ )
610+
611+ def get_target_metadata (self , target : dict [str , str ], metric : str = None ):
612+ """
613+ Get metadata about metrics from a specific target.
614+
615+ :param target: (dict) A dictionary containing target labels to match against (e.g. {'job': 'prometheus'})
616+ :param metric: (str) Optional metric name to filter metadata
617+ :returns: (list) A list of metadata entries for matching targets
618+ :raises:
619+ (RequestException) Raises an exception in case of a connection error
620+ (PrometheusApiClientException) Raises in case of non 200 response status code
621+ """
622+ params = {}
623+
624+ # Convert target dict to label selector string
625+ if metric :
626+ params ['metric' ] = metric
627+
628+ if target :
629+ match_target = "{" + \
630+ "," .join (f'{ k } ="{ v } "' for k , v in target .items ()) + "}"
631+ params ['match_target' ] = match_target
632+
633+ response = self ._session .get (
634+ "{0}/api/v1/targets/metadata" .format (self .url ),
635+ verify = self ._session .verify ,
636+ headers = self .headers ,
637+ params = params ,
638+ auth = self .auth ,
639+ cert = self ._session .cert ,
640+ timeout = self ._timeout ,
641+ )
642+
643+ if response .status_code == 200 :
644+ return response .json ()["data" ]
645+ else :
646+ raise PrometheusApiClientException (
647+ "HTTP Status Code {} ({!r})" .format (
648+ response .status_code , response .content )
649+ )
650+
651+ def get_metric_metadata (self , metric : str , limit : int = None , limit_per_metric : int = None ):
652+ """
653+ Get metadata about metrics.
654+
655+ :param metric: (str) Optional metric name to filter metadata
656+ :param limit: (int) Optional maximum number of metrics to return
657+ :param limit_per_metric: (int) Optional maximum number of metadata entries per metric
658+ :returns: (dict) A dictionary mapping metric names to lists of metadata entries in format:
659+ {'metric_name': [{'type': str, 'help': str, 'unit': str}, ...]}
660+ :raises:
661+ (RequestException) Raises an exception in case of a connection error
662+ (PrometheusApiClientException) Raises in case of non 200 response status code
663+ """
664+ params = {}
665+
666+ if metric :
667+ params ['metric' ] = metric
668+
669+ if limit :
670+ params ['limit' ] = limit
671+
672+ if limit_per_metric :
673+ params ['limit_per_metric' ] = limit_per_metric
674+
675+ response = self ._session .get (
676+ "{0}/api/v1/metadata" .format (self .url ),
677+ verify = self ._session .verify ,
678+ headers = self .headers ,
679+ params = params ,
680+ auth = self .auth ,
681+ cert = self ._session .cert ,
682+ timeout = self ._timeout ,
683+ )
684+
685+ if response .status_code == 200 :
686+ data = response .json ()["data" ]
687+ formatted_data = []
688+ for k , v in data .items ():
689+ for v_ in v :
690+ formatted_data .append ({
691+ "metric_name" : k ,
692+ "type" : v_ .get ('type' , 'unknown' ),
693+ "help" : v_ .get ('help' , '' ),
694+ "unit" : v_ .get ('unit' , '' )
695+ })
696+ return formatted_data
697+ else :
698+ raise PrometheusApiClientException (
699+ "HTTP Status Code {} ({!r})" .format (
700+ response .status_code , response .content )
701+ )
0 commit comments