def _test_metrics(self, observer_name, expected):
        meter = self.meter_provider.get_meter(__name__)

        with mock.patch("opentelemetry.metrics.get_meter") as mock_get_meter:
            mock_get_meter.return_value = meter
            system_metrics = SystemMetrics(self.memory_metrics_exporter)
            self._assert_metrics(observer_name, system_metrics, expected)
    def test_system_metrics_constructor(self):
        # ensure the observers have been registered
        meter = metrics.get_meter(__name__)
        with mock.patch("opentelemetry.metrics.get_meter") as mock_get_meter:
            mock_get_meter.return_value = meter
            SystemMetrics(self.memory_metrics_exporter)

        self.assertEqual(len(meter.observers), 18)

        observer_names = [
            "system.cpu.time",
            "system.cpu.utilization",
            "system.memory.usage",
            "system.memory.utilization",
            "system.swap.usage",
            "system.swap.utilization",
            "system.disk.io",
            "system.disk.operations",
            "system.disk.time",
            "system.disk.merged",
            "system.network.dropped_packets",
            "system.network.packets",
            "system.network.errors",
            "system.network.io",
            "system.network.connections",
            "runtime.{}.memory".format(self.implementation),
            "runtime.{}.cpu_time".format(self.implementation),
            "runtime.{}.gc_count".format(self.implementation),
        ]

        for observer in meter.observers:
            self.assertIn(observer.name, observer_names)
            observer_names.remove(observer.name)
 def test_system_metrics_constructor(self):
     # ensure the observers have been registered
     meter = metrics.get_meter(__name__)
     with mock.patch("opentelemetry.metrics.get_meter") as mock_get_meter:
         mock_get_meter.return_value = meter
         SystemMetrics(self.memory_metrics_exporter)
     self.assertEqual(len(meter.observers), 6)
     observer_names = [
         "system.mem",
         "system.cpu",
         "system.net.bytes",
         "runtime.python.mem",
         "runtime.python.cpu",
         "runtime.python.gc.count",
     ]
     for observer in meter.observers:
         self.assertIn(observer.name, observer_names)
         observer_names.remove(observer.name)
    def test_runtime_memory(self):
        meter = self.meter_provider.get_meter(__name__)
        with mock.patch("opentelemetry.metrics.get_meter") as mock_get_meter:
            mock_get_meter.return_value = meter
            system_metrics = SystemMetrics(self.memory_metrics_exporter)

            with mock.patch.object(
                    system_metrics._proc,  # pylint: disable=protected-access
                    "memory_info",
            ) as mock_runtime_memory:
                RuntimeMemory = namedtuple(
                    "RuntimeMemory",
                    ["rss", "vms", "pfaults", "pageins"],
                )
                mock_runtime_memory.return_value = RuntimeMemory(
                    rss=9777152, vms=4385665024, pfaults=2631, pageins=49)
                expected = {
                    (("type", "rss"), ): 9777152,
                    (("type", "vms"), ): 4385665024,
                }
                self._assert_metrics("runtime.python.mem", system_metrics,
                                     expected)
    def test_runtime_cpu(self):
        meter = self.meter_provider.get_meter(__name__)
        with mock.patch("opentelemetry.metrics.get_meter") as mock_get_meter:
            mock_get_meter.return_value = meter
            system_metrics = SystemMetrics(self.memory_metrics_exporter)

            with mock.patch.object(
                    system_metrics._proc,  # pylint: disable=protected-access
                    "cpu_times",
            ) as mock_runtime_cpu_times:
                RuntimeCPU = namedtuple("RuntimeCPU",
                                        ["user", "nice", "system"])
                mock_runtime_cpu_times.return_value = RuntimeCPU(user=100.48,
                                                                 nice=0.0,
                                                                 system=200.43)

                expected = {
                    (("type", "user"), ): 100.48,
                    (("type", "system"), ): 200.43,
                }

                self._assert_metrics("runtime.python.cpu", system_metrics,
                                     expected)