Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add new process metrics #1948

Merged
merged 10 commits into from
Oct 9, 2023
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Added
- `opentelemetry-instrumentation-system-metrics` Add support for collecting process metrics
([#1948](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1948))

## Version 1.20.0/0.41b0 (2023-09-01)

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@
"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"],
}


Expand Down Expand Up @@ -150,6 +153,11 @@ def __init__(
self._runtime_memory_labels = self._labels.copy()
self._runtime_cpu_time_labels = self._labels.copy()
self._runtime_gc_count_labels = self._labels.copy()
self._runtime_thread_count_labels = self._labels.copy()

self._runtime_cpu_utilization_labels = self._labels.copy()

self._runtime_context_switches_labels = self._labels.copy()

def instrumentation_dependencies(self) -> Collection[str]:
return _instruments
Expand Down Expand Up @@ -347,6 +355,29 @@ def _instrument(self, **kwargs):
unit="bytes",
)

if "process.runtime.thread_count" in self._config:
self._meter.create_observable_gauge(
lzchen marked this conversation as resolved.
Show resolved Hide resolved
name="process.runtime.thread_count",
bourbonkk marked this conversation as resolved.
Show resolved Hide resolved
callbacks=[self._get_runtime_thread_count],
description="Runtime active threads count",
)

if "process.runtime.cpu.utilization" in self._config:
self._meter.create_observable_gauge(
name="process.runtime.cpu.utilization",
callbacks=[self._get_runtime_cpu_utilization],
description="Runtime CPU utilization",
unit="1",
)

if "process.runtime.context.switches" in self._config:
self._meter.create_observable_counter(
name="process.runtime.context.switches",
callbacks=[self._get_runtime_context_switches],
description="Runtime context switches",
unit="switches",
)

def _uninstrument(self, **__):
pass

Expand Down Expand Up @@ -646,3 +677,34 @@ def _get_runtime_gc_count(
for index, count in enumerate(gc.get_count()):
self._runtime_gc_count_labels["count"] = str(index)
yield Observation(count, self._runtime_gc_count_labels.copy())

def _get_runtime_thread_count(
self, options: CallbackOptions
) -> Iterable[Observation]:
"""Observer callback for runtime active thread count"""
yield Observation(
self._proc.num_threads(), self._runtime_thread_count_labels.copy()
)

def _get_runtime_cpu_utilization(
self, options: CallbackOptions
) -> Iterable[Observation]:
"""Observer callback for runtime CPU utilization"""
proc_cpu_percent = self._proc.cpu_percent()
lzchen marked this conversation as resolved.
Show resolved Hide resolved
yield Observation(
proc_cpu_percent,
self._runtime_cpu_utilization_labels.copy(),
)

def _get_runtime_context_switches(
self, options: CallbackOptions
) -> Iterable[Observation]:
"""Observer callback for runtime context switches"""
ctx_switches = self._proc.num_ctx_switches()
for metric in self._config["process.runtime.context.switches"]:
if hasattr(ctx_switches, metric):
self._runtime_context_switches_labels["type"] = metric
yield Observation(
getattr(ctx_switches, metric),
self._runtime_context_switches_labels.copy(),
)
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ 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), 18)
self.assertEqual(len(metric_names), 21)

observer_names = [
"system.cpu.time",
Expand All @@ -117,6 +117,9 @@ def test_system_metrics_instrument(self):
f"process.runtime.{self.implementation}.memory",
f"process.runtime.{self.implementation}.cpu_time",
f"process.runtime.{self.implementation}.gc_count",
"process.runtime.thread_count",
"process.runtime.context.switches",
"process.runtime.cpu.utilization",
]

for observer in metric_names:
Expand All @@ -128,6 +131,9 @@ def test_runtime_metrics_instrument(self):
"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"],
}

reader = InMemoryMetricReader()
Expand All @@ -140,12 +146,15 @@ 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), 3)
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",
"process.runtime.thread_count",
"process.runtime.context.switches",
"process.runtime.cpu.utilization",
]

for observer in metric_names:
Expand Down Expand Up @@ -782,3 +791,53 @@ def test_runtime_get_count(self, mock_gc_get_count):
self._test_metrics(
f"process.runtime.{self.implementation}.gc_count", expected
)

@mock.patch("psutil.Process.num_ctx_switches")
def test_runtime_context_switches(self, mock_process_num_ctx_switches):
PCtxSwitches = namedtuple("PCtxSwitches", ["voluntary", "involuntary"])

mock_process_num_ctx_switches.configure_mock(
**{"return_value": PCtxSwitches(voluntary=1, involuntary=2)}
)

expected = [
_SystemMetricsResult({"type": "voluntary"}, 1),
_SystemMetricsResult({"type": "involuntary"}, 2),
]
self._test_metrics(
"process.runtime.context.switches", expected
)

@mock.patch("psutil.Process.num_threads")
def test_runtime_thread_num(self, mock_process_thread_num):
mock_process_thread_num.configure_mock(**{"return_value": 42})

expected = [_SystemMetricsResult({}, 42)]
self._test_metrics(
"process.runtime.thread_count", expected
)

@mock.patch("psutil.Process.cpu_percent")
def test_runtime_cpu_percent(self, mock_process_cpu_percent):
mock_process_cpu_percent.configure_mock(**{"return_value": 42})

expected = [_SystemMetricsResult({}, 42)]
self._test_metrics(
"process.runtime.cpu.utilization", expected
)

@mock.patch("psutil.Process.num_ctx_switches")
def test_runtime_context_switches(self, mock_process_num_ctx_switches):
PCtxSwitches = namedtuple("PCtxSwitches", ["voluntary", "involuntary"])

mock_process_num_ctx_switches.configure_mock(
**{"return_value": PCtxSwitches(voluntary=1, involuntary=2)}
)

expected = [
_SystemMetricsResult({"type": "voluntary"}, 1),
_SystemMetricsResult({"type": "involuntary"}, 2),
]
self._test_metrics(
"process.runtime.context.switches", expected
)
Loading