Exemplo n.º 1
0
    def test_consume_measurement_counter(self, mock_sync_measurement_consumer):
        sync_consumer_instance = mock_sync_measurement_consumer()
        meter_provider = MeterProvider()
        counter = meter_provider.get_meter("name").create_counter("name")

        counter.add(1)

        sync_consumer_instance.consume_measurement.assert_called()
Exemplo n.º 2
0
    def test_register_asynchronous_instrument(self,
                                              mock_sync_measurement_consumer):

        meter_provider = MeterProvider()

        meter_provider._measurement_consumer.register_asynchronous_instrument.assert_called_with(
            meter_provider.get_meter("name").create_observable_counter(
                "name", Mock()))
        meter_provider._measurement_consumer.register_asynchronous_instrument.assert_called_with(
            meter_provider.get_meter("name").create_observable_up_down_counter(
                "name", Mock()))
        meter_provider._measurement_consumer.register_asynchronous_instrument.assert_called_with(
            meter_provider.get_meter("name").create_observable_gauge(
                "name", Mock()))
Exemplo n.º 3
0
    def test_meter_provider_resource(self):
        """
        `MeterProvider` provides a way to allow a `Resource` to be specified.
        """

        meter_provider_0 = MeterProvider()
        meter_provider_1 = MeterProvider()

        self.assertIs(meter_provider_0._resource, meter_provider_1._resource)
        self.assertIsInstance(meter_provider_0._resource, Resource)
        self.assertIsInstance(meter_provider_1._resource, Resource)

        resource = Resource({"key": "value"})
        self.assertIs(MeterProvider(resource)._resource, resource)
Exemplo n.º 4
0
    def test_register_metric_reader(self):
        """ "
        `MeterProvider` provides a way to configure `SDKMetricReader`s.
        """

        meter_provider = MeterProvider()

        self.assertTrue(hasattr(meter_provider, "register_metric_reader"))

        metric_reader = SDKMetricReader()

        meter_provider.register_metric_reader(metric_reader)

        self.assertTrue(meter_provider._metric_readers, [metric_reader])
Exemplo n.º 5
0
    def test_register_metric_exporter(self):
        """ "
        `MeterProvider` provides a way to configure `ConsoleMetricExporter`s.
        """

        meter_provider = MeterProvider()

        self.assertTrue(hasattr(meter_provider, "register_metric_exporter"))

        metric_exporter = ConsoleMetricExporter()

        meter_provider.register_metric_exporter(metric_exporter)

        self.assertTrue(meter_provider._metric_exporters, [metric_exporter])
Exemplo n.º 6
0
    def test_register_view(self):
        """ "
        `MeterProvider` provides a way to configure `View`s.
        """

        meter_provider = MeterProvider()

        self.assertTrue(hasattr(meter_provider, "register_view"))

        view = View()

        meter_provider.register_view(view)

        self.assertTrue(meter_provider._views, [view])
Exemplo n.º 7
0
 def test_shutdown_race(self, mock_logger):
     mock_logger.warning = MockFunc()
     meter_provider = MeterProvider()
     num_threads = 70
     self.run_with_many_threads(meter_provider.shutdown,
                                num_threads=num_threads)
     self.assertEqual(mock_logger.warning.call_count, num_threads - 1)
Exemplo n.º 8
0
    def test_measurement_collect_callback(self,
                                          mock_sync_measurement_consumer):
        metric_readers = [DummyMetricReader()] * 5
        sync_consumer_instance = mock_sync_measurement_consumer()
        sync_consumer_instance.collect = MockFunc()
        MeterProvider(metric_readers=metric_readers)

        for reader in metric_readers:
            reader.collect()
        self.assertEqual(sync_consumer_instance.collect.call_count,
                         len(metric_readers))
