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 <srikanth.chekuri92@gmail.com>
Co-authored-by: Diego Hurtado <ocelotl@users.noreply.github.com>
Co-authored-by: Leighton Chen <lechen@microsoft.com>
This commit is contained in:
Rytis Bagdziunas
2023-02-28 18:19:30 +01:00
committed by GitHub
parent b701980ab8
commit 4a859e34d6
3 changed files with 52 additions and 1 deletions

View File

@ -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)

View File

@ -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

View File

@ -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