コード例 #1
0
    def _format_summary(self, summary: MetricTupleType, name: str,
                        const_labels: LabelsType) -> pmp.Metric:
        '''
        :param summary: a 2-tuple containing labels and a dict representing
          the summary value. The dict contains keys for each quantile as
          well as the sum and count fields.
        :param labels: a dict of labels for a metric.
        :param const_labels: a dict of constant labels to be associated with
          the metric.
        '''

        summary_labels, summary_value_dict = summary
        # typing check, no runtime behaviour.
        summary_value_dict = cast(SummaryDictType, summary_value_dict)
        labels = self._unify_labels(summary_labels, const_labels, ordered=True)
        pb_labels = self._create_labels(labels)

        pb_quantiles = []
        for k, v in summary_value_dict.items():
            if not isinstance(k, str):
                pb_quantile = pmp.Quantile(quantile=k, value=v)
                pb_quantiles.append(pb_quantile)

        pb_summary = pmp.Summary(sample_count=summary_value_dict['count'],
                                 sample_sum=summary_value_dict['sum'],
                                 quantile=pb_quantiles)

        pb_metric = pmp.Metric(label=pb_labels, summary=pb_summary)
        if self.timestamp:
            pb_metric.timestamp_ms = self._get_timestamp()

        return pb_metric
コード例 #2
0
    def _format_histogram(self, histogram: MetricTupleType, name: str,
                          const_labels: LabelsType) -> pmp.Metric:
        '''
        :param histogram: a 2-tuple containing labels and a dict representing
          the histogram value. The dict contains keys for each bucket as
          well as the sum and count fields.
        :param labels: a dict of labels for a metric.
        :param const_labels: a dict of constant labels to be associated with
          the metric.
        '''
        histogram_labels, histogram_value_dict = histogram
        # typing check, no runtime behaviour.
        histogram_value_dict = cast(HistogramDictType, histogram_value_dict)
        labels = self._unify_labels(histogram_labels,
                                    const_labels,
                                    ordered=True)
        pb_labels = self._create_labels(labels)

        pb_buckets = []
        for k, v in histogram_value_dict.items():
            if not isinstance(k, str):
                pb_bucket = pmp.Bucket(cumulative_count=v, upper_bound=k)
                pb_buckets.append(pb_bucket)

        pb_histogram = pmp.Histogram(
            sample_count=histogram_value_dict['count'],
            sample_sum=histogram_value_dict['sum'],
            bucket=pb_buckets)

        pb_metric = pmp.Metric(label=pb_labels, histogram=pb_histogram)
        if self.timestamp:
            pb_metric.timestamp_ms = self._get_timestamp()

        return pb_metric
コード例 #3
0
    def _format_gauge(self, gauge: MetricTupleType, name: str,
                      const_labels: LabelsType) -> pmp.Metric:
        '''
        :param gauge: a 2-tuple containing labels and the gauge value.
        :param labels: a dict of labels for a metric.
        :param const_labels: a dict of constant labels to be associated with
          the metric.
        '''
        gauge_labels, gauge_value = gauge
        labels = self._unify_labels(gauge_labels, const_labels, ordered=True)
        pb_labels = self._create_labels(labels)

        pb_gauge = pmp.Gauge(value=gauge_value)

        pb_metric = pmp.Metric(label=pb_labels, gauge=pb_gauge)
        if self.timestamp:
            pb_metric.timestamp_ms = self._get_timestamp()
        return pb_metric
コード例 #4
0
    def _format_counter(self,
                        # counter: Tuple[LabelsType, ValueType],
                        counter: MetricTupleType,
                        name: str,
                        const_labels: LabelsType) -> pmp.Metric:
        '''
        :param counter: a 2-tuple containing labels and the counter value.
        :param labels: a dict of labels for a metric.
        :param const_labels: a dict of constant labels to be associated with
          the metric.
        '''
        counter_labels, counter_value = counter
        labels = self._unify_labels(counter_labels, const_labels, ordered=True)
        pb_labels = self._create_labels(labels)

        pb_counter = pmp.Counter(value=counter_value)

        pb_metric = pmp.Metric(label=pb_labels, counter=pb_counter)
        if self.timestamp:
            pb_metric.timestamp_ms = self._get_timestamp()

        return pb_metric
コード例 #5
0
    def _create_protobuf_object(self,
                                data,
                                metrics,
                                metric_type,
                                const_labels={},
                                ts=False):
        pb_metrics = []
        for i in metrics:
            labels = [pmp.LabelPair(name=k, value=v) for k, v in i[0].items()]
            c_labels = [
                pmp.LabelPair(name=k, value=v)
                for k, v in const_labels.items()
            ]
            labels.extend(c_labels)

            if metric_type == pmp.COUNTER:
                metric = pmp.Metric(counter=pmp.Counter(value=i[1]),
                                    label=labels)
            elif metric_type == pmp.GAUGE:
                metric = pmp.Metric(gauge=pmp.Gauge(value=i[1]), label=labels)
            elif metric_type == pmp.SUMMARY:
                quantiles = []

                for k, v in i[1].items():
                    if not isinstance(k, str):
                        q = pmp.Quantile(quantile=k, value=v)
                        quantiles.append(q)

                metric = pmp.Metric(summary=pmp.Summary(
                    quantile=quantiles,
                    sample_sum=i[1]['sum'],
                    sample_count=i[1]['count']),
                                    label=labels)
            elif metric_type == pmp.HISTOGRAM:
                buckets = []

                for k, v in i[1].items():
                    if not isinstance(k, str):
                        bucket = pmp.Bucket(cumulative_count=v, upper_bound=k)
                        buckets.append(bucket)

                metric = pmp.Metric(summary=pmp.Histogram(
                    buckets=buckets,
                    histogram_sum=i[1]['sum'],
                    histogram_count=i[1]['count']),
                                    label=labels)

            else:
                raise TypeError("Not a valid metric")

            if ts:
                metric.timestamp_ms = int(
                    datetime.datetime.now(
                        tz=datetime.timezone.utc).timestamp() * 1000)

            pb_metrics.append(metric)

        valid_result = pmp.MetricFamily(name=data['name'],
                                        help=data['doc'],
                                        type=metric_type,
                                        metric=pb_metrics)

        return valid_result