From 32687f8bbc8bb45b05afdee9eafee44cb71ea7f0 Mon Sep 17 00:00:00 2001 From: Alex Boten Date: Mon, 4 Apr 2022 11:03:17 -0700 Subject: [PATCH] fix tests --- .../system_metrics/__init__.py | 85 +-- .../instrumentation/system_metrics/package.py | 16 + .../tests/test_system_metrics.py | 558 ++++++------------ 3 files changed, 245 insertions(+), 414 deletions(-) create mode 100644 instrumentation/opentelemetry-instrumentation-system-metrics/src/opentelemetry/instrumentation/system_metrics/package.py diff --git a/instrumentation/opentelemetry-instrumentation-system-metrics/src/opentelemetry/instrumentation/system_metrics/__init__.py b/instrumentation/opentelemetry-instrumentation-system-metrics/src/opentelemetry/instrumentation/system_metrics/__init__.py index a5ba6c58e4..d41ed6dcce 100644 --- a/instrumentation/opentelemetry-instrumentation-system-metrics/src/opentelemetry/instrumentation/system_metrics/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-system-metrics/src/opentelemetry/instrumentation/system_metrics/__init__.py @@ -28,7 +28,6 @@ "system.disk.io": ["read", "write"], "system.disk.operations": ["read", "write"], "system.disk.time": ["read", "write"], - "system.disk.merged": ["read", "write"], "system.network.dropped.packets": ["transmit", "receive"], "system.network.packets": ["transmit", "receive"], "system.network.errors": ["transmit", "receive"], @@ -64,12 +63,14 @@ "runtime.memory": ["rss", "vms"], "runtime.cpu.time": ["user", "system"], } - SystemMetrics(exporter, config=configuration) + SystemMetrics(config=configuration) API --- """ +from typing import Collection + import gc import os from typing import Dict, List, Iterable, Optional @@ -77,24 +78,22 @@ import psutil -from opentelemetry._metrics import get_meter_provider +from opentelemetry._metrics import get_meter from opentelemetry._metrics.measurement import Measurement +from opentelemetry.instrumentation.instrumentor import BaseInstrumentor +from opentelemetry.instrumentation.system_metrics.version import __version__ +from opentelemetry.instrumentation.system_metrics.package import _instruments from opentelemetry.sdk.util import get_dict_as_key -class SystemMetrics: +class SystemMetrics(BaseInstrumentor): # pylint: disable=too-many-statements def __init__( self, - interval: int = 30, labels: Optional[Dict[str, str]] = None, config: Optional[Dict[str, List[str]]] = None, ): - self._labels = {} if labels is None else labels - self.meter = get_meter_provider().get_meter( - "io.otel.python.instrumentation.system-metrics" - ) # TODO: find a better name here - self._python_implementation = python_implementation().lower() + super().__init__() if config is None: self._config = { "system.cpu.time": ["idle", "user", "system", "irq"], @@ -108,7 +107,6 @@ def __init__( "system.disk.io": ["read", "write"], "system.disk.operations": ["read", "write"], "system.disk.time": ["read", "write"], - "system.disk.merged": ["read", "write"], # "system.filesystem.usage": [], # "system.filesystem.utilization": [], "system.network.dropped.packets": ["transmit", "receive"], @@ -121,6 +119,20 @@ def __init__( } else: self._config = config + self._labels = {} if labels is None else labels + self._meter = None + + def instrumentation_dependencies(self) -> Collection[str]: + return _instruments + + def _instrument(self, **kwargs): + meter_provider = kwargs.get("meter_provider") + self._meter = get_meter( + "io.otel.python.instrumentation.system_metrics", + __version__, + meter_provider, + ) + self._python_implementation = python_implementation().lower() self._proc = psutil.Process(os.getpid()) @@ -153,49 +165,49 @@ def __init__( self._runtime_cpu_time_labels = self._labels.copy() self._runtime_gc_count_labels = self._labels.copy() - self.meter.create_observable_counter( + self._meter.create_observable_counter( callback=self._get_system_cpu_time, name="system.cpu.time", description="System CPU time", unit="seconds", ) - self.meter.create_observable_gauge( + self._meter.create_observable_gauge( callback=self._get_system_cpu_utilization, name="system.cpu.utilization", description="System CPU utilization", unit="1", ) - self.meter.create_observable_gauge( + self._meter.create_observable_gauge( callback=self._get_system_memory_usage, name="system.memory.usage", description="System memory usage", unit="bytes", ) - self.meter.create_observable_gauge( + self._meter.create_observable_gauge( callback=self._get_system_memory_utilization, name="system.memory.utilization", description="System memory utilization", unit="1", ) - self.meter.create_observable_gauge( + self._meter.create_observable_gauge( callback=self._get_system_swap_usage, name="system.swap.usage", description="System swap usage", unit="pages", ) - self.meter.create_observable_gauge( + self._meter.create_observable_gauge( callback=self._get_system_swap_utilization, name="system.swap.utilization", description="System swap utilization", unit="1", ) - # # self.meter.create_observable_counter( + # # self._meter.create_observable_counter( # # callback=self._get_system_swap_page_faults, # # name="system.swap.page_faults", # # description="System swap page faults", @@ -203,42 +215,34 @@ def __init__( # # value_type=int, # # ) - # # self.meter.create_observable_counter( + # # self._meter.create_observable_counter( # # callback=self._get_system_swap_page_operations, # # name="system.swap.page_operations", # # description="System swap page operations", # # unit="operations", # # value_type=int, # # ) - - self.meter.create_observable_counter( + self._meter.create_observable_counter( callback=self._get_system_disk_io, name="system.disk.io", description="System disk IO", unit="bytes", ) - self.meter.create_observable_counter( + self._meter.create_observable_counter( callback=self._get_system_disk_operations, name="system.disk.operations", description="System disk operations", unit="operations", ) - self.meter.create_observable_counter( + self._meter.create_observable_counter( callback=self._get_system_disk_time, name="system.disk.time", description="System disk time", unit="seconds", ) - self.meter.create_observable_counter( - callback=self._get_system_disk_merged, - name="system.disk.merged", - description="System disk merged", - unit="1", - ) - # self.accumulator.register_valueobserver( # callback=self._get_system_filesystem_usage, # name="system.filesystem.usage", @@ -247,7 +251,7 @@ def __init__( # value_type=int, # ) - # self.meter.create_observable_gauge( + # self._meter.create_observable_gauge( # callback=self._get_system_filesystem_utilization, # name="system.filesystem.utilization", # description="System filesystem utilization", @@ -255,42 +259,42 @@ def __init__( # value_type=float, # ) - self.meter.create_observable_counter( + self._meter.create_observable_counter( callback=self._get_system_network_dropped_packets, name="system.network.dropped_packets", description="System network dropped_packets", unit="packets", ) - self.meter.create_observable_counter( + self._meter.create_observable_counter( callback=self._get_system_network_packets, name="system.network.packets", description="System network packets", unit="packets", ) - self.meter.create_observable_counter( + self._meter.create_observable_counter( callback=self._get_system_network_errors, name="system.network.errors", description="System network errors", unit="errors", ) - self.meter.create_observable_counter( + self._meter.create_observable_counter( callback=self._get_system_network_io, name="system.network.io", description="System network io", unit="bytes", ) - self.meter.create_observable_up_down_counter( + self._meter.create_observable_up_down_counter( callback=self._get_system_network_connections, name="system.network.connections", description="System network connections", unit="connections", ) - self.meter.create_observable_counter( + self._meter.create_observable_counter( callback=self._get_runtime_memory, name="runtime.{}.memory".format(self._python_implementation), description="Runtime {} memory".format( @@ -299,7 +303,7 @@ def __init__( unit="bytes", ) - self.meter.create_observable_counter( + self._meter.create_observable_counter( callback=self._get_runtime_cpu_time, name="runtime.{}.cpu_time".format(self._python_implementation), description="Runtime {} CPU time".format( @@ -308,7 +312,7 @@ def __init__( unit="seconds", ) - self.meter.create_observable_counter( + self._meter.create_observable_counter( callback=self._get_runtime_gc_count, name="runtime.{}.gc_count".format(self._python_implementation), description="Runtime {} GC count".format( @@ -316,6 +320,9 @@ def __init__( ), unit="bytes", ) + + def _uninstrument(self, **__): + pass def _get_system_cpu_time(self) -> Iterable[Measurement]: """Observer callback for system CPU time diff --git a/instrumentation/opentelemetry-instrumentation-system-metrics/src/opentelemetry/instrumentation/system_metrics/package.py b/instrumentation/opentelemetry-instrumentation-system-metrics/src/opentelemetry/instrumentation/system_metrics/package.py new file mode 100644 index 0000000000..7eb5b17a99 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-system-metrics/src/opentelemetry/instrumentation/system_metrics/package.py @@ -0,0 +1,16 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +_instruments = ("psutil >= 5.7.3",) diff --git a/instrumentation/opentelemetry-instrumentation-system-metrics/tests/test_system_metrics.py b/instrumentation/opentelemetry-instrumentation-system-metrics/tests/test_system_metrics.py index e8ef6257dd..9b37bfeddb 100644 --- a/instrumentation/opentelemetry-instrumentation-system-metrics/tests/test_system_metrics.py +++ b/instrumentation/opentelemetry-instrumentation-system-metrics/tests/test_system_metrics.py @@ -21,22 +21,54 @@ from opentelemetry._metrics import get_meter_provider from opentelemetry._metrics.measurement import Measurement from opentelemetry.instrumentation.system_metrics import SystemMetrics +from opentelemetry.sdk._metrics import MeterProvider +from opentelemetry.sdk._metrics.export import InMemoryMetricReader from opentelemetry.test.test_base import TestBase +def _mock_netconnection(): + NetConnection = namedtuple( + "NetworkConnection", ["family", "type", "status"] + ) + Type = namedtuple("Type", ["value"]) + return [ + NetConnection( + family=1, + status="ESTABLISHED", + type=Type(value=2), + ), + NetConnection( + family=1, + status="ESTABLISHED", + type=Type(value=1), + ), + ] + +class _SystemMetricsResult(): + def __init__(self, attributes, value) -> None: + self.attributes = attributes + self.value = value + class TestSystemMetrics(TestBase): def setUp(self): super().setUp() self.implementation = python_implementation().lower() - - def test_system_metrics_constructor(self): - # ensure the observers have been registered - meter = get_meter_provider().get_meter(__name__) - with mock.patch("opentelemetry.metrics.get_meter") as mock_get_meter: - mock_get_meter.return_value = meter - SystemMetrics() - - self.assertEqual(len(meter.instruments), 18) + self._patch_net_connections = mock.patch("psutil.net_connections", _mock_netconnection) + self._patch_net_connections.start() + + def tearDown(self): + super().tearDown() + self._patch_net_connections.stop() + SystemMetrics().uninstrument() + + def test_system_metrics_instrument(self): + reader = InMemoryMetricReader() + meter_provider = MeterProvider(metric_readers=[reader]) + system_metrics = SystemMetrics() + system_metrics.instrument(meter_provider=meter_provider) + metrics = reader.get_metrics() + metric_names = list({x.name for x in metrics }) + self.assertEqual(len(metric_names), 17) observer_names = [ "system.cpu.time", @@ -48,7 +80,6 @@ def test_system_metrics_constructor(self): "system.disk.io", "system.disk.operations", "system.disk.time", - "system.disk.merged", "system.network.dropped_packets", "system.network.packets", "system.network.errors", @@ -59,36 +90,37 @@ def test_system_metrics_constructor(self): "runtime.{}.gc_count".format(self.implementation), ] - for observer in meter.instruments.values(): - self.assertIn(observer.name, observer_names) - observer_names.remove(observer.name) + for observer in metric_names: + self.assertIn(observer, observer_names) + observer_names.remove(observer) - def _assert_metrics(self, observer_name, system_metrics, expected): - system_metrics.controller.tick() + def _assert_metrics(self, observer_name, reader, expected): assertions = 0 for ( metric ) in ( - self.memory_metrics_exporter._exported_metrics # pylint: disable=protected-access + reader.get_metrics() # pylint: disable=protected-access ): - if ( - metric.labels in expected - and metric.instrument.name == observer_name - ): - self.assertEqual( - metric.aggregator.checkpoint, - expected[metric.labels], - ) - assertions += 1 + for e in expected: + if ( + metric.attributes == e.attributes + and metric.name == observer_name + ): + self.assertEqual( + metric.point.value, + e.value, + ) + assertions += 1 self.assertEqual(len(expected), assertions) def _test_metrics(self, observer_name, expected): - meter = self.meter_provider.get_meter(__name__) + reader = InMemoryMetricReader() + meter_provider = MeterProvider(metric_readers=[reader]) - with mock.patch("opentelemetry.metrics.get_meter") as mock_get_meter: - mock_get_meter.return_value = meter - system_metrics = SystemMetrics() - self._assert_metrics(observer_name, system_metrics, expected) + system_metrics = SystemMetrics() + system_metrics.instrument(meter_provider=meter_provider) + self._assert_metrics(observer_name, reader, expected) + system_metrics.uninstrument() # When this test case is executed, _get_system_cpu_utilization gets run # too because of the controller thread which runs all observers. This patch @@ -103,40 +135,40 @@ def test_system_cpu_time(self, mock_cpu_times, mock_cpu_times_percent): CPUTimes(idle=1.2, user=3.4, system=5.6, irq=7.8), ] - expected = { - ( - ("cpu", 1), - ("state", "idle"), - ): 1.2, - ( - ("cpu", 1), - ("state", "user"), - ): 3.4, - ( - ("cpu", 1), - ("state", "system"), - ): 5.6, - ( - ("cpu", 1), - ("state", "irq"), - ): 7.8, - ( - ("cpu", 2), - ("state", "idle"), - ): 1.2, - ( - ("cpu", 2), - ("state", "user"), - ): 3.4, - ( - ("cpu", 2), - ("state", "system"), - ): 5.6, - ( - ("cpu", 2), - ("state", "irq"), - ): 7.8, - } + expected = [ + _SystemMetricsResult({ + "cpu": 1, + "state": "idle", + }, 1.2), + _SystemMetricsResult({ + "cpu": 1, + "state": "user", + }, 3.4), + _SystemMetricsResult({ + "cpu": 1, + "state": "system", + }, 5.6), + _SystemMetricsResult({ + "cpu": 1, + "state": "irq", + }, 7.8), + _SystemMetricsResult({ + "cpu": 2, + "state": "idle", + }, 1.2), + _SystemMetricsResult({ + "cpu": 2, + "state": "user", + }, 3.4), + _SystemMetricsResult({ + "cpu": 2, + "state": "system", + }, 5.6), + _SystemMetricsResult({ + "cpu": 2, + "state": "irq", + }, 7.8), + ] self._test_metrics("system.cpu.time", expected) @mock.patch("psutil.cpu_times_percent") @@ -149,64 +181,16 @@ def test_system_cpu_utilization(self, mock_cpu_times_percent): CPUTimesPercent(idle=1.2, user=3.4, system=5.6, irq=7.8), ] - expected = { - (("cpu", 1), ("state", "idle"),): Measurement._TYPE( - min=1.2 / 100, - max=1.2 / 100, - sum=1.2 / 100, - count=1, - last=1.2 / 100, - ), - (("cpu", 1), ("state", "user"),): Measurement._TYPE( - min=3.4 / 100, - max=3.4 / 100, - sum=3.4 / 100, - count=1, - last=3.4 / 100, - ), - (("cpu", 1), ("state", "system"),): Measurement._TYPE( - min=5.6 / 100, - max=5.6 / 100, - sum=5.6 / 100, - count=1, - last=5.6 / 100, - ), - (("cpu", 1), ("state", "irq"),): Measurement._TYPE( - min=7.8 / 100, - max=7.8 / 100, - sum=7.8 / 100, - count=1, - last=7.8 / 100, - ), - (("cpu", 2), ("state", "idle"),): Measurement._TYPE( - min=1.2 / 100, - max=1.2 / 100, - sum=1.2 / 100, - count=1, - last=1.2 / 100, - ), - (("cpu", 2), ("state", "user"),): Measurement._TYPE( - min=3.4 / 100, - max=3.4 / 100, - sum=3.4 / 100, - count=1, - last=3.4 / 100, - ), - (("cpu", 2), ("state", "system"),): Measurement._TYPE( - min=5.6 / 100, - max=5.6 / 100, - sum=5.6 / 100, - count=1, - last=5.6 / 100, - ), - (("cpu", 2), ("state", "irq"),): Measurement._TYPE( - min=7.8 / 100, - max=7.8 / 100, - sum=7.8 / 100, - count=1, - last=7.8 / 100, - ), - } + expected = [ + _SystemMetricsResult({"cpu": 1, "state": "idle"}, 1.2 / 100), + _SystemMetricsResult({"cpu": 1, "state": "user"},3.4 / 100), + _SystemMetricsResult({"cpu": 1, "state": "system"}, 5.6 / 100), + _SystemMetricsResult({"cpu": 1, "state": "irq"}, 7.8 / 100), + _SystemMetricsResult({"cpu": 2, "state": "idle"}, 1.2 / 100), + _SystemMetricsResult({"cpu": 2, "state": "user"}, 3.4 / 100), + _SystemMetricsResult({"cpu": 2, "state": "system"}, 5.6 / 100), + _SystemMetricsResult({"cpu": 2, "state": "irq"}, 7.8 / 100), + ] self._test_metrics("system.cpu.utilization", expected) @mock.patch("psutil.virtual_memory") @@ -218,17 +202,11 @@ def test_system_memory_usage(self, mock_virtual_memory): used=1, free=2, cached=3, total=4 ) - expected = { - (("state", "used"),): Measurement._TYPE( - min=1, max=1, sum=1, count=1, last=1 - ), - (("state", "free"),): Measurement._TYPE( - min=2, max=2, sum=2, count=1, last=2 - ), - (("state", "cached"),): Measurement._TYPE( - min=3, max=3, sum=3, count=1, last=3 - ), - } + expected = [ + _SystemMetricsResult({"state": "used"}, 1), + _SystemMetricsResult({"state": "free"}, 2), + _SystemMetricsResult({"state": "cached"}, 3), + ] self._test_metrics("system.memory.usage", expected) @mock.patch("psutil.virtual_memory") @@ -240,17 +218,11 @@ def test_system_memory_utilization(self, mock_virtual_memory): used=1, free=2, cached=3, total=4 ) - expected = { - (("state", "used"),): Measurement._TYPE( - min=1 / 4, max=1 / 4, sum=1 / 4, count=1, last=1 / 4 - ), - (("state", "free"),): Measurement._TYPE( - min=2 / 4, max=2 / 4, sum=2 / 4, count=1, last=2 / 4 - ), - (("state", "cached"),): Measurement._TYPE( - min=3 / 4, max=3 / 4, sum=3 / 4, count=1, last=3 / 4 - ), - } + expected = [ + _SystemMetricsResult({"state": "used"}, 1 / 4), + _SystemMetricsResult({"state": "free"}, 2 / 4), + _SystemMetricsResult({"state": "cached"}, 3 / 4), + ] self._test_metrics("system.memory.utilization", expected) @mock.patch("psutil.swap_memory") @@ -258,14 +230,10 @@ def test_system_swap_usage(self, mock_swap_memory): SwapMemory = namedtuple("SwapMemory", ["used", "free", "total"]) mock_swap_memory.return_value = SwapMemory(used=1, free=2, total=3) - expected = { - (("state", "used"),): Measurement._TYPE( - min=1, max=1, sum=1, count=1, last=1 - ), - (("state", "free"),): Measurement._TYPE( - min=2, max=2, sum=2, count=1, last=2 - ), - } + expected = [ + _SystemMetricsResult({"state": "used"}, 1), + _SystemMetricsResult({"state": "free"}, 2), + ] self._test_metrics("system.swap.usage", expected) @mock.patch("psutil.swap_memory") @@ -273,14 +241,10 @@ def test_system_swap_utilization(self, mock_swap_memory): SwapMemory = namedtuple("SwapMemory", ["used", "free", "total"]) mock_swap_memory.return_value = SwapMemory(used=1, free=2, total=3) - expected = { - (("state", "used"),): Measurement._TYPE( - min=1 / 3, max=1 / 3, sum=1 / 3, count=1, last=1 / 3 - ), - (("state", "free"),): Measurement._TYPE( - min=2 / 3, max=2 / 3, sum=2 / 3, count=1, last=2 / 3 - ), - } + expected = [ + _SystemMetricsResult({"state": "used"}, 1/3), + _SystemMetricsResult({"state": "free"}, 2/3), + ] self._test_metrics("system.swap.utilization", expected) @mock.patch("psutil.disk_io_counters") @@ -321,24 +285,12 @@ def test_system_disk_io(self, mock_disk_io_counters): ), } - expected = { - ( - ("device", "sda"), - ("direction", "read"), - ): 3, - ( - ("device", "sda"), - ("direction", "write"), - ): 4, - ( - ("device", "sdb"), - ("direction", "read"), - ): 11, - ( - ("device", "sdb"), - ("direction", "write"), - ): 12, - } + expected = [ + _SystemMetricsResult({"device": "sda", "direction": "read"}, 3), + _SystemMetricsResult({"device": "sda", "direction": "write"}, 4), + _SystemMetricsResult({"device": "sdb", "direction": "read"}, 11), + _SystemMetricsResult({"device": "sdb", "direction": "write"}, 12), + ] self._test_metrics("system.disk.io", expected) @mock.patch("psutil.disk_io_counters") @@ -379,24 +331,12 @@ def test_system_disk_operations(self, mock_disk_io_counters): ), } - expected = { - ( - ("device", "sda"), - ("direction", "read"), - ): 1, - ( - ("device", "sda"), - ("direction", "write"), - ): 2, - ( - ("device", "sdb"), - ("direction", "read"), - ): 9, - ( - ("device", "sdb"), - ("direction", "write"), - ): 10, - } + expected = [ + _SystemMetricsResult({"device": "sda", "direction": "read"}, 1), + _SystemMetricsResult({"device": "sda", "direction": "write"}, 2), + _SystemMetricsResult({"device": "sdb", "direction": "read"}, 9), + _SystemMetricsResult({"device": "sdb", "direction": "write"}, 10), + ] self._test_metrics("system.disk.operations", expected) @mock.patch("psutil.disk_io_counters") @@ -437,88 +377,14 @@ def test_system_disk_time(self, mock_disk_io_counters): ), } - expected = { - ( - ("device", "sda"), - ("direction", "read"), - ): 5 - / 1000, - ( - ("device", "sda"), - ("direction", "write"), - ): 6 - / 1000, - ( - ("device", "sdb"), - ("direction", "read"), - ): 13 - / 1000, - ( - ("device", "sdb"), - ("direction", "write"), - ): 14 - / 1000, - } + expected = [ + _SystemMetricsResult({"device": "sda", "direction": "read"}, 5/ 1000), + _SystemMetricsResult({"device": "sda", "direction": "write"}, 6/ 1000), + _SystemMetricsResult({"device": "sdb", "direction": "read"}, 13/ 1000), + _SystemMetricsResult({"device": "sdb", "direction": "write"}, 14/ 1000), + ] self._test_metrics("system.disk.time", expected) - @mock.patch("psutil.disk_io_counters") - def test_system_disk_merged(self, mock_disk_io_counters): - DiskIO = namedtuple( - "DiskIO", - [ - "read_count", - "write_count", - "read_bytes", - "write_bytes", - "read_time", - "write_time", - "read_merged_count", - "write_merged_count", - ], - ) - mock_disk_io_counters.return_value = { - "sda": DiskIO( - read_count=1, - write_count=2, - read_bytes=3, - write_bytes=4, - read_time=5, - write_time=6, - read_merged_count=7, - write_merged_count=8, - ), - "sdb": DiskIO( - read_count=9, - write_count=10, - read_bytes=11, - write_bytes=12, - read_time=13, - write_time=14, - read_merged_count=15, - write_merged_count=16, - ), - } - - expected = { - ( - ("device", "sda"), - ("direction", "read"), - ): 7, - ( - ("device", "sda"), - ("direction", "write"), - ): 8, - ( - ("device", "sdb"), - ("direction", "read"), - ): 15, - ( - ("device", "sdb"), - ("direction", "write"), - ): 16, - } - self._test_metrics("system.disk.merged", expected) - @mock.patch("psutil.net_io_counters") def test_system_network_dropped_packets(self, mock_net_io_counters): NetIO = namedtuple( @@ -557,24 +423,12 @@ def test_system_network_dropped_packets(self, mock_net_io_counters): ), } - expected = { - ( - ("device", "eth0"), - ("direction", "receive"), - ): 1, - ( - ("device", "eth0"), - ("direction", "transmit"), - ): 2, - ( - ("device", "eth1"), - ("direction", "receive"), - ): 9, - ( - ("device", "eth1"), - ("direction", "transmit"), - ): 10, - } + expected = [ + _SystemMetricsResult({"device": "eth0", "direction": "receive"}, 1), + _SystemMetricsResult({"device": "eth0", "direction": "transmit"}, 2), + _SystemMetricsResult({"device": "eth1", "direction": "receive"}, 9), + _SystemMetricsResult({"device": "eth1", "direction": "transmit"}, 10), + ] self._test_metrics("system.network.dropped_packets", expected) @mock.patch("psutil.net_io_counters") @@ -615,24 +469,12 @@ def test_system_network_packets(self, mock_net_io_counters): ), } - expected = { - ( - ("device", "eth0"), - ("direction", "receive"), - ): 4, - ( - ("device", "eth0"), - ("direction", "transmit"), - ): 3, - ( - ("device", "eth1"), - ("direction", "receive"), - ): 12, - ( - ("device", "eth1"), - ("direction", "transmit"), - ): 11, - } + expected = [ + _SystemMetricsResult({"device": "eth0", "direction": "receive"}, 4), + _SystemMetricsResult({"device": "eth0", "direction": "transmit"}, 3), + _SystemMetricsResult({"device": "eth1", "direction": "receive"}, 12), + _SystemMetricsResult({"device": "eth1", "direction": "transmit"}, 11), + ] self._test_metrics("system.network.packets", expected) @mock.patch("psutil.net_io_counters") @@ -673,24 +515,12 @@ def test_system_network_errors(self, mock_net_io_counters): ), } - expected = { - ( - ("device", "eth0"), - ("direction", "receive"), - ): 5, - ( - ("device", "eth0"), - ("direction", "transmit"), - ): 6, - ( - ("device", "eth1"), - ("direction", "receive"), - ): 13, - ( - ("device", "eth1"), - ("direction", "transmit"), - ): 14, - } + expected = [ + _SystemMetricsResult({"device": "eth0", "direction": "receive"}, 5), + _SystemMetricsResult({"device": "eth0", "direction": "transmit"}, 6), + _SystemMetricsResult({"device": "eth1", "direction": "receive"}, 13), + _SystemMetricsResult({"device": "eth1", "direction": "transmit"}, 14), + ] self._test_metrics("system.network.errors", expected) @mock.patch("psutil.net_io_counters") @@ -731,24 +561,12 @@ def test_system_network_io(self, mock_net_io_counters): ), } - expected = { - ( - ("device", "eth0"), - ("direction", "receive"), - ): 8, - ( - ("device", "eth0"), - ("direction", "transmit"), - ): 7, - ( - ("device", "eth1"), - ("direction", "receive"), - ): 16, - ( - ("device", "eth1"), - ("direction", "transmit"), - ): 15, - } + expected = [ + _SystemMetricsResult({"device": "eth0", "direction": "receive"}, 8), + _SystemMetricsResult({"device": "eth0", "direction": "transmit"}, 7), + _SystemMetricsResult({"device": "eth1", "direction": "receive"}, 16), + _SystemMetricsResult({"device": "eth1", "direction": "transmit"}, 15), + ] self._test_metrics("system.network.io", expected) @mock.patch("psutil.net_connections") @@ -770,20 +588,10 @@ def test_system_network_connections(self, mock_net_connections): ), ] - expected = { - ( - ("family", 1), - ("protocol", "udp"), - ("state", "ESTABLISHED"), - ("type", Type(value=2)), - ): 1, - ( - ("family", 1), - ("protocol", "tcp"), - ("state", "ESTABLISHED"), - ("type", Type(value=1)), - ): 1, - } + expected = [ + _SystemMetricsResult({"family": 1, "protocol": "udp", "state": "ESTABLISHED", "type": Type(value=2)}, 1), + _SystemMetricsResult({"family": 1, "protocol": "tcp", "state": "ESTABLISHED", "type": Type(value=1)}, 1), + ] self._test_metrics("system.network.connections", expected) @mock.patch("psutil.Process.memory_info") @@ -795,10 +603,10 @@ def test_runtime_memory(self, mock_process_memory_info): **{"return_value": PMem(rss=1, vms=2)} ) - expected = { - (("type", "rss"),): 1, - (("type", "vms"),): 2, - } + expected = [ + _SystemMetricsResult({"type": "rss"}, 1), + _SystemMetricsResult({"type": "vms"}, 2), + ] self._test_metrics( "runtime.{}.memory".format(self.implementation), expected ) @@ -812,10 +620,10 @@ def test_runtime_cpu_time(self, mock_process_cpu_times): **{"return_value": PCPUTimes(user=1.1, system=2.2)} ) - expected = { - (("type", "user"),): 1.1, - (("type", "system"),): 2.2, - } + expected = [ + _SystemMetricsResult({"type": "user"}, 1.1), + _SystemMetricsResult({"type": "system"}, 2.2), + ] self._test_metrics( "runtime.{}.cpu_time".format(self.implementation), expected ) @@ -825,11 +633,11 @@ def test_runtime_get_count(self, mock_gc_get_count): mock_gc_get_count.configure_mock(**{"return_value": (1, 2, 3)}) - expected = { - (("count", "0"),): 1, - (("count", "1"),): 2, - (("count", "2"),): 3, - } + expected = [ + _SystemMetricsResult({"count": "0"}, 1), + _SystemMetricsResult({"count": "1"}, 2), + _SystemMetricsResult({"count": "2"}, 3), + ] self._test_metrics( "runtime.{}.gc_count".format(self.implementation), expected )