Skip to content

Commit 12f86da

Browse files
committed
Suppress warnings related to psutil.swap_memory when performing observations
1 parent 28d837e commit 12f86da

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
@@ -664,7 +666,8 @@ def _get_system_swap_usage(
664666
self, options: CallbackOptions
665667
) -> Iterable[Observation]:
666668
"""Observer callback for swap usage"""
667-
system_swap = psutil.swap_memory()
669+
with self._suppress_psutil_swap_warnings():
670+
system_swap = psutil.swap_memory()
668671

669672
for metric in self._config["system.swap.usage"]:
670673
self._system_swap_usage_labels["state"] = metric
@@ -678,7 +681,8 @@ def _get_system_swap_utilization(
678681
self, options: CallbackOptions
679682
) -> Iterable[Observation]:
680683
"""Observer callback for swap utilization"""
681-
system_swap = psutil.swap_memory()
684+
with self._suppress_psutil_swap_warnings():
685+
system_swap = psutil.swap_memory()
682686

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

0 commit comments

Comments
 (0)