Skip to content

Commit 6f6c28d

Browse files
Added support for pypy3 system metrics (#2062)
* Added support for pypy3 system metrics Co-authored-by: Suryanarayana Peri <[email protected]> Signed-off-by: Rahul Kumar <[email protected]> * Change in if condition to log warn message for pypy Co-authored-by: Suryanarayana Peri <[email protected]> Signed-off-by: Rahul Kumar <[email protected]> * Added logger to else block if pypy Co-authored-by: Suryanarayana Peri <[email protected]> Signed-off-by: Rahul Kumar <[email protected]> * Reverting the changes Co-authored-by: Suryanarayana Peri <[email protected]> Signed-off-by: Rahul Kumar <[email protected]> * Changes requested by external reviewer Co-authored-by: Suryanarayana Peri <[email protected]> Signed-off-by: Rahul Kumar <[email protected]> --------- Signed-off-by: Rahul Kumar <[email protected]> Co-authored-by: Suryanarayana Peri <[email protected]>
1 parent b6d77f1 commit 6f6c28d

File tree

4 files changed

+37
-13
lines changed

4 files changed

+37
-13
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1394,6 +1394,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
13941394
- `opentelemetry-ext-wsgi` Updates for core library changes
13951395
- `opentelemetry-ext-http-requests` Updates for core library changes
13961396

1397+
- `Added support for PyPy3` Initial release
1398+
## [#1033](https://github.com/open-telemetryopentelemetry-python-contrib/issues/1033)
1399+
13971400
## Version 0.1a0 (2019-09-30)
13981401

13991402
### Added

instrumentation/opentelemetry-instrumentation-system-metrics/src/opentelemetry/instrumentation/system_metrics/__init__.py

+16-6
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777

7878
import gc
7979
import os
80+
import logging
8081
import threading
8182
from platform import python_implementation
8283
from typing import Collection, Dict, Iterable, List, Optional
@@ -91,6 +92,9 @@
9192
from opentelemetry.metrics import CallbackOptions, Observation, get_meter
9293
from opentelemetry.sdk.util import get_dict_as_key
9394

95+
_logger = logging.getLogger(__name__)
96+
97+
9498
_DEFAULT_CONFIG = {
9599
"system.cpu.time": ["idle", "user", "system", "irq"],
96100
"system.cpu.utilization": ["idle", "user", "system", "irq"],
@@ -352,12 +356,18 @@ def _instrument(self, **kwargs):
352356
)
353357

354358
if "process.runtime.gc_count" in self._config:
355-
self._meter.create_observable_counter(
356-
name=f"process.runtime.{self._python_implementation}.gc_count",
357-
callbacks=[self._get_runtime_gc_count],
358-
description=f"Runtime {self._python_implementation} GC count",
359-
unit="bytes",
360-
)
359+
if self._python_implementation == "pypy":
360+
_logger.warning(
361+
"The process.runtime.gc_count metric won't be collected because the interpreter is PyPy"
362+
)
363+
else:
364+
self._meter.create_observable_counter(
365+
name=f"process.runtime.{self._python_implementation}.gc_count",
366+
callbacks=[self._get_runtime_gc_count],
367+
description=f"Runtime {self._python_implementation} GC count",
368+
unit="bytes",
369+
)
370+
361371

362372
if "process.runtime.thread_count" in self._config:
363373
self._meter.create_observable_up_down_counter(

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

+17-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
from collections import namedtuple
1818
from platform import python_implementation
19-
from unittest import mock
19+
from unittest import mock, skipIf
2020

2121
from opentelemetry.sdk.metrics import MeterProvider
2222
from opentelemetry.sdk.metrics.export import InMemoryMetricReader
@@ -97,7 +97,6 @@ def test_system_metrics_instrument(self):
9797
for scope_metrics in resource_metrics.scope_metrics:
9898
for metric in scope_metrics.metrics:
9999
metric_names.append(metric.name)
100-
self.assertEqual(len(metric_names), 21)
101100

102101
observer_names = [
103102
"system.cpu.time",
@@ -117,11 +116,16 @@ def test_system_metrics_instrument(self):
117116
"system.thread_count",
118117
f"process.runtime.{self.implementation}.memory",
119118
f"process.runtime.{self.implementation}.cpu_time",
120-
f"process.runtime.{self.implementation}.gc_count",
121119
f"process.runtime.{self.implementation}.thread_count",
122120
f"process.runtime.{self.implementation}.context_switches",
123121
f"process.runtime.{self.implementation}.cpu.utilization",
124122
]
123+
124+
if self.implementation == "pypy":
125+
self.assertEqual(len(metric_names), 20)
126+
else:
127+
self.assertEqual(len(metric_names), 21)
128+
observer_names.append(f"process.runtime.{self.implementation}.gc_count",)
125129

126130
for observer in metric_names:
127131
self.assertIn(observer, observer_names)
@@ -131,11 +135,13 @@ def test_runtime_metrics_instrument(self):
131135
runtime_config = {
132136
"process.runtime.memory": ["rss", "vms"],
133137
"process.runtime.cpu.time": ["user", "system"],
134-
"process.runtime.gc_count": None,
135138
"process.runtime.thread_count": None,
136139
"process.runtime.cpu.utilization": None,
137140
"process.runtime.context_switches": ["involuntary", "voluntary"],
138141
}
142+
143+
if self.implementation != "pypy":
144+
runtime_config["process.runtime.gc_count"] = None
139145

140146
reader = InMemoryMetricReader()
141147
meter_provider = MeterProvider(metric_readers=[reader])
@@ -147,17 +153,21 @@ def test_runtime_metrics_instrument(self):
147153
for scope_metrics in resource_metrics.scope_metrics:
148154
for metric in scope_metrics.metrics:
149155
metric_names.append(metric.name)
150-
self.assertEqual(len(metric_names), 6)
151156

152157
observer_names = [
153158
f"process.runtime.{self.implementation}.memory",
154159
f"process.runtime.{self.implementation}.cpu_time",
155-
f"process.runtime.{self.implementation}.gc_count",
156160
f"process.runtime.{self.implementation}.thread_count",
157161
f"process.runtime.{self.implementation}.context_switches",
158162
f"process.runtime.{self.implementation}.cpu.utilization",
159163
]
160164

165+
if self.implementation == "pypy":
166+
self.assertEqual(len(metric_names), 5)
167+
else:
168+
self.assertEqual(len(metric_names), 6)
169+
observer_names.append(f"process.runtime.{self.implementation}.gc_count")
170+
161171
for observer in metric_names:
162172
self.assertIn(observer, observer_names)
163173
observer_names.remove(observer)
@@ -781,6 +791,7 @@ def test_runtime_cpu_time(self, mock_process_cpu_times):
781791
)
782792

783793
@mock.patch("gc.get_count")
794+
@skipIf(python_implementation().lower() == "pypy", "not supported for pypy")
784795
def test_runtime_get_count(self, mock_gc_get_count):
785796
mock_gc_get_count.configure_mock(**{"return_value": (1, 2, 3)})
786797

tox.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ envlist =
194194

195195
; opentelemetry-instrumentation-system-metrics
196196
py3{6,7,8,9,10,11}-test-instrumentation-system-metrics
197-
; instrumentation-system-metrics intentionally excluded from pypy3
197+
pypy3-test-instrumentation-system-metrics
198198

199199
; opentelemetry-instrumentation-tornado
200200
py3{7,8,9,10,11}-test-instrumentation-tornado

0 commit comments

Comments
 (0)