Exemplo n.º 1
0
    def test_histogram(self):
        """Test that we can track histogram in Service303"""
        # Add a histogram with a label to the regisry
        c = Histogram('process_max_fds',
                      'A summary', ['result'],
                      registry=self.registry,
                      buckets=[0, 2, float('inf')])
        c.labels('success').observe(1.23)
        c.labels('failure').observe(2.34)

        # Build proto outputs
        histogram1 = metrics_pb2.Histogram(sample_count=1, sample_sum=1.23)
        histogram1.bucket.add(upper_bound=0, cumulative_count=0)
        histogram1.bucket.add(upper_bound=2, cumulative_count=1)
        histogram1.bucket.add(upper_bound=float('inf'), cumulative_count=1)
        histogram2 = metrics_pb2.Histogram(sample_count=1, sample_sum=2.34)
        histogram2.bucket.add(upper_bound=0, cumulative_count=0)
        histogram2.bucket.add(upper_bound=2, cumulative_count=0)
        histogram2.bucket.add(upper_bound=float('inf'), cumulative_count=1)
        metric1 = metrics_pb2.Metric(histogram=histogram1,
                                     timestamp_ms=1234000)
        metric2 = metrics_pb2.Metric(histogram=histogram2,
                                     timestamp_ms=1234000)
        family = metrics_pb2.MetricFamily(name=str(
            metricsd_pb2.process_max_fds),
                                          type=metrics_pb2.HISTOGRAM)
        metric1.label.add(name=str(metricsd_pb2.result), value='success')
        metric2.label.add(name=str(metricsd_pb2.result), value='failure')
        family.metric.extend([metric1, metric2])

        with unittest.mock.patch('time.time') as mock_time:
            mock_time.side_effect = lambda: 1234
            self.assertCountEqual(
                list(metrics_export.get_metrics(self.registry))[0].metric,
                family.metric)
Exemplo n.º 2
0
def _histogram_to_proto(
        metric: prometheus_client.core.Metric,
) -> [metrics_pb2.MetricFamily]:
    """
    1. Get metrics by unique labelset ignoring quantile
    2. convert to proto separately for each one
    """

    family_by_labelset = {}

    for sample in metric.samples:
        (name, labels, value, *_) = sample
        # get real family by checking labels (ignoring le)
        distinct_labels = frozenset(_remove_label(labels, 'le').items())
        if distinct_labels not in family_by_labelset:
            fam = metrics_pb2.MetricFamily(
                name=metric.name,
                type=metrics_pb2.HISTOGRAM,
            )
            hist = metrics_pb2.Histogram(sample_count=0, sample_sum=0)
            fam.metric.extend([metrics_pb2.Metric(histogram=hist)])
            family_by_labelset[distinct_labels] = fam

        unique_family = family_by_labelset[distinct_labels]

        if str.endswith(name, "_sum"):
            unique_family.metric[0].histogram.sample_sum = value
        elif str.endswith(name, "_count"):
            unique_family.metric[0].histogram.sample_count = int(value)
        elif 'le' in labels:
            unique_family.metric[0].histogram.bucket.extend([
                metrics_pb2.Bucket(
                    upper_bound=float(labels['le']),
                    cumulative_count=value,
                ),
            ])

    # Add non-quantile labels to all metrics
    for labelset in family_by_labelset.keys():
        for label in labelset:
            family_by_labelset[labelset].metric[0].label.add(
                name=str(label[0]), value=str(label[1]),
            )

    return list(family_by_labelset.values())