Added support for pypy3 system metrics (#2062)

* Added support for pypy3 system metrics

Co-authored-by: Suryanarayana Peri <suryanarayana.peri@fmr.com>
Signed-off-by: Rahul Kumar <Rahul.Kumar@fmr.com>

* Change in if condition to log warn message for pypy
Co-authored-by: Suryanarayana Peri <suryanarayana.peri@fmr.com>

Signed-off-by: Rahul Kumar <Rahul.Kumar@fmr.com>

* Added logger to else block if pypy
Co-authored-by: Suryanarayana Peri <suryanarayana.peri@fmr.com>

Signed-off-by: Rahul Kumar <Rahul.Kumar@fmr.com>

* Reverting the changes
Co-authored-by: Suryanarayana Peri <suryanarayana.peri@fmr.com>

Signed-off-by: Rahul Kumar <Rahul.Kumar@fmr.com>

* Changes requested by external reviewer
Co-authored-by: Suryanarayana Peri <suryanarayana.peri@fmr.com>

Signed-off-by: Rahul Kumar <Rahul.Kumar@fmr.com>

---------

Signed-off-by: Rahul Kumar <Rahul.Kumar@fmr.com>
Co-authored-by: Suryanarayana Peri <suryanarayana.peri@fmr.com>
This commit is contained in:
Rahul Kumar
2023-11-17 00:48:51 +05:30
committed by GitHub
parent b6d77f1146
commit 6f6c28da45
4 changed files with 37 additions and 13 deletions

View File

@ -1394,6 +1394,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `opentelemetry-ext-wsgi` Updates for core library changes
- `opentelemetry-ext-http-requests` Updates for core library changes
- `Added support for PyPy3` Initial release
## [#1033](https://github.com/open-telemetryopentelemetry-python-contrib/issues/1033)
## Version 0.1a0 (2019-09-30)
### Added

View File

@ -77,6 +77,7 @@ API
import gc
import os
import logging
import threading
from platform import python_implementation
from typing import Collection, Dict, Iterable, List, Optional
@ -91,6 +92,9 @@ from opentelemetry.instrumentation.system_metrics.version import __version__
from opentelemetry.metrics import CallbackOptions, Observation, get_meter
from opentelemetry.sdk.util import get_dict_as_key
_logger = logging.getLogger(__name__)
_DEFAULT_CONFIG = {
"system.cpu.time": ["idle", "user", "system", "irq"],
"system.cpu.utilization": ["idle", "user", "system", "irq"],
@ -352,12 +356,18 @@ class SystemMetricsInstrumentor(BaseInstrumentor):
)
if "process.runtime.gc_count" in self._config:
self._meter.create_observable_counter(
name=f"process.runtime.{self._python_implementation}.gc_count",
callbacks=[self._get_runtime_gc_count],
description=f"Runtime {self._python_implementation} GC count",
unit="bytes",
)
if self._python_implementation == "pypy":
_logger.warning(
"The process.runtime.gc_count metric won't be collected because the interpreter is PyPy"
)
else:
self._meter.create_observable_counter(
name=f"process.runtime.{self._python_implementation}.gc_count",
callbacks=[self._get_runtime_gc_count],
description=f"Runtime {self._python_implementation} GC count",
unit="bytes",
)
if "process.runtime.thread_count" in self._config:
self._meter.create_observable_up_down_counter(

View File

@ -16,7 +16,7 @@
from collections import namedtuple
from platform import python_implementation
from unittest import mock
from unittest import mock, skipIf
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import InMemoryMetricReader
@ -97,7 +97,6 @@ class TestSystemMetrics(TestBase):
for scope_metrics in resource_metrics.scope_metrics:
for metric in scope_metrics.metrics:
metric_names.append(metric.name)
self.assertEqual(len(metric_names), 21)
observer_names = [
"system.cpu.time",
@ -117,11 +116,16 @@ class TestSystemMetrics(TestBase):
"system.thread_count",
f"process.runtime.{self.implementation}.memory",
f"process.runtime.{self.implementation}.cpu_time",
f"process.runtime.{self.implementation}.gc_count",
f"process.runtime.{self.implementation}.thread_count",
f"process.runtime.{self.implementation}.context_switches",
f"process.runtime.{self.implementation}.cpu.utilization",
]
if self.implementation == "pypy":
self.assertEqual(len(metric_names), 20)
else:
self.assertEqual(len(metric_names), 21)
observer_names.append(f"process.runtime.{self.implementation}.gc_count",)
for observer in metric_names:
self.assertIn(observer, observer_names)
@ -131,11 +135,13 @@ class TestSystemMetrics(TestBase):
runtime_config = {
"process.runtime.memory": ["rss", "vms"],
"process.runtime.cpu.time": ["user", "system"],
"process.runtime.gc_count": None,
"process.runtime.thread_count": None,
"process.runtime.cpu.utilization": None,
"process.runtime.context_switches": ["involuntary", "voluntary"],
}
if self.implementation != "pypy":
runtime_config["process.runtime.gc_count"] = None
reader = InMemoryMetricReader()
meter_provider = MeterProvider(metric_readers=[reader])
@ -147,17 +153,21 @@ class TestSystemMetrics(TestBase):
for scope_metrics in resource_metrics.scope_metrics:
for metric in scope_metrics.metrics:
metric_names.append(metric.name)
self.assertEqual(len(metric_names), 6)
observer_names = [
f"process.runtime.{self.implementation}.memory",
f"process.runtime.{self.implementation}.cpu_time",
f"process.runtime.{self.implementation}.gc_count",
f"process.runtime.{self.implementation}.thread_count",
f"process.runtime.{self.implementation}.context_switches",
f"process.runtime.{self.implementation}.cpu.utilization",
]
if self.implementation == "pypy":
self.assertEqual(len(metric_names), 5)
else:
self.assertEqual(len(metric_names), 6)
observer_names.append(f"process.runtime.{self.implementation}.gc_count")
for observer in metric_names:
self.assertIn(observer, observer_names)
observer_names.remove(observer)
@ -781,6 +791,7 @@ class TestSystemMetrics(TestBase):
)
@mock.patch("gc.get_count")
@skipIf(python_implementation().lower() == "pypy", "not supported for pypy")
def test_runtime_get_count(self, mock_gc_get_count):
mock_gc_get_count.configure_mock(**{"return_value": (1, 2, 3)})

View File

@ -194,7 +194,7 @@ envlist =
; opentelemetry-instrumentation-system-metrics
py3{6,7,8,9,10,11}-test-instrumentation-system-metrics
; instrumentation-system-metrics intentionally excluded from pypy3
pypy3-test-instrumentation-system-metrics
; opentelemetry-instrumentation-tornado
py3{7,8,9,10,11}-test-instrumentation-tornado