def test_gauge(self): """Test that we can track gauges in Service303""" # Add a gauge with a label to the regisry c = Gauge('process_max_fds', 'A gauge', ['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 gauge1 = metrics_pb2.Gauge(value=1.23) gauge2 = metrics_pb2.Gauge(value=2.34) metric1 = metrics_pb2.Metric(gauge=gauge1, timestamp_ms=1234000) metric2 = metrics_pb2.Metric(gauge=gauge2, timestamp_ms=1234000) family = metrics_pb2.MetricFamily( name=str(metricsd_pb2.process_max_fds), type=metrics_pb2.GAUGE) 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)
def _gauge_to_proto( metric: prometheus_client.core.Metric, ) -> metrics_pb2.MetricFamily: ret = metrics_pb2.MetricFamily(name=metric.name, type=metrics_pb2.GAUGE) for sample in metric.samples: (_, labels, value, *_) = sample met = metrics_pb2.Metric(gauge=metrics_pb2.Gauge(value=value)) for key in labels: met.label.add(name=key, value=labels[key]) ret.metric.extend([met]) return ret