Beispiel #1
0
    def test_counter(self):
        """Test that we can track counters in Service303"""
        # Add a counter with a label to the regisry
        c = Counter('process_max_fds', 'A counter', ['result'],
                    registry=self.registry)

        # Create two series for value1 and value2
        c.labels('success').inc(1.23)
        c.labels('failure').inc(2.34)

        # Build proto outputs
        counter1 = metrics_pb2.Counter(value=1.23)
        counter2 = metrics_pb2.Counter(value=2.34)
        metric1 = metrics_pb2.Metric(counter=counter1,
                                                timestamp_ms=1234000)
        metric2 = metrics_pb2.Metric(counter=counter2,
                                                timestamp_ms=1234000)
        family = metrics_pb2.MetricFamily(
            name=str(metricsd_pb2.process_max_fds),
            type=metrics_pb2.COUNTER)
        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)
Beispiel #2
0
def _counter_to_proto(
    metric: prometheus_client.core.Metric, ) -> metrics_pb2.MetricFamily:
    ret = metrics_pb2.MetricFamily(name=metric.name, type=metrics_pb2.COUNTER)
    for sample in metric.samples:
        counter = metrics_pb2.Counter(value=sample[2])
        met = metrics_pb2.Metric(counter=counter)
        for key in sample[1]:
            met.label.add(name=key, value=sample[1][key])
        ret.metric.extend([met])
    return ret