Skip to content

Commit eb3f5cc

Browse files
committed
Suppress warnings related to psutil.swap_memory when performing observations
1 parent ca68348 commit eb3f5cc

File tree

1 file changed

+20
-2
lines changed
  • instrumentation/opentelemetry-instrumentation-system-metrics/src/opentelemetry/instrumentation/system_metrics

1 file changed

+20
-2
lines changed

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@
9999
import os
100100
import sys
101101
import threading
102+
import warnings
103+
from contextlib import contextmanager
102104
from copy import deepcopy
103105
from platform import python_implementation
104106
from typing import Any, Collection, Iterable
@@ -661,7 +663,8 @@ def _get_system_swap_usage(
661663
self, options: CallbackOptions
662664
) -> Iterable[Observation]:
663665
"""Observer callback for swap usage"""
664-
system_swap = psutil.swap_memory()
666+
with self._suppress_psutil_swap_warnings():
667+
system_swap = psutil.swap_memory()
665668

666669
for metric in self._config["system.swap.usage"]:
667670
self._system_swap_usage_labels["state"] = metric
@@ -675,7 +678,8 @@ def _get_system_swap_utilization(
675678
self, options: CallbackOptions
676679
) -> Iterable[Observation]:
677680
"""Observer callback for swap utilization"""
678-
system_swap = psutil.swap_memory()
681+
with self._suppress_psutil_swap_warnings():
682+
system_swap = psutil.swap_memory()
679683

680684
for metric in self._config["system.swap.utilization"]:
681685
if hasattr(system_swap, metric):
@@ -1034,3 +1038,17 @@ def _get_runtime_context_switches(
10341038
getattr(ctx_switches, metric),
10351039
self._runtime_context_switches_labels.copy(),
10361040
)
1041+
1042+
@staticmethod
1043+
@contextmanager
1044+
def _suppress_psutil_swap_warnings():
1045+
with warnings.catch_warnings():
1046+
warnings.filterwarnings(
1047+
action="ignore",
1048+
category=RuntimeWarning,
1049+
# language=regexp
1050+
message=r"^'sin' and 'sout' swap memory stats couldn't be determined and were set to 0",
1051+
# language=regexp
1052+
module=r"^psutil$",
1053+
)
1054+
yield

0 commit comments

Comments
 (0)