(self, metric_name, metric, scraper_config, hostname=None)
Extracts metrics from a prometheus summary metric and sends them as gauges
Extracts metrics from a prometheus summary metric and sends them as gauges
Extracts metrics from a prometheus summary metric and sends them as gauges
def _submit_gauges_from_summary(self, metric_name, metric, scraper_config, hostname=None):
        """
        Extracts metrics from a prometheus summary metric and sends them as gauges
        """
        for sample in metric.samples:
            val = sample[self.SAMPLE_VALUE]
            if not self._is_value_valid(val):
                self.log.debug("Metric value is not supported for metric %s", sample[self.SAMPLE_NAME])
                continue
            if self._ignore_metrics_by_label(scraper_config, metric_name, sample):
                continue
            custom_hostname = self._get_hostname(hostname, sample, scraper_config)
            if sample[self.SAMPLE_NAME].endswith("_sum"):
                tags = self._metric_tags(metric_name, val, sample, scraper_config, hostname=custom_hostname)
                self._submit_distribution_count(
                    scraper_config['send_distribution_sums_as_monotonic'],
                    scraper_config['send_monotonic_with_gauge'],
                    "{}.sum".format(self._metric_name_with_namespace(metric_name, scraper_config)),
                    val,
                    tags=tags,
                    hostname=custom_hostname,
                    flush_first_value=scraper_config['_flush_first_value'],
                )
            elif sample[self.SAMPLE_NAME].endswith("_count"):
                tags = self._metric_tags(metric_name, val, sample, scraper_config, hostname=custom_hostname)
                self._submit_distribution_count(
                    scraper_config['send_distribution_counts_as_monotonic'],
                    scraper_config['send_monotonic_with_gauge'],
                    "{}.count".format(self._metric_name_with_namespace(metric_name, scraper_config)),
                    val,
                    tags=tags,
                    hostname=custom_hostname,
                    flush_first_value=scraper_config['_flush_first_value'],
                )
            else:
                try:
                    quantile = sample[self.SAMPLE_LABELS]["quantile"]
                except KeyError:
                    # TODO: In the Prometheus spec the 'quantile' label is optional, but it's not clear yet
                    # what we should do in this case. Let's skip for now and submit the rest of metrics.
                    message = (
                        '"quantile" label not present in metric %r. '
                        'Quantile-less summary metrics are not currently supported. Skipping...'
                    )
                    self.log.debug(message, metric_name)
                    continue

                sample[self.SAMPLE_LABELS]["quantile"] = str(float(quantile))
                tags = self._metric_tags(metric_name, val, sample, scraper_config, hostname=custom_hostname)
                self.gauge(
                    "{}.quantile".format(self._metric_name_with_namespace(metric_name, scraper_config)),
                    val,
                    tags=tags,
                    hostname=custom_hostname,
                )
def _submit_gauges_from_summary ( self , metric_name , metric , scraper_config , hostname = None ) : for sample in metric . samples : val = sample [ self . SAMPLE_VALUE ] if not self . _is_value_valid ( val ) : self . log . debug ( "Metric value is not supported for metric %s" , sample [ self . SAMPLE_NAME ] ) continue if self . _ignore_metrics_by_label ( scraper_config , metric_name , sample ) : continue custom_hostname = self . _get_hostname ( hostname , sample , scraper_config ) if sample [ self . SAMPLE_NAME ] . endswith ( "_sum" ) : tags = self . _metric_tags ( metric_name , val , sample , scraper_config , hostname = custom_hostname ) self . _submit_distribution_count ( scraper_config [ 'send_distribution_sums_as_monotonic' ] , scraper_config [ 'send_monotonic_with_gauge' ] , "{}.sum" . format ( self . _metric_name_with_namespace ( metric_name , scraper_config ) ) , val , tags = tags , hostname = custom_hostname , flush_first_value = scraper_config [ '_flush_first_value' ] , ) elif sample [ self . SAMPLE_NAME ] . endswith ( "_count" ) : tags = self . _metric_tags ( metric_name , val , sample , scraper_config , hostname = custom_hostname ) self . _submit_distribution_count ( scraper_config [ 'send_distribution_counts_as_monotonic' ] , scraper_config [ 'send_monotonic_with_gauge' ] , "{}.count" . format ( self . _metric_name_with_namespace ( metric_name , scraper_config ) ) , val , tags = tags , hostname = custom_hostname , flush_first_value = scraper_config [ '_flush_first_value' ] , ) else : try : quantile = sample [ self . SAMPLE_LABELS ] [ "quantile" ] except KeyError : # TODO: In the Prometheus spec the 'quantile' label is optional, but it's not clear yet # what we should do in this case. Let's skip for now and submit the rest of metrics. message = ( '"quantile" label not present in metric %r. ' 'Quantile-less summary metrics are not currently supported. Skipping...' ) self . log . debug ( message , metric_name ) continue sample [ self . SAMPLE_LABELS ] [ "quantile" ] = str ( float ( quantile ) ) tags = self . _metric_tags ( metric_name , val , sample , scraper_config , hostname = custom_hostname ) self . gauge ( "{}.quantile" . format ( self . _metric_name_with_namespace ( metric_name , scraper_config ) ) , val , tags = tags , hostname = custom_hostname , )