forked from 4n4nd/prometheus-api-client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprometheus_connect.py
701 lines (614 loc) · 27.4 KB
/
prometheus_connect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
"""A Class for collection of metrics from a Prometheus Host."""
from urllib.parse import urlparse
import bz2
import os
import json
import logging
import numpy
from datetime import datetime, timedelta
import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
from requests import Session
from .exceptions import PrometheusApiClientException
# set up logging
_LOGGER = logging.getLogger(__name__)
# In case of a connection failure try 2 more times
MAX_REQUEST_RETRIES = 3
# wait 1 second before retrying in case of an error
RETRY_BACKOFF_FACTOR = 1
# retry only on these status
RETRY_ON_STATUS = [408, 429, 500, 502, 503, 504]
class PrometheusConnect:
"""
A Class for collection of metrics from a Prometheus Host.
:param url: (str) url for the prometheus host
:param headers: (dict) A dictionary of http headers to be used to communicate with
the host. Example: {"Authorization": "bearer my_oauth_token_to_the_host"}
:param disable_ssl: (bool) If set to True, will disable ssl certificate verification
for the http requests made to the prometheus host
:param retry: (Retry) Retry adapter to retry on HTTP errors
:param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth. See python
requests library auth parameter for further explanation.
:param proxy: (Optional) Proxies dictionary to enable connection through proxy.
Example: {"http_proxy": "<ip_address/hostname:port>", "https_proxy": "<ip_address/hostname:port>"}
:param session (Optional) Custom requests.Session to enable complex HTTP configuration
:param timeout: (Optional) A timeout (in seconds) applied to all requests
"""
def __init__(
self,
url: str = "http://127.0.0.1:9090",
headers: dict = None,
disable_ssl: bool = False,
retry: Retry = None,
auth: tuple = None,
proxy: dict = None,
session: Session = None,
timeout: int = None,
):
"""Functions as a Constructor for the class PrometheusConnect."""
if url is None:
raise TypeError("missing url")
self.headers = headers
self.url = url
self.prometheus_host = urlparse(self.url).netloc
self._all_metrics = None
self._timeout = timeout
if retry is None:
retry = Retry(
total=MAX_REQUEST_RETRIES,
backoff_factor=RETRY_BACKOFF_FACTOR,
status_forcelist=RETRY_ON_STATUS,
)
self.auth = auth
if session is not None:
self._session = session
else:
self._session = requests.Session()
self._session.verify = not disable_ssl
if proxy is not None:
self._session.proxies = proxy
self._session.mount(self.url, HTTPAdapter(max_retries=retry))
def check_prometheus_connection(self, params: dict = None) -> bool:
"""
Check Promethus connection.
:param params: (dict) Optional dictionary containing parameters to be
sent along with the API request.
:returns: (bool) True if the endpoint can be reached, False if cannot be reached.
"""
response = self._session.get(
"{0}/".format(self.url),
verify=self._session.verify,
headers=self.headers,
params=params,
auth=self.auth,
cert=self._session.cert,
timeout=self._timeout,
)
return response.ok
def all_metrics(self, params: dict = None):
"""
Get the list of all the metrics that the prometheus host scrapes.
:param params: (dict) Optional dictionary containing GET parameters to be
sent along with the API request, such as "time"
:returns: (list) A list of names of all the metrics available from the
specified prometheus host
:raises:
(RequestException) Raises an exception in case of a connection error
(PrometheusApiClientException) Raises in case of non 200 response status code
"""
self._all_metrics = self.get_label_values(label_name="__name__", params=params)
return self._all_metrics
def get_label_names(self, params: dict = None):
"""
Get a list of all labels.
:param params: (dict) Optional dictionary containing GET parameters to be
sent along with the API request, such as "start", "end" or "match[]".
:returns: (list) A list of labels from the specified prometheus host
:raises:
(RequestException) Raises an exception in case of a connection error
(PrometheusApiClientException) Raises in case of non 200 response status code
"""
params = params or {}
response = self._session.get(
"{0}/api/v1/labels".format(self.url),
verify=self._session.verify,
headers=self.headers,
params=params,
auth=self.auth,
cert=self._session.cert,
timeout=self._timeout,
)
if response.status_code == 200:
labels = response.json()["data"]
else:
raise PrometheusApiClientException(
"HTTP Status Code {} ({!r})".format(response.status_code, response.content)
)
return labels
def get_label_values(self, label_name: str, params: dict = None):
"""
Get a list of all values for the label.
:param label_name: (str) The name of the label for which you want to get all the values.
:param params: (dict) Optional dictionary containing GET parameters to be
sent along with the API request, such as "time"
:returns: (list) A list of names for the label from the specified prometheus host
:raises:
(RequestException) Raises an exception in case of a connection error
(PrometheusApiClientException) Raises in case of non 200 response status code
"""
params = params or {}
response = self._session.get(
"{0}/api/v1/label/{1}/values".format(self.url, label_name),
verify=self._session.verify,
headers=self.headers,
params=params,
auth=self.auth,
cert=self._session.cert,
timeout=self._timeout,
)
if response.status_code == 200:
labels = response.json()["data"]
else:
raise PrometheusApiClientException(
"HTTP Status Code {} ({!r})".format(response.status_code, response.content)
)
return labels
def get_current_metric_value(
self, metric_name: str, label_config: dict = None, params: dict = None
):
r"""
Get the current metric value for the specified metric and label configuration.
:param metric_name: (str) The name of the metric
:param label_config: (dict) A dictionary that specifies metric labels and their
values
:param params: (dict) Optional dictionary containing GET parameters to be sent
along with the API request, such as "time"
:returns: (list) A list of current metric values for the specified metric
:raises:
(RequestException) Raises an exception in case of a connection error
(PrometheusApiClientException) Raises in case of non 200 response status code
Example Usage:
.. code-block:: python
prom = PrometheusConnect()
my_label_config = {'cluster': 'my_cluster_id', 'label_2': 'label_2_value'}
prom.get_current_metric_value(metric_name='up', label_config=my_label_config)
"""
params = params or {}
data = []
if label_config:
label_list = [str(key + "=" + "'" + label_config[key] + "'") for key in label_config]
query = metric_name + "{" + ",".join(label_list) + "}"
else:
query = metric_name
# using the query API to get raw data
response = self._session.get(
"{0}/api/v1/query".format(self.url),
params={**{"query": query}, **params},
verify=self._session.verify,
headers=self.headers,
auth=self.auth,
cert=self._session.cert,
timeout=self._timeout,
)
if response.status_code == 200:
data += response.json()["data"]["result"]
else:
raise PrometheusApiClientException(
"HTTP Status Code {} ({!r})".format(response.status_code, response.content)
)
return data
def get_metric_range_data(
self,
metric_name: str,
label_config: dict = None,
start_time: datetime = (datetime.now() - timedelta(minutes=10)),
end_time: datetime = datetime.now(),
chunk_size: timedelta = None,
store_locally: bool = False,
params: dict = None,
):
r"""
Get the current metric value for the specified metric and label configuration.
:param metric_name: (str) The name of the metric.
:param label_config: (dict) A dictionary specifying metric labels and their
values.
:param start_time: (datetime) A datetime object that specifies the metric range start time.
:param end_time: (datetime) A datetime object that specifies the metric range end time.
:param chunk_size: (timedelta) Duration of metric data downloaded in one request. For
example, setting it to timedelta(hours=3) will download 3 hours worth of data in each
request made to the prometheus host
:param store_locally: (bool) If set to True, will store data locally at,
`"./metrics/hostname/metric_date/name_time.json.bz2"`
:param params: (dict) Optional dictionary containing GET parameters to be
sent along with the API request, such as "time"
:return: (list) A list of metric data for the specified metric in the given time
range
:raises:
(RequestException) Raises an exception in case of a connection error
(PrometheusApiClientException) Raises in case of non 200 response status code
"""
params = params or {}
data = []
_LOGGER.debug("start_time: %s", start_time)
_LOGGER.debug("end_time: %s", end_time)
_LOGGER.debug("chunk_size: %s", chunk_size)
if not (isinstance(start_time, datetime) and isinstance(end_time, datetime)):
raise TypeError("start_time and end_time can only be of type datetime.datetime")
if not chunk_size:
chunk_size = end_time - start_time
if not isinstance(chunk_size, timedelta):
raise TypeError("chunk_size can only be of type datetime.timedelta")
start = round(start_time.timestamp())
end = round(end_time.timestamp())
if end_time < start_time:
raise ValueError("end_time must not be before start_time")
if (end_time - start_time).total_seconds() < chunk_size.total_seconds():
raise ValueError("specified chunk_size is too big")
chunk_seconds = round(chunk_size.total_seconds())
if label_config:
label_list = [str(key + "=" + "'" + label_config[key] + "'") for key in label_config]
query = metric_name + "{" + ",".join(label_list) + "}"
else:
query = metric_name
_LOGGER.debug("Prometheus Query: %s", query)
while start < end:
if start + chunk_seconds > end:
chunk_seconds = end - start
# using the query API to get raw data
response = self._session.get(
"{0}/api/v1/query".format(self.url),
params={
**{
"query": query + "[" + str(chunk_seconds) + "s" + "]",
"time": start + chunk_seconds,
},
**params,
},
verify=self._session.verify,
headers=self.headers,
auth=self.auth,
cert=self._session.cert,
timeout=self._timeout,
)
if response.status_code == 200:
data += response.json()["data"]["result"]
else:
raise PrometheusApiClientException(
"HTTP Status Code {} ({!r})".format(response.status_code, response.content)
)
if store_locally:
# store it locally
self._store_metric_values_local(
metric_name,
json.dumps(response.json()["data"]["result"]),
start + chunk_seconds,
)
start += chunk_seconds
return data
def _store_metric_values_local(self, metric_name, values, end_timestamp, compressed=False):
r"""
Store metrics on the local filesystem, optionally with bz2 compression.
:param metric_name: (str) the name of the metric being saved
:param values: (str) metric data in JSON string format
:param end_timestamp: (int) timestamp in any format understood by \
datetime.datetime.fromtimestamp()
:param compressed: (bool) whether or not to apply bz2 compression
:returns: (str) path to the saved metric file
"""
if not values:
_LOGGER.debug("No values for %s", metric_name)
return None
file_path = self._metric_filename(metric_name, end_timestamp)
if compressed:
payload = bz2.compress(str(values).encode("utf-8"))
file_path = file_path + ".bz2"
else:
payload = str(values).encode("utf-8")
os.makedirs(os.path.dirname(file_path), exist_ok=True)
with open(file_path, "wb") as file:
file.write(payload)
return file_path
def _metric_filename(self, metric_name: str, end_timestamp: int):
r"""
Add a timestamp to the filename before it is stored.
:param metric_name: (str) the name of the metric being saved
:param end_timestamp: (int) timestamp in any format understood by \
datetime.datetime.fromtimestamp()
:returns: (str) the generated path
"""
end_time_stamp = datetime.fromtimestamp(end_timestamp)
directory_name = end_time_stamp.strftime("%Y%m%d")
timestamp = end_time_stamp.strftime("%Y%m%d%H%M")
object_path = (
"./metrics/"
+ self.prometheus_host
+ "/"
+ metric_name
+ "/"
+ directory_name
+ "/"
+ timestamp
+ ".json"
)
return object_path
def custom_query(self, query: str, params: dict = None, timeout: int = None):
"""
Send a custom query to a Prometheus Host.
This method takes as input a string which will be sent as a query to
the specified Prometheus Host. This query is a PromQL query.
:param query: (str) This is a PromQL query, a few examples can be found
at https://prometheus.io/docs/prometheus/latest/querying/examples/
:param params: (dict) Optional dictionary containing GET parameters to be
sent along with the API request, such as "time"
:param timeout: (Optional) A timeout (in seconds) applied to the request
:returns: (list) A list of metric data received in response of the query sent
:raises:
(RequestException) Raises an exception in case of a connection error
(PrometheusApiClientException) Raises in case of non 200 response status code
"""
params = params or {}
data = None
query = str(query)
timeout = self._timeout if timeout is None else timeout
# using the query API to get raw data
response = self._session.get(
"{0}/api/v1/query".format(self.url),
params={**{"query": query}, **params},
verify=self._session.verify,
headers=self.headers,
auth=self.auth,
cert=self._session.cert,
timeout=timeout,
)
if response.status_code == 200:
data = response.json()["data"]["result"]
else:
raise PrometheusApiClientException(
"HTTP Status Code {} ({!r})".format(response.status_code, response.content)
)
return data
def custom_query_range(
self, query: str, start_time: datetime, end_time: datetime, step: str, params: dict = None, timeout: int = None
):
"""
Send a query_range to a Prometheus Host.
This method takes as input a string which will be sent as a query to
the specified Prometheus Host. This query is a PromQL query.
:param query: (str) This is a PromQL query, a few examples can be found
at https://prometheus.io/docs/prometheus/latest/querying/examples/
:param start_time: (datetime) A datetime object that specifies the query range start time.
:param end_time: (datetime) A datetime object that specifies the query range end time.
:param step: (str) Query resolution step width in duration format or float number of seconds
:param params: (dict) Optional dictionary containing GET parameters to be
sent along with the API request, such as "timeout"
:param timeout: (Optional) A timeout (in seconds) applied to the request
:returns: (dict) A dict of metric data received in response of the query sent
:raises:
(RequestException) Raises an exception in case of a connection error
(PrometheusApiClientException) Raises in case of non 200 response status code
"""
start = round(start_time.timestamp())
end = round(end_time.timestamp())
params = params or {}
data = None
query = str(query)
timeout = self._timeout if timeout is None else timeout
# using the query_range API to get raw data
response = self._session.get(
"{0}/api/v1/query_range".format(self.url),
params={**{"query": query, "start": start, "end": end, "step": step}, **params},
verify=self._session.verify,
headers=self.headers,
auth=self.auth,
cert=self._session.cert,
timeout=timeout,
)
if response.status_code == 200:
data = response.json()["data"]["result"]
else:
raise PrometheusApiClientException(
"HTTP Status Code {} ({!r})".format(response.status_code, response.content)
)
return data
def get_metric_aggregation(
self,
query: str,
operations: list,
start_time: datetime = None,
end_time: datetime = None,
step: str = "15",
params: dict = None,
):
"""
Get aggregations on metric values received from PromQL query.
This method takes as input a string which will be sent as a query to
the specified Prometheus Host. This query is a PromQL query. And, a
list of operations to perform such as- sum, max, min, deviation, etc.
with start_time, end_time and step.
The received query is passed to the custom_query_range method which returns
the result of the query and the values are extracted from the result.
:param query: (str) This is a PromQL query, a few examples can be found
at https://prometheus.io/docs/prometheus/latest/querying/examples/
:param operations: (list) A list of operations to perform on the values.
Operations are specified in string type.
:param start_time: (datetime) A datetime object that specifies the query range start time.
:param end_time: (datetime) A datetime object that specifies the query range end time.
:param step: (str) Query resolution step width in duration format or float number of seconds
:param params: (dict) Optional dictionary containing GET parameters to be
sent along with the API request, such as "timeout"
Available operations - sum, max, min, variance, nth percentile, deviation
and average.
:returns: (dict) A dict of aggregated values received in response to the operations
performed on the values for the query sent.
Example output:
.. code-block:: python
{
'sum': 18.05674,
'max': 6.009373
}
"""
if not isinstance(operations, list):
raise TypeError("Operations can be only of type list")
if len(operations) == 0:
_LOGGER.debug("No operations found to perform")
return None
aggregated_values = {}
query_values = []
if start_time is not None and end_time is not None:
data = self.custom_query_range(
query=query, params=params, start_time=start_time, end_time=end_time, step=step
)
for result in data:
values = result["values"]
for val in values:
query_values.append(float(val[1]))
else:
data = self.custom_query(query, params)
for result in data:
val = float(result["value"][1])
query_values.append(val)
if len(query_values) == 0:
_LOGGER.debug("No values found for given query.")
return None
np_array = numpy.array(query_values)
for operation in operations:
if operation == "sum":
aggregated_values["sum"] = numpy.sum(np_array)
elif operation == "max":
aggregated_values["max"] = numpy.max(np_array)
elif operation == "min":
aggregated_values["min"] = numpy.min(np_array)
elif operation == "average":
aggregated_values["average"] = numpy.average(np_array)
elif operation.startswith("percentile"):
percentile = float(operation.split("_")[1])
aggregated_values["percentile_" + str(percentile)] = numpy.percentile(
query_values, percentile
)
elif operation == "deviation":
aggregated_values["deviation"] = numpy.std(np_array)
elif operation == "variance":
aggregated_values["variance"] = numpy.var(np_array)
else:
raise TypeError("Invalid operation: " + operation)
return aggregated_values
def get_scrape_pools(self) -> list[str]:
"""
Get a list of all scrape pools in activeTargets.
"""
scrape_pools = []
for target in self.get_targets()['activeTargets']:
scrape_pools.append(target['scrapePool'])
return list(set(scrape_pools))
def get_targets(self, state: str = None, scrape_pool: str = None):
"""
Get a list of all targets from Prometheus.
:param state: (str) Optional filter for target state ('active', 'dropped', 'any').
If None, returns both active and dropped targets.
:param scrape_pool: (str) Optional filter by scrape pool name
:returns: (dict) A dictionary containing active and dropped targets
:raises:
(RequestException) Raises an exception in case of a connection error
(PrometheusApiClientException) Raises in case of non 200 response status code
"""
params = {}
if state:
params['state'] = state
if scrape_pool:
params['scrapePool'] = scrape_pool
response = self._session.get(
"{0}/api/v1/targets".format(self.url),
verify=self._session.verify,
headers=self.headers,
params=params,
auth=self.auth,
cert=self._session.cert,
timeout=self._timeout,
)
if response.status_code == 200:
return response.json()["data"]
else:
raise PrometheusApiClientException(
"HTTP Status Code {} ({!r})".format(
response.status_code, response.content)
)
def get_target_metadata(self, target: dict[str, str], metric: str = None):
"""
Get metadata about metrics from a specific target.
:param target: (dict) A dictionary containing target labels to match against (e.g. {'job': 'prometheus'})
:param metric: (str) Optional metric name to filter metadata
:returns: (list) A list of metadata entries for matching targets
:raises:
(RequestException) Raises an exception in case of a connection error
(PrometheusApiClientException) Raises in case of non 200 response status code
"""
params = {}
# Convert target dict to label selector string
if metric:
params['metric'] = metric
if target:
match_target = "{" + \
",".join(f'{k}="{v}"' for k, v in target.items()) + "}"
params['match_target'] = match_target
response = self._session.get(
"{0}/api/v1/targets/metadata".format(self.url),
verify=self._session.verify,
headers=self.headers,
params=params,
auth=self.auth,
cert=self._session.cert,
timeout=self._timeout,
)
if response.status_code == 200:
return response.json()["data"]
else:
raise PrometheusApiClientException(
"HTTP Status Code {} ({!r})".format(
response.status_code, response.content)
)
def get_metric_metadata(self, metric: str, limit: int = None, limit_per_metric: int = None):
"""
Get metadata about metrics.
:param metric: (str) Optional metric name to filter metadata
:param limit: (int) Optional maximum number of metrics to return
:param limit_per_metric: (int) Optional maximum number of metadata entries per metric
:returns: (dict) A dictionary mapping metric names to lists of metadata entries in format:
{'metric_name': [{'type': str, 'help': str, 'unit': str}, ...]}
:raises:
(RequestException) Raises an exception in case of a connection error
(PrometheusApiClientException) Raises in case of non 200 response status code
"""
params = {}
if metric:
params['metric'] = metric
if limit:
params['limit'] = limit
if limit_per_metric:
params['limit_per_metric'] = limit_per_metric
response = self._session.get(
"{0}/api/v1/metadata".format(self.url),
verify=self._session.verify,
headers=self.headers,
params=params,
auth=self.auth,
cert=self._session.cert,
timeout=self._timeout,
)
if response.status_code == 200:
data = response.json()["data"]
formatted_data = []
for k, v in data.items():
for v_ in v:
formatted_data.append({
"metric_name": k,
"type": v_.get('type', 'unknown'),
"help": v_.get('help', ''),
"unit": v_.get('unit', '')
})
return formatted_data
else:
raise PrometheusApiClientException(
"HTTP Status Code {} ({!r})".format(
response.status_code, response.content)
)