Skip to content

Commit

Permalink
Add capture to open file descriptors in system metrics instrumentation (
Browse files Browse the repository at this point in the history
#2652)

* add capture to open file descriptors in system metrics instrumentation

* fix meter type

* fix pypy test

* add changelog entry

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

Co-authored-by: Leighton Chen <lechen@microsoft.com>

* remove runtiem prefix

* update tests

---------

Co-authored-by: Leighton Chen <lechen@microsoft.com>
  • Loading branch information
emdneto and lzchen committed Jul 5, 2024
1 parent 75faaad commit c4c9b6b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#2631](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2631))
- `opentelemetry-instrumentation-system-metrics` Permit to use psutil 6.0+.
([#2630](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2630))
- `opentelemetry-instrumentation-system-metrics` Add support for capture open file descriptors
([#2652](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2652))

### Breaking changes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
"process.runtime.thread_count": None,
"process.runtime.cpu.utilization": None,
"process.runtime.context_switches": ["involuntary", "voluntary"],
"process.open_file_descriptor.count": None,
}

if sys.platform == "darwin":
Expand Down Expand Up @@ -169,6 +170,7 @@ def __init__(
self._runtime_thread_count_labels = self._labels.copy()
self._runtime_cpu_utilization_labels = self._labels.copy()
self._runtime_context_switches_labels = self._labels.copy()
self._open_file_descriptor_count_labels = self._labels.copy()

def instrumentation_dependencies(self) -> Collection[str]:
return _instruments
Expand Down Expand Up @@ -395,9 +397,25 @@ def _instrument(self, **kwargs):
unit="switches",
)

if "process.open_file_descriptor.count" in self._config:
self._meter.create_observable_up_down_counter(
name="process.open_file_descriptor.count",
callbacks=[self._get_open_file_descriptors],
description="Number of file descriptors in use by the process.",
)

def _uninstrument(self, **__):
pass

def _get_open_file_descriptors(
self, options: CallbackOptions
) -> Iterable[Observation]:
"""Observer callback for Number of file descriptors in use by the process"""
yield Observation(
self._proc.num_fds(),
self._open_file_descriptor_count_labels.copy(),
)

def _get_system_cpu_time(
self, options: CallbackOptions
) -> Iterable[Observation]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,13 @@ def test_system_metrics_instrument(self):
f"process.runtime.{self.implementation}.thread_count",
f"process.runtime.{self.implementation}.context_switches",
f"process.runtime.{self.implementation}.cpu.utilization",
"process.open_file_descriptor.count",
]

if self.implementation == "pypy":
self.assertEqual(len(metric_names), 20)
else:
self.assertEqual(len(metric_names), 21)
else:
self.assertEqual(len(metric_names), 22)
observer_names.append(
f"process.runtime.{self.implementation}.gc_count",
)
Expand Down Expand Up @@ -842,3 +843,14 @@ def test_runtime_cpu_percent(self, mock_process_cpu_percent):
self._test_metrics(
f"process.runtime.{self.implementation}.cpu.utilization", expected
)

@mock.patch("psutil.Process.num_fds")
def test_open_file_descriptor_count(self, mock_process_num_fds):
mock_process_num_fds.configure_mock(**{"return_value": 3})

expected = [_SystemMetricsResult({}, 3)]
self._test_metrics(
"process.open_file_descriptor.count",
expected,
)
mock_process_num_fds.assert_called()

0 comments on commit c4c9b6b

Please sign in to comment.