def test_counter_to_prometheus(self):
        meter = get_meter_provider().get_meter(__name__)
        metric = meter.create_metric(
            "test@name",
            "testdesc",
            "unit",
            int,
            metrics.Counter,
            ["environment@", "os"],
        )
        labels = {"environment@": "staging", "os": "Windows"}
        key_labels = metrics.get_labels_as_key(labels)
        aggregator = SumAggregator()
        aggregator.update(123)
        aggregator.take_checkpoint()
        record = MetricRecord(metric, key_labels, aggregator)
        collector = CustomCollector("testprefix")
        collector.add_metrics_data([record])

        for prometheus_metric in collector.collect():
            self.assertEqual(type(prometheus_metric), CounterMetricFamily)
            self.assertEqual(prometheus_metric.name, "testprefix_test_name")
            self.assertEqual(prometheus_metric.documentation, "testdesc")
            self.assertTrue(len(prometheus_metric.samples) == 1)
            self.assertEqual(prometheus_metric.samples[0].value, 123)
            self.assertTrue(len(prometheus_metric.samples[0].labels) == 2)
            self.assertEqual(
                prometheus_metric.samples[0].labels["environment_"], "staging"
            )
            self.assertEqual(
                prometheus_metric.samples[0].labels["os"], "Windows"
            )
 def test_invalid_metric(self):
     meter = get_meter_provider().get_meter(__name__)
     metric = meter.create_metric("tesname", "testdesc", "unit", int,
                                  StubMetric)
     labels = {"environment": "staging"}
     key_labels = metrics.get_labels_as_key(labels)
     record = MetricRecord(metric, key_labels, None)
     collector = CustomCollector("testprefix")
     collector.add_metrics_data([record])
     collector.collect()
     self.assertLogs("opentelemetry.ext.prometheus", level="WARNING")
    def setUp(self):
        set_meter_provider(metrics.MeterProvider())
        self._meter = get_meter_provider().get_meter(__name__)
        self._test_metric = self._meter.create_metric(
            "testname",
            "testdesc",
            "unit",
            int,
            metrics.Counter,
            ["environment"],
        )
        labels = {"environment": "staging"}
        self._labels_key = metrics.get_labels_as_key(labels)

        self._mock_registry_register = mock.Mock()
        self._registry_register_patch = mock.patch(
            "prometheus_client.core.REGISTRY.register",
            side_effect=self._mock_registry_register,
        )
Beispiel #4
0
 def setUpClass(cls):
     # pylint: disable=protected-access
     metrics.set_meter_provider(MeterProvider())
     cls._meter = metrics.get_meter(__name__)
     cls._labels = {"environment": "staging"}
     cls._key_labels = get_labels_as_key(cls._labels)