Skip to content

Conversation

dabico
Copy link

@dabico dabico commented Sep 7, 2025

Description

Changes:

  • Default and user-specified instances of configuration dictionaries are now deeply copied when passed to SystemMetricsInstrumentor.
  • On macOS, if the user-specified config contains system.network.connections, then the metric will be removed and a warning is logged.
  • On Linux, if the user-specified config contains system.swap.usage or system.swap.utilization, the sin and sout states will be removed from both metrics if /proc/vmstat is not found, and a warning is logged.
  • Any RuntimeWarning emitted by psutil is (narrowly) suppressed whenever psutil.swap_memory is called in gauge callbacks.

Fixes #3740

Type of change

Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

Added a dedicated test for this functionality:

instrumentation/opentelemetry-instrumentation-system-metrics/tests/test_system_metrics.py::TestSystemMetrics::test_system_swap_states_removed_when_vmstat_missing

Ran it against several Python versions as follows:

tox -e py39-test-instrumentation-system-metrics
tox -e py310-test-instrumentation-system-metrics
tox -e py311-test-instrumentation-system-metrics
tox -e py312-test-instrumentation-system-metrics
tox -e py313-test-instrumentation-system-metrics

Note that it's set to only run on Linux.

Does This PR Require a Core Repo Change?

No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

Copy link

linux-foundation-easycla bot commented Sep 7, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@dabico
Copy link
Author

dabico commented Sep 7, 2025

Spending some more time with the code, I came to the realisation I over-compensated with the metric removals. The error message states that sin and sout can't be computed, but these two states aren't part of the _DEFAULT configuration. A more desirable course of action would be to remove these two states from the passed config and issue a log warning if the trigger conditions are met. Will rework this.

@dabico dabico force-pushed the swap branch 2 times, most recently from 64a6eb0 to 82bf62b Compare September 8, 2025 21:21
@dabico dabico changed the title Remove system.swap.* metrics if they can not be computed Remove sin & sout states from system.swap.* if they can not be computed Sep 8, 2025
@xrmx xrmx moved this to Ready for review in @xrmx's Python PR digest Sep 9, 2025

@staticmethod
@contextmanager
def _suppress_psutil_swap_warnings():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please elaborate in which cases we are going to need this?

Copy link
Author

@dabico dabico Oct 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

psutil.swap_memory will always produce a warning when invoked from gauge callbacks in conditions #3740 describes (in spite of the states specified in the config). As a result, the output stream will repeatedly contain:

/home/app/.local/lib/python3.13/site-packages/psutil/__init__.py:2036: RuntimeWarning: 'sin' and 'sout' swap memory stats couldn't be determined and were set to 0 ([Errno 2] No such file or directory: '/proc/vmstat')
  return _psplatform.swap_memory()

On each callback. What I tried to do here was silence this particular warning and only log a warning message to the user once (when the instrumentor is initialized).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, so you are removing sin and sout from the config to not report them but psutil will get a warning because it tries to read them anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Ready for review

Development

Successfully merging this pull request may close these issues.

Some system.swap.* metric states are not available in sandbox runtimes

2 participants