Skip to content

Commit 4a859e3

Browse files
rbagdsrikanthccvocelotllzchen
authored
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 <[email protected]> Co-authored-by: Diego Hurtado <[email protected]> Co-authored-by: Leighton Chen <[email protected]>
1 parent b701980 commit 4a859e3

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2424

2525
- Fix Flask instrumentation to only close the span if it was created by the same thread.
2626
([#1654](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1654))
27+
- `opentelemetry-instrumentation-system-metrics` Fix initialization of the instrumentation class when configuration is provided
28+
([#1438](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1439))
2729

2830
## Version 1.16.0/0.37b0 (2023-02-17)
2931

instrumentation/opentelemetry-instrumentation-system-metrics/tests/test_system_metrics.py

+49
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,31 @@ def setUp(self):
6161
)
6262
self._patch_net_connections.start()
6363

64+
# Reset the singleton class on each test run
65+
SystemMetricsInstrumentor._instance = None
66+
6467
def tearDown(self):
6568
super().tearDown()
6669
self._patch_net_connections.stop()
6770
SystemMetricsInstrumentor().uninstrument()
6871

72+
def test_system_metrics_instrumentor_initialization(self):
73+
try:
74+
SystemMetricsInstrumentor()
75+
SystemMetricsInstrumentor(config={})
76+
except Exception as error: # pylint: disable=broad-except
77+
self.fail(f"Unexpected exception {error} raised")
78+
79+
SystemMetricsInstrumentor._instance = None
80+
81+
try:
82+
SystemMetricsInstrumentor(config={})
83+
SystemMetricsInstrumentor()
84+
except Exception as error: # pylint: disable=broad-except
85+
self.fail(f"Unexpected exception {error} raised")
86+
87+
SystemMetricsInstrumentor().instrument()
88+
6989
def test_system_metrics_instrument(self):
7090
reader = InMemoryMetricReader()
7191
meter_provider = MeterProvider(metric_readers=[reader])
@@ -103,6 +123,35 @@ def test_system_metrics_instrument(self):
103123
self.assertIn(observer, observer_names)
104124
observer_names.remove(observer)
105125

126+
def test_runtime_metrics_instrument(self):
127+
runtime_config = {
128+
"runtime.memory": ["rss", "vms"],
129+
"runtime.cpu.time": ["user", "system"],
130+
"runtime.gc_count": None,
131+
}
132+
133+
reader = InMemoryMetricReader()
134+
meter_provider = MeterProvider(metric_readers=[reader])
135+
runtime_metrics = SystemMetricsInstrumentor(config=runtime_config)
136+
runtime_metrics.instrument(meter_provider=meter_provider)
137+
138+
metric_names = []
139+
for resource_metrics in reader.get_metrics_data().resource_metrics:
140+
for scope_metrics in resource_metrics.scope_metrics:
141+
for metric in scope_metrics.metrics:
142+
metric_names.append(metric.name)
143+
self.assertEqual(len(metric_names), 3)
144+
145+
observer_names = [
146+
f"runtime.{self.implementation}.memory",
147+
f"runtime.{self.implementation}.cpu_time",
148+
f"runtime.{self.implementation}.gc_count",
149+
]
150+
151+
for observer in metric_names:
152+
self.assertIn(observer, observer_names)
153+
observer_names.remove(observer)
154+
106155
def _assert_metrics(self, observer_name, reader, expected):
107156
assertions = 0
108157
# pylint: disable=too-many-nested-blocks

opentelemetry-instrumentation/src/opentelemetry/instrumentation/instrumentor.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class BaseInstrumentor(ABC):
4747

4848
def __new__(cls, *args, **kwargs):
4949
if cls._instance is None:
50-
cls._instance = object.__new__(cls, *args, **kwargs)
50+
cls._instance = object.__new__(cls)
5151

5252
return cls._instance
5353

0 commit comments

Comments
 (0)