Exemplo n.º 9
0
    def test_get_meter_empty(self):
        """
        `MeterProvider.get_meter` called with None or empty string as name
        should return a NoOpMeter.
        """

        meter = MeterProvider().get_meter(
            None,
            version="version",
            schema_url="schema_url",
        )
        self.assertIsInstance(meter, NoOpMeter)
        self.assertEqual(meter._name, None)

        meter = MeterProvider().get_meter(
            "",
            version="version",
            schema_url="schema_url",
        )
        self.assertIsInstance(meter, NoOpMeter)
        self.assertEqual(meter._name, "")
Exemplo n.º 10
0
    def test_shutdown(self):

        mock_metric_reader_0 = MagicMock(
            **{
                "shutdown.return_value": False,
                "__str__.return_value": "mock_metric_reader_0",
            })
        mock_metric_reader_1 = Mock(**{"shutdown.return_value": True})

        meter_provider = MeterProvider(
            metric_readers=[mock_metric_reader_0, mock_metric_reader_1])

        with self.assertLogs(level=WARNING) as log:
            self.assertFalse(meter_provider.shutdown())
            self.assertEqual(
                log.records[0].getMessage(),
                "MetricReader mock_metric_reader_0 failed to shutdown",
            )
        mock_metric_reader_0.shutdown.assert_called_once()
        mock_metric_reader_1.shutdown.assert_called_once()

        mock_metric_reader_0 = Mock(**{"shutdown.return_value": True})
        mock_metric_reader_1 = Mock(**{"shutdown.return_value": True})

        meter_provider = MeterProvider(
            metric_readers=[mock_metric_reader_0, mock_metric_reader_1])

        self.assertTrue(meter_provider.shutdown())
        mock_metric_reader_0.shutdown.assert_called_once()
        mock_metric_reader_1.shutdown.assert_called_once()
Exemplo n.º 11
0
 def test_get_meter_duplicate(self):
     """
     Subsequent calls to `MeterProvider.get_meter` with the same arguments
     should return the same `Meter` instance.
     """
     mp = MeterProvider()
     meter1 = mp.get_meter(
         "name",
         version="version",
         schema_url="schema_url",
     )
     meter2 = mp.get_meter(
         "name",
         version="version",
         schema_url="schema_url",
     )
     meter3 = mp.get_meter(
         "name2",
         version="version",
         schema_url="schema_url",
     )
     self.assertIs(meter1, meter2)
     self.assertIsNot(meter1, meter3)
Exemplo n.º 12
0
    def test_get_meter(self):
        """
        `MeterProvider.get_meter` arguments are used to create an
        `InstrumentationInfo` object on the created `Meter`.
        """

        meter = MeterProvider().get_meter(
            "name",
            version="version",
            schema_url="schema_url",
        )

        self.assertEqual(meter._instrumentation_info.name, "name")
        self.assertEqual(meter._instrumentation_info.version, "version")
        self.assertEqual(meter._instrumentation_info.schema_url, "schema_url")
Exemplo n.º 13
0
    def test_shutdown_subsequent_calls(self):
        """
        No subsequent attempts to get a `Meter` are allowed after calling
        `MeterProvider.shutdown`
        """

        meter_provider = MeterProvider()

        with self.assertRaises(AssertionError):
            with self.assertLogs(level=WARNING):
                meter_provider.shutdown()

        with self.assertLogs(level=WARNING):
            meter_provider.shutdown()
Exemplo n.º 14
0
    def test_meter_configuration(self):
        """
        Any updated configuration is applied to all returned `Meter`s.
        """

        meter_provider = MeterProvider()

        view_0 = View()

        meter_provider.register_view(view_0)

        meter_0 = meter_provider.get_meter("meter_0")
        meter_1 = meter_provider.get_meter("meter_1")

        self.assertEqual(meter_0._meter_provider._views, [view_0])
        self.assertEqual(meter_1._meter_provider._views, [view_0])

        view_1 = View()

        meter_provider.register_view(view_1)

        self.assertEqual(meter_0._meter_provider._views, [view_0, view_1])
        self.assertEqual(meter_1._meter_provider._views, [view_0, view_1])
