Skip to content

Commit fa9436b

Browse files
lixirAlexey Sergeev
andauthored
Add get_label_values function in prometheus_connect.py. (#235)
* Add get_label_values function in prometheus_connect.py. * Improving the all_metrics function via get_label_values. * Added tests for get_label_values(). Co-authored-by: Alexey Sergeev <alexey.sergeev@nordigy.ru>
1 parent f561caf commit fa9436b

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

prometheus_api_client/prometheus_connect.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,21 +91,36 @@ def all_metrics(self, params: dict = None):
9191
(RequestException) Raises an exception in case of a connection error
9292
(PrometheusApiClientException) Raises in case of non 200 response status code
9393
"""
94+
self._all_metrics = self.get_label_values(label_name="__name__", params=params)
95+
return self._all_metrics
96+
97+
def get_label_values(self, label_name: str, params: dict = None):
98+
"""
99+
Get a list of all values for the label.
100+
101+
:param label_name: (str) The name of the label for which you want to get all the values.
102+
:param params: (dict) Optional dictionary containing GET parameters to be
103+
sent along with the API request, such as "time"
104+
:returns: (list) A list of names for the label from the specified prometheus host
105+
:raises:
106+
(RequestException) Raises an exception in case of a connection error
107+
(PrometheusApiClientException) Raises in case of non 200 response status code
108+
"""
94109
params = params or {}
95110
response = self._session.get(
96-
"{0}/api/v1/label/__name__/values".format(self.url),
111+
"{0}/api/v1/label/{1}/values".format(self.url, label_name),
97112
verify=self.ssl_verification,
98113
headers=self.headers,
99114
params=params,
100115
)
101116

102117
if response.status_code == 200:
103-
self._all_metrics = response.json()["data"]
118+
labels = response.json()["data"]
104119
else:
105120
raise PrometheusApiClientException(
106121
"HTTP Status Code {} ({!r})".format(response.status_code, response.content)
107122
)
108-
return self._all_metrics
123+
return labels
109124

110125
def get_current_metric_value(
111126
self, metric_name: str, label_config: dict = None, params: dict = None

tests/test_prometheus_connect.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ def test_metrics_list(self):
2323
"""Check if setup was done correctly."""
2424
metrics_list = self.pc.all_metrics()
2525
self.assertTrue(len(metrics_list) > 0, "no metrics received from prometheus")
26+
# Checking that the results of all_metrics() and get_label_values("__name__") are the same.
27+
self.assertEqual(metrics_list, self.pc.get_label_values("__name__"))
28+
# Check for the "job" label.
29+
label_values = self.pc.get_label_values("job")
30+
self.assertTrue(len(label_values) > 0, "no metrics received from prometheus")
2631

2732
def test_get_metric_range_data(self): # noqa D102
2833
start_time = datetime.now() - timedelta(minutes=10)
@@ -159,11 +164,20 @@ def test_unauthorized(self): # noqa D102
159164
self.pc.all_metrics()
160165
self.assertEqual("HTTP Status Code 403 (b'Unauthorized')", str(exc.exception))
161166

167+
with self.mock_response("Unauthorized", status_code=403):
168+
with self.assertRaises(PrometheusApiClientException) as exc:
169+
self.pc.get_label_values("label_name")
170+
self.assertEqual("HTTP Status Code 403 (b'Unauthorized')", str(exc.exception))
171+
162172
def test_broken_responses(self): # noqa D102
163173
with self.assertRaises(PrometheusApiClientException) as exc:
164174
self.pc.all_metrics()
165175
self.assertEqual("HTTP Status Code 403 (b'BOOM!')", str(exc.exception))
166176

177+
with self.assertRaises(PrometheusApiClientException) as exc:
178+
self.pc.get_label_values("label_name")
179+
self.assertEqual("HTTP Status Code 403 (b'BOOM!')", str(exc.exception))
180+
167181
with self.assertRaises(PrometheusApiClientException) as exc:
168182
self.pc.get_current_metric_value("metric")
169183
self.assertEqual("HTTP Status Code 403 (b'BOOM!')", str(exc.exception))
@@ -196,3 +210,12 @@ def test_all_metrics_method(self): # noqa D102
196210
self.assertEqual(handler.call_count, 1)
197211
request = handler.requests[0]
198212
self.assertEqual(request.path_url, "/api/v1/label/__name__/values")
213+
214+
def test_get_label_values_method(self): # noqa D102
215+
all_metrics_payload = {"status": "success", "data": ["value1", "value2"]}
216+
217+
with self.mock_response(all_metrics_payload) as handler:
218+
self.assertTrue(len(self.pc.get_label_values("label_name")))
219+
self.assertEqual(handler.call_count, 1)
220+
request = handler.requests[0]
221+
self.assertEqual(request.path_url, "/api/v1/label/label_name/values")

0 commit comments

Comments
 (0)