Skip to content

Commit 8fa3248

Browse files
committed
Added get_metric_value helper function
1 parent 3c0ce4a commit 8fa3248

2 files changed

Lines changed: 38 additions & 14 deletions

File tree

tests/test_path_rmdir.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import shutil
33

44
import pytest
5-
import requests
65

76
from event import Event, EventType, Process
7+
from utils import get_metric_value
88

99

1010
def get_inode_removed_count(fact_config):
@@ -17,18 +17,8 @@ def get_inode_removed_count(fact_config):
1717
Returns:
1818
The current value of host_scanner_scan{label="InodeRemoved"} metric.
1919
"""
20-
config, _ = fact_config
21-
response = requests.get(f'http://{config["endpoint"]["address"]}/metrics')
22-
assert response.status_code == 200
23-
24-
for line in response.text.split('\n'):
25-
if 'host_scanner_scan' in line and 'label="InodeRemoved"' in line:
26-
# Format: host_scanner_scan{label="InodeRemoved"} 42
27-
parts = line.split()
28-
if len(parts) >= 2:
29-
return int(parts[-1])
30-
31-
return 0
20+
value = get_metric_value(fact_config, "host_scanner_scan", {"label": "InodeRemoved"})
21+
return int(value) if value is not None else 0
3222

3323

3424
@pytest.mark.parametrize("dirname", [

tests/utils.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import os
22
import re
33

4+
import requests
5+
46

57
def join_path_with_filename(directory, filename):
68
"""
@@ -66,8 +68,40 @@ def rust_style_join(args):
6668
"""
6769
Concatenate arguments after quoting them. Each argument is separated
6870
by a single space.
69-
71+
7072
Args:
7173
args: The string to quote
7274
"""
7375
return ' '.join(rust_style_quote(arg) for arg in args)
76+
77+
78+
def get_metric_value(fact_config, metric_name, labels=None):
79+
"""
80+
Query Prometheus metrics endpoint to get the value of a metric.
81+
82+
Args:
83+
fact_config: The fact configuration tuple (config dict, config file path).
84+
metric_name: Name of the metric to query (e.g., "host_scanner_scan").
85+
labels: Optional dict of label filters (e.g., {"label": "InodeRemoved"}).
86+
87+
Returns:
88+
The metric value as a string if found, None otherwise.
89+
"""
90+
config, _ = fact_config
91+
response = requests.get(f'http://{config["endpoint"]["address"]}/metrics')
92+
assert response.status_code == 200
93+
94+
labels = labels or {}
95+
96+
for line in response.text.split('\n'):
97+
if metric_name not in line:
98+
continue
99+
100+
# Check if all label filters match
101+
if all(f'{k}="{v}"' in line for k, v in labels.items()):
102+
# Format: metric_name{label="value"} 42
103+
parts = line.split()
104+
if len(parts) >= 2:
105+
return parts[-1]
106+
107+
return None

0 commit comments

Comments
 (0)