From 93d1d1143e01cb92e2da1dca44680905be677fd0 Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Wed, 2 Sep 2020 20:20:52 -0600 Subject: [PATCH] Fix lint and use metrics --- .../system_metrics/__init__.py | 14 +++++---- .../tests/test_system_metrics.py | 29 ++++++++++--------- 2 files changed, 25 insertions(+), 18 deletions(-) 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 6bec063ef58..5882c0947ad 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 @@ -63,11 +63,13 @@ from opentelemetry import metrics from opentelemetry.sdk.metrics import ( - SumObserver, ValueObserver, UpDownSumObserver + SumObserver, + UpDownSumObserver, + ValueObserver, ) -from opentelemetry.sdk.util import get_dict_as_key from opentelemetry.sdk.metrics.export import MetricsExporter from opentelemetry.sdk.metrics.export.controller import PushController +from opentelemetry.sdk.util import get_dict_as_key class SystemMetrics: @@ -616,6 +618,9 @@ def _get_system_network_connections( self._system_network_connections_labels[ "state" ] = net_connection.status + self._system_network_connections_labels[metric] = getattr( + net_connection, metric + ) connection_counters_key = get_dict_as_key( self._system_network_connections_labels @@ -626,13 +631,12 @@ def _get_system_network_connections( else: connection_counters[connection_counters_key] = { "counter": 1, - "labels": self._system_network_connections_labels.copy() + "labels": self._system_network_connections_labels.copy(), } for connection_counter in connection_counters.values(): observer.observe( - connection_counter["counter"], - connection_counter["labels"], + connection_counter["counter"], connection_counter["labels"], ) def _get_runtime_memory(self, observer: metrics.SumObserver) -> None: 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 7395213fbf9..2f155383f46 100644 --- a/instrumentation/opentelemetry-instrumentation-system-metrics/tests/test_system_metrics.py +++ b/instrumentation/opentelemetry-instrumentation-system-metrics/tests/test_system_metrics.py @@ -72,7 +72,7 @@ def _assert_metrics(self, observer_name, system_metrics, expected): ) in ( self.memory_metrics_exporter._exported_metrics # pylint: disable=protected-access ): - if ( + if ( metric.labels in expected and metric.instrument.name == observer_name ): @@ -93,6 +93,7 @@ def _test_metrics(self, observer_name, expected): # When this test case is executed, _get_system_cpu_utilization gets run # too because of the controller thread which runs all observers. This patch # is added here to stop a warning that would otherwise be raised. + # pylint: disable=unused-argument @mock.patch("psutil.cpu_times_percent") @mock.patch("psutil.cpu_times") def test_system_cpu_time(self, mock_cpu_times, mock_cpu_times_percent): @@ -633,21 +634,23 @@ def test_system_network_connections(self, mock_net_connections): ) Type = namedtuple("Type", ["value"]) mock_net_connections.return_value = [ - NetConnection( - family=1, - status="ESTABLISHED", - type=Type(value=2), - ), - NetConnection( - family=1, - status="ESTABLISHED", - type=Type(value=1), - ), + NetConnection(family=1, status="ESTABLISHED", type=Type(value=2),), + NetConnection(family=1, status="ESTABLISHED", type=Type(value=1),), ] expected = { - (("protocol", "udp"), ("state", "ESTABLISHED"),): 1, - (("protocol", "tcp"), ("state", "ESTABLISHED"),): 1, + ( + ("family", 1), + ("protocol", "udp"), + ("state", "ESTABLISHED"), + ("type", Type(value=2)), + ): 1, + ( + ("family", 1), + ("protocol", "tcp"), + ("state", "ESTABLISHED"), + ("type", Type(value=1)), + ): 1, } self._test_metrics("system.network.connections", expected)