Skip to content

Commit 8a0317b

Browse files
committed
Add a test case
1 parent 205db0c commit 8a0317b

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
# pylint: disable=protected-access,too-many-lines
1616

17+
import importlib.util
1718
import sys
1819
from collections import namedtuple
1920
from platform import python_implementation
@@ -1091,3 +1092,37 @@ def test_that_correct_config_is_read(self):
10911092
instrumentor.instrument(meter_provider=meter_provider)
10921093
meter_provider.force_flush()
10931094
instrumentor.uninstrument()
1095+
1096+
def test_linux_missing_vmstat_removes_swap_metrics(self):
1097+
with (
1098+
mock.patch(
1099+
target="psutil.LINUX",
1100+
new=True,
1101+
),
1102+
mock.patch(
1103+
target="psutil.PROCFS_PATH",
1104+
new="/bogus/path/to/procfs",
1105+
create=True,
1106+
),
1107+
):
1108+
# Import the module into an isolated, temporary module object under the patched context
1109+
spec = importlib.util.find_spec(
1110+
"opentelemetry.instrumentation.system_metrics"
1111+
)
1112+
assert (
1113+
spec and spec.origin
1114+
), "Could not find system_metrics module spec"
1115+
1116+
tmp_spec = importlib.util.spec_from_file_location(
1117+
name="tmp_system_metrics_for_test",
1118+
location=spec.origin,
1119+
)
1120+
module = importlib.util.module_from_spec(tmp_spec)
1121+
1122+
# Ensure relative imports inside the module resolve correctly
1123+
module.__package__ = "opentelemetry.instrumentation.system_metrics"
1124+
tmp_spec.loader.exec_module(module)
1125+
1126+
config = getattr(module, "_DEFAULT_CONFIG")
1127+
assert "system.swap.usage" not in config
1128+
assert "system.swap.utilization" not in config

0 commit comments

Comments
 (0)