From c61615a3437cd16f613046921491c48ad0f7faa1 Mon Sep 17 00:00:00 2001 From: Rytis Bagdziunas Date: Fri, 11 Nov 2022 22:56:45 +0100 Subject: [PATCH 1/7] Add a test case to reproduce the issue --- .../tests/test_system_metrics.py | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) 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 aadc834301..3d4d0eb328 100644 --- a/instrumentation/opentelemetry-instrumentation-system-metrics/tests/test_system_metrics.py +++ b/instrumentation/opentelemetry-instrumentation-system-metrics/tests/test_system_metrics.py @@ -61,6 +61,9 @@ def setUp(self): ) self._patch_net_connections.start() + # Reset the singleton class on each test run + SystemMetricsInstrumentor._instance = None + def tearDown(self): super().tearDown() self._patch_net_connections.stop() @@ -103,6 +106,36 @@ def test_system_metrics_instrument(self): self.assertIn(observer, observer_names) observer_names.remove(observer) + def test_runtime_metrics_instrument(self): + + runtime_config = { + f"runtime.memory": ["rss", "vms"], + f"runtime.cpu.time": ["user", "system"], + f"runtime.gc_count": None, + } + + reader = InMemoryMetricReader() + meter_provider = MeterProvider(metric_readers=[reader]) + runtime_metrics = SystemMetricsInstrumentor(config=runtime_config) + runtime_metrics.instrument(meter_provider=meter_provider) + + metric_names = [] + for resource_metrics in reader.get_metrics_data().resource_metrics: + for scope_metrics in resource_metrics.scope_metrics: + for metric in scope_metrics.metrics: + metric_names.append(metric.name) + self.assertEqual(len(metric_names), 3) + + observer_names = [ + f"runtime.{self.implementation}.memory", + f"runtime.{self.implementation}.cpu_time", + f"runtime.{self.implementation}.gc_count", + ] + + for observer in metric_names: + self.assertIn(observer, observer_names) + observer_names.remove(observer) + def _assert_metrics(self, observer_name, reader, expected): assertions = 0 # pylint: disable=too-many-nested-blocks From 70154e8d14eef7cf3cc6824aa838e6adf55a1f79 Mon Sep 17 00:00:00 2001 From: Rytis Bagdziunas Date: Fri, 11 Nov 2022 22:57:44 +0100 Subject: [PATCH 2/7] Fix the class initialization when parameters are provided --- .../src/opentelemetry/instrumentation/instrumentor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/instrumentor.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/instrumentor.py index 060ac484e7..a5bdc18df2 100644 --- a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/instrumentor.py +++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/instrumentor.py @@ -48,7 +48,7 @@ class BaseInstrumentor(ABC): def __new__(cls, *args, **kwargs): if cls._instance is None: - cls._instance = object.__new__(cls, *args, **kwargs) + cls._instance = object.__new__(cls) return cls._instance From 680da6456fee6deffdc1109c1b78d11e09cf1e2d Mon Sep 17 00:00:00 2001 From: Rytis Bagdziunas Date: Tue, 15 Nov 2022 14:21:03 +0100 Subject: [PATCH 3/7] Update CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 626c29bb2b..a0de42d9ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#1424](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1424)) - Remove db.name attribute from Redis instrumentation ([#1427](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1427)) +- `opentelemetry-instrumentation-system-metrics` Fix initialization of the instrumentation class when configuration is provided + ([#1438](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1439)) ## Version 1.14.0/0.35b0 (2022-11-03) From 2755adec48387a25651c3b552d33c87c388fe5ce Mon Sep 17 00:00:00 2001 From: Rytis Bagdziunas Date: Tue, 15 Nov 2022 14:27:35 +0100 Subject: [PATCH 4/7] Fix linting issues --- .../tests/test_system_metrics.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 3d4d0eb328..42ed2af57c 100644 --- a/instrumentation/opentelemetry-instrumentation-system-metrics/tests/test_system_metrics.py +++ b/instrumentation/opentelemetry-instrumentation-system-metrics/tests/test_system_metrics.py @@ -109,9 +109,9 @@ def test_system_metrics_instrument(self): def test_runtime_metrics_instrument(self): runtime_config = { - f"runtime.memory": ["rss", "vms"], - f"runtime.cpu.time": ["user", "system"], - f"runtime.gc_count": None, + "runtime.memory": ["rss", "vms"], + "runtime.cpu.time": ["user", "system"], + "runtime.gc_count": None, } reader = InMemoryMetricReader() From 817938867aa8b68fdfab3231b0f1243456436f2c Mon Sep 17 00:00:00 2001 From: Rytis Bagdziunas Date: Fri, 18 Nov 2022 20:58:23 +0100 Subject: [PATCH 5/7] Additional test case which inits SystemMetricsInstrumentor twice --- .../tests/test_system_metrics.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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 42ed2af57c..797274c40d 100644 --- a/instrumentation/opentelemetry-instrumentation-system-metrics/tests/test_system_metrics.py +++ b/instrumentation/opentelemetry-instrumentation-system-metrics/tests/test_system_metrics.py @@ -69,6 +69,24 @@ def tearDown(self): self._patch_net_connections.stop() SystemMetricsInstrumentor().uninstrument() + def test_system_metrics_instrumentor_initialization(self): + + try: + SystemMetricsInstrumentor() + SystemMetricsInstrumentor(config={}) + except Exception as error: # pylint: disable=broad-except + self.fail(f"Unexpected exception {error} raised") + + SystemMetricsInstrumentor._instance = None + + try: + SystemMetricsInstrumentor(config={}) + SystemMetricsInstrumentor() + except Exception as error: # pylint: disable=broad-except + self.fail(f"Unexpected exception {error} raised") + + SystemMetricsInstrumentor().instrument() + def test_system_metrics_instrument(self): reader = InMemoryMetricReader() meter_provider = MeterProvider(metric_readers=[reader]) From 7d7da96919e757ccb0a949c157629497a244c9fb Mon Sep 17 00:00:00 2001 From: Rytis Bagdziunas Date: Wed, 22 Feb 2023 22:12:55 +0100 Subject: [PATCH 6/7] Updated linting following update to black style --- .../tests/test_system_metrics.py | 2 -- 1 file changed, 2 deletions(-) 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 2e6e9e4668..763f629ed5 100644 --- a/instrumentation/opentelemetry-instrumentation-system-metrics/tests/test_system_metrics.py +++ b/instrumentation/opentelemetry-instrumentation-system-metrics/tests/test_system_metrics.py @@ -70,7 +70,6 @@ def tearDown(self): SystemMetricsInstrumentor().uninstrument() def test_system_metrics_instrumentor_initialization(self): - try: SystemMetricsInstrumentor() SystemMetricsInstrumentor(config={}) @@ -125,7 +124,6 @@ def test_system_metrics_instrument(self): observer_names.remove(observer) def test_runtime_metrics_instrument(self): - runtime_config = { "runtime.memory": ["rss", "vms"], "runtime.cpu.time": ["user", "system"], From bded09e99ff6de63550b17e958610044d8c22e4d Mon Sep 17 00:00:00 2001 From: Rytis Bagdziunas Date: Thu, 23 Feb 2023 21:11:55 +0100 Subject: [PATCH 7/7] Moved changelog entry to unreleased section --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 894a1a7dbd..905b84e6ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fix Flask instrumentation to only close the span if it was created by the same thread. ([#1654](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1654)) +- `opentelemetry-instrumentation-system-metrics` Fix initialization of the instrumentation class when configuration is provided + ([#1438](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1439)) ## Version 1.16.0/0.37b0 (2023-02-17) @@ -102,8 +104,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#1424](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1424)) - Remove db.name attribute from Redis instrumentation ([#1427](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1427)) -- `opentelemetry-instrumentation-system-metrics` Fix initialization of the instrumentation class when configuration is provided - ([#1438](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1439)) - `opentelemetry-instrumentation-asgi` Fix target extraction for duration metric ([#1461](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1461)) - Add grpc.aio instrumentation to package entry points