Reduce Prometheus requests for modify_query calls

Each call to modify_query will send a request to Prometheus to
determine all metric names saved in Prometheus unless we
provide the list with the modify_query call. We use the modify_query
in two places in Aetos, one is the query controller, the other one
is the process_matches function. In the query controller, we call
modify_query just once, so the code can stay as is. In process_matches
we call modify_query in a loop, so we send a request to Prometheus
to retrieve a list of metrics before the loop and provide the list
to modify_query to to avoid sending a request to Prometheus with each
iteration of the loop.

Change-Id: I5f5c82ce25033748c321dd836256094fbcf305ef
Signed-off-by: Jaromir Wysoglad <jwysogla@redhat.com>
This commit is contained in:
Jaromir Wysoglad
2025-08-13 15:33:50 -04:00
parent 918dcf4d14
commit e563b9e0dc
3 changed files with 16 additions and 8 deletions

View File

@@ -94,9 +94,11 @@ class Base(rest.RestController):
return promQLRbac.append_rbac_labels('')
# Apply RBAC modification to each match
metric_names = self.prometheus_client.label_values("__name__")
modified_matches = []
for match in matches:
modified_matches.append(promQLRbac.modify_query(match))
modified_matches.append(
promQLRbac.modify_query(match, metric_names=metric_names))
return modified_matches

View File

@@ -166,11 +166,13 @@ class TestCoreEndpointsAsUser(base.TestCase):
expected_params = {'match[]': expected_matches}
with (
mock.patch.object(prometheus_client.PrometheusAPIClient,
'label_values', return_value=['metric_name']),
mock.patch.object(prometheus_client.PrometheusAPIClient, '_get',
return_value=returned_from_prometheus
) as get_mock,
mock.patch.object(rbac.PromQLRbac, 'modify_query',
side_effect=lambda x:
side_effect=lambda x, metric_names:
expected_matches[matches.index(x)]
) as rbac_mock
):
@@ -184,7 +186,7 @@ class TestCoreEndpointsAsUser(base.TestCase):
f'label/{label_name}/values', expected_params
)
for match in matches:
rbac_mock.assert_any_call(match)
rbac_mock.assert_any_call(match, metric_names=['metric_name'])
def test_labels(self):
expected_status_code = 200
@@ -240,11 +242,13 @@ class TestCoreEndpointsAsUser(base.TestCase):
expected_params = {'match[]': expected_matches}
with (
mock.patch.object(prometheus_client.PrometheusAPIClient,
'label_values', return_value=['metric_name']),
mock.patch.object(prometheus_client.PrometheusAPIClient, '_get',
return_value=returned_from_prometheus
) as get_mock,
mock.patch.object(rbac.PromQLRbac, 'modify_query',
side_effect=lambda x:
side_effect=lambda x, metric_names:
expected_matches[matches.index(x)]
) as rbac_mock
):
@@ -258,7 +262,7 @@ class TestCoreEndpointsAsUser(base.TestCase):
'labels', expected_params
)
for match in matches:
rbac_mock.assert_any_call(match)
rbac_mock.assert_any_call(match, metric_names=['metric_name'])
def test_query(self):
expected_status_code = 200
@@ -342,11 +346,13 @@ class TestCoreEndpointsAsUser(base.TestCase):
modified_params = {'match[]': modified_matches}
with (
mock.patch.object(prometheus_client.PrometheusAPIClient,
'label_values', return_value=['metric_name']),
mock.patch.object(prometheus_client.PrometheusAPIClient, '_get',
return_value=returned_from_prometheus
) as get_mock,
mock.patch.object(rbac.PromQLRbac, 'modify_query',
side_effect=lambda x:
side_effect=lambda x, metric_names:
modified_matches[matches.index(x)]
) as rbac_mock
):
@@ -358,7 +364,7 @@ class TestCoreEndpointsAsUser(base.TestCase):
self.assertEqual(expected_status_code, result.status_code)
get_mock.assert_called_once_with('series', modified_params)
for match in matches:
rbac_mock.assert_any_call(match)
rbac_mock.assert_any_call(match, metric_names=['metric_name'])
def test_status(self):
expected_status_code = 403

View File

@@ -15,4 +15,4 @@ pecan>=0.8.0 # BSD
oslo.middleware>=3.22.0 # Apache-2.0
oslo.utils>=4.7.0 # Apache-2.0
WSME>=0.12.1 # MIT
python-observabilityclient>=0.0.4 # Apache-2.0
python-observabilityclient>=1.1.0 # Apache-2.0