diff --git a/CHANGELOG.md b/CHANGELOG.md index 0537ee147e..3e8f981e0e 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 8c2416fe49..763f629ed5 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 @@ 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() 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 @@ def test_system_metrics_instrument(self): 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 160fe3c450..7f05e7f30a 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