Exemplo n.º 15
0
    def test_cpu_time_generator(self):
        def cpu_time_generator(
        ) -> Generator[Iterable[Measurement], None, None]:
            while True:
                measurements = []
                procstat = io.StringIO(self.procstat_str)
                procstat.readline()  # skip the first line
                for line in procstat:
                    if not line.startswith("cpu"):
                        break
                    cpu, *states = line.split()
                    measurements.append(
                        Measurement(
                            int(states[0]) // 100,
                            {
                                "cpu": cpu,
                                "state": "user"
                            },
                        ))
                    measurements.append(
                        Measurement(
                            int(states[1]) // 100,
                            {
                                "cpu": cpu,
                                "state": "nice"
                            },
                        ))
                    measurements.append(
                        Measurement(
                            int(states[2]) // 100,
                            {
                                "cpu": cpu,
                                "state": "system"
                            },
                        ))
                    measurements.append(
                        Measurement(
                            int(states[3]) // 100,
                            {
                                "cpu": cpu,
                                "state": "idle"
                            },
                        ))
                    measurements.append(
                        Measurement(
                            int(states[4]) // 100,
                            {
                                "cpu": cpu,
                                "state": "iowait"
                            },
                        ))
                    measurements.append(
                        Measurement(
                            int(states[5]) // 100, {
                                "cpu": cpu,
                                "state": "irq"
                            }))
                    measurements.append(
                        Measurement(
                            int(states[6]) // 100,
                            {
                                "cpu": cpu,
                                "state": "softirq"
                            },
                        ))
                    measurements.append(
                        Measurement(
                            int(states[7]) // 100,
                            {
                                "cpu": cpu,
                                "state": "guest"
                            },
                        ))
                    measurements.append(
                        Measurement(
                            int(states[8]) // 100,
                            {
                                "cpu": cpu,
                                "state": "guest_nice"
                            },
                        ))
                yield measurements

        meter = MeterProvider().get_meter("name")
        observable_counter = meter.create_observable_counter(
            "system.cpu.time",
            callback=cpu_time_generator(),
            unit="s",
            description="CPU time",
        )
        measurements = list(observable_counter._callback())
        self.assertEqual(measurements, self.measurements_expected)
Exemplo n.º 16
0
 def test_creates_sync_measurement_consumer(self,
                                            mock_sync_measurement_consumer):
     MeterProvider()
     mock_sync_measurement_consumer.assert_called()
Exemplo n.º 17
0
    def test_force_flush_result(self):
        """
        `MeterProvider.force_flush` provides a way to let the caller know if it
        succeeded or failed.

        `MeterProvider.force_flush` is implemented by at least invoking
        ``force_flush`` on all registered `SDKMetricReader`s and `ConsoleMetricExporter`s.
        """

        meter_provider = MeterProvider()

        meter_provider.register_metric_reader(
            Mock(**{"force_flush.return_value": True}))
        meter_provider.register_metric_exporter(
            Mock(**{"force_flush.return_value": True}))

        self.assertTrue(meter_provider.force_flush())

        meter_provider = MeterProvider()

        meter_provider.register_metric_reader(
            Mock(**{"force_flush.return_value": True}))
        meter_provider.register_metric_exporter(
            Mock(**{"force_flush.return_value": False}))

        self.assertFalse(meter_provider.force_flush())
Exemplo n.º 18
0
from opentelemetry._metrics import get_meter_provider, set_meter_provider
from opentelemetry.exporter.otlp.proto.grpc._metric_exporter import (
    OTLPMetricExporter,
)
from opentelemetry.sdk._metrics import MeterProvider
from opentelemetry.sdk._metrics.export import PeriodicExportingMetricReader

exporter = OTLPMetricExporter(insecure=True)
reader = PeriodicExportingMetricReader(exporter)
provider = MeterProvider(metric_readers=[reader])
set_meter_provider(provider)

meter = get_meter_provider().get_meter("getting-started")
counter = meter.create_counter("first_counter")
counter.add(1)
# TODO: fill in details for additional metrics