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:
@@ -94,9 +94,11 @@ class Base(rest.RestController):
|
|||||||
return promQLRbac.append_rbac_labels('')
|
return promQLRbac.append_rbac_labels('')
|
||||||
|
|
||||||
# Apply RBAC modification to each match
|
# Apply RBAC modification to each match
|
||||||
|
metric_names = self.prometheus_client.label_values("__name__")
|
||||||
modified_matches = []
|
modified_matches = []
|
||||||
for match in 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
|
return modified_matches
|
||||||
|
|
||||||
|
@@ -166,11 +166,13 @@ class TestCoreEndpointsAsUser(base.TestCase):
|
|||||||
expected_params = {'match[]': expected_matches}
|
expected_params = {'match[]': expected_matches}
|
||||||
|
|
||||||
with (
|
with (
|
||||||
|
mock.patch.object(prometheus_client.PrometheusAPIClient,
|
||||||
|
'label_values', return_value=['metric_name']),
|
||||||
mock.patch.object(prometheus_client.PrometheusAPIClient, '_get',
|
mock.patch.object(prometheus_client.PrometheusAPIClient, '_get',
|
||||||
return_value=returned_from_prometheus
|
return_value=returned_from_prometheus
|
||||||
) as get_mock,
|
) as get_mock,
|
||||||
mock.patch.object(rbac.PromQLRbac, 'modify_query',
|
mock.patch.object(rbac.PromQLRbac, 'modify_query',
|
||||||
side_effect=lambda x:
|
side_effect=lambda x, metric_names:
|
||||||
expected_matches[matches.index(x)]
|
expected_matches[matches.index(x)]
|
||||||
) as rbac_mock
|
) as rbac_mock
|
||||||
):
|
):
|
||||||
@@ -184,7 +186,7 @@ class TestCoreEndpointsAsUser(base.TestCase):
|
|||||||
f'label/{label_name}/values', expected_params
|
f'label/{label_name}/values', expected_params
|
||||||
)
|
)
|
||||||
for match in matches:
|
for match in matches:
|
||||||
rbac_mock.assert_any_call(match)
|
rbac_mock.assert_any_call(match, metric_names=['metric_name'])
|
||||||
|
|
||||||
def test_labels(self):
|
def test_labels(self):
|
||||||
expected_status_code = 200
|
expected_status_code = 200
|
||||||
@@ -240,11 +242,13 @@ class TestCoreEndpointsAsUser(base.TestCase):
|
|||||||
expected_params = {'match[]': expected_matches}
|
expected_params = {'match[]': expected_matches}
|
||||||
|
|
||||||
with (
|
with (
|
||||||
|
mock.patch.object(prometheus_client.PrometheusAPIClient,
|
||||||
|
'label_values', return_value=['metric_name']),
|
||||||
mock.patch.object(prometheus_client.PrometheusAPIClient, '_get',
|
mock.patch.object(prometheus_client.PrometheusAPIClient, '_get',
|
||||||
return_value=returned_from_prometheus
|
return_value=returned_from_prometheus
|
||||||
) as get_mock,
|
) as get_mock,
|
||||||
mock.patch.object(rbac.PromQLRbac, 'modify_query',
|
mock.patch.object(rbac.PromQLRbac, 'modify_query',
|
||||||
side_effect=lambda x:
|
side_effect=lambda x, metric_names:
|
||||||
expected_matches[matches.index(x)]
|
expected_matches[matches.index(x)]
|
||||||
) as rbac_mock
|
) as rbac_mock
|
||||||
):
|
):
|
||||||
@@ -258,7 +262,7 @@ class TestCoreEndpointsAsUser(base.TestCase):
|
|||||||
'labels', expected_params
|
'labels', expected_params
|
||||||
)
|
)
|
||||||
for match in matches:
|
for match in matches:
|
||||||
rbac_mock.assert_any_call(match)
|
rbac_mock.assert_any_call(match, metric_names=['metric_name'])
|
||||||
|
|
||||||
def test_query(self):
|
def test_query(self):
|
||||||
expected_status_code = 200
|
expected_status_code = 200
|
||||||
@@ -342,11 +346,13 @@ class TestCoreEndpointsAsUser(base.TestCase):
|
|||||||
modified_params = {'match[]': modified_matches}
|
modified_params = {'match[]': modified_matches}
|
||||||
|
|
||||||
with (
|
with (
|
||||||
|
mock.patch.object(prometheus_client.PrometheusAPIClient,
|
||||||
|
'label_values', return_value=['metric_name']),
|
||||||
mock.patch.object(prometheus_client.PrometheusAPIClient, '_get',
|
mock.patch.object(prometheus_client.PrometheusAPIClient, '_get',
|
||||||
return_value=returned_from_prometheus
|
return_value=returned_from_prometheus
|
||||||
) as get_mock,
|
) as get_mock,
|
||||||
mock.patch.object(rbac.PromQLRbac, 'modify_query',
|
mock.patch.object(rbac.PromQLRbac, 'modify_query',
|
||||||
side_effect=lambda x:
|
side_effect=lambda x, metric_names:
|
||||||
modified_matches[matches.index(x)]
|
modified_matches[matches.index(x)]
|
||||||
) as rbac_mock
|
) as rbac_mock
|
||||||
):
|
):
|
||||||
@@ -358,7 +364,7 @@ class TestCoreEndpointsAsUser(base.TestCase):
|
|||||||
self.assertEqual(expected_status_code, result.status_code)
|
self.assertEqual(expected_status_code, result.status_code)
|
||||||
get_mock.assert_called_once_with('series', modified_params)
|
get_mock.assert_called_once_with('series', modified_params)
|
||||||
for match in matches:
|
for match in matches:
|
||||||
rbac_mock.assert_any_call(match)
|
rbac_mock.assert_any_call(match, metric_names=['metric_name'])
|
||||||
|
|
||||||
def test_status(self):
|
def test_status(self):
|
||||||
expected_status_code = 403
|
expected_status_code = 403
|
||||||
|
@@ -15,4 +15,4 @@ pecan>=0.8.0 # BSD
|
|||||||
oslo.middleware>=3.22.0 # Apache-2.0
|
oslo.middleware>=3.22.0 # Apache-2.0
|
||||||
oslo.utils>=4.7.0 # Apache-2.0
|
oslo.utils>=4.7.0 # Apache-2.0
|
||||||
WSME>=0.12.1 # MIT
|
WSME>=0.12.1 # MIT
|
||||||
python-observabilityclient>=0.0.4 # Apache-2.0
|
python-observabilityclient>=1.1.0 # Apache-2.0
|
||||||
|
Reference in New Issue
Block a user