From 4a859e34d6ac38966cda94c7ec3f5b586613697e Mon Sep 17 00:00:00 2001 From: Rytis Bagdziunas Date: Tue, 28 Feb 2023 18:19:30 +0100 Subject: [PATCH] Remove custom arguments in object.__new__ of BaseInstrumentor (#1439) * Add a test case to reproduce the issue * Fix the class initialization when parameters are provided * Update CHANGELOG.md * Fix linting issues * Additional test case which inits SystemMetricsInstrumentor twice * Updated linting following update to black style * Moved changelog entry to unreleased section --------- Co-authored-by: Srikanth Chekuri Co-authored-by: Diego Hurtado Co-authored-by: Leighton Chen --- CHANGELOG.md | 2 + .../tests/test_system_metrics.py | 49 +++++++++++++++++++ .../instrumentation/instrumentor.py | 2 +- 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0537ee147..3e8f981e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,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) 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 8c2416fe4..763f629ed 100644 --- a/instrumentation/opentelemetry-instrumentation-system-metrics/tests/test_system_metrics.py +++ b/instrumentation/opentelemetry-instrumentation-system-metrics/tests/test_system_metrics.py @@ -61,11 +61,31 @@ class TestSystemMetrics(TestBase): ) 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() 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]) @@ -103,6 +123,35 @@ class TestSystemMetrics(TestBase): self.assertIn(observer, observer_names) observer_names.remove(observer) + def test_runtime_metrics_instrument(self): + runtime_config = { + "runtime.memory": ["rss", "vms"], + "runtime.cpu.time": ["user", "system"], + "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 diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/instrumentor.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/instrumentor.py index 160fe3c45..7f05e7f30 100644 --- a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/instrumentor.py +++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/instrumentor.py @@ -47,7 +47,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