Skip to content

Commit

Permalink
Added support for pypy3 system metrics (#2062)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
rahulhacker and SuryanarayanaPeri authored Nov 16, 2023
1 parent b6d77f1 commit 6f6c28d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@

import gc
import os
import logging
import threading
from platform import python_implementation
from typing import Collection, Dict, Iterable, List, Optional
Expand All @@ -91,6 +92,9 @@
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"],
Expand Down Expand Up @@ -352,12 +356,18 @@ def _instrument(self, **kwargs):
)

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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -97,7 +97,6 @@ def test_system_metrics_instrument(self):
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",
Expand All @@ -117,11 +116,16 @@ def test_system_metrics_instrument(self):
"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)
Expand All @@ -131,11 +135,13 @@ def test_runtime_metrics_instrument(self):
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])
Expand All @@ -147,17 +153,21 @@ def test_runtime_metrics_instrument(self):
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)
Expand Down Expand Up @@ -781,6 +791,7 @@ def test_runtime_cpu_time(self, mock_process_cpu_times):
)

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

Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6f6c28d

Please sign in to comment.