Skip to content

Commit 64a6eb0

Browse files
committed
Add a test case
1 parent eb3f5cc commit 64a6eb0

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

instrumentation/opentelemetry-instrumentation-system-metrics/tests/test_system_metrics.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
import sys
1818
from collections import namedtuple
1919
from platform import python_implementation
20-
from unittest import mock, skipIf
20+
from tempfile import TemporaryDirectory
21+
from unittest import mock, skipIf, skipUnless
2122

2223
from opentelemetry.instrumentation.system_metrics import (
2324
_DEFAULT_CONFIG,
@@ -415,6 +416,38 @@ def test_system_swap_utilization(self, mock_swap_memory):
415416
]
416417
self._test_metrics("system.swap.utilization", expected)
417418

419+
@skipUnless(sys.platform == "linux", "Linux only")
420+
def test_system_swap_states_removed_when_vmstat_missing(self):
421+
with (
422+
self.assertLogs(level="WARNING") as logwatcher,
423+
TemporaryDirectory() as tmpdir,
424+
mock.patch(
425+
target="psutil.PROCFS_PATH",
426+
new=tmpdir,
427+
create=True,
428+
),
429+
):
430+
runtime_config = {
431+
"system.swap.usage": ["free", "sin", "sout", "used"],
432+
"system.swap.utilization": ["free", "sin", "sout", "used"],
433+
}
434+
435+
runtime_metrics = SystemMetricsInstrumentor(config=runtime_config)
436+
runtime_metrics.instrument()
437+
438+
self.assertEqual(
439+
first=len(logwatcher.records),
440+
second=1,
441+
)
442+
self.assertListEqual(
443+
list1=runtime_metrics._config["system.swap.usage"],
444+
list2=["free", "used"],
445+
)
446+
self.assertListEqual(
447+
list1=runtime_metrics._config["system.swap.utilization"],
448+
list2=["free", "used"],
449+
)
450+
418451
@mock.patch("psutil.disk_io_counters")
419452
def test_system_disk_io(self, mock_disk_io_counters):
420453
DiskIO = namedtuple(

0 commit comments

Comments
 (0)