This repository has been archived by the owner on May 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thanos_api_client.py
80 lines (69 loc) · 3.29 KB
/
thanos_api_client.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
from prometheus_api_client import PrometheusConnect, PrometheusApiClientException
from typing import Sequence
from pandas import DataFrame
import datetime
class ThanosConnect(PrometheusConnect):
def __init__(self, url, op_token):
super().__init__(url=url,
headers={"Authorization": f"Bearer {op_token}"},
disable_ssl=False)
@staticmethod
def build_query(labels_config: dict, metric_name: str = ''):
if labels_config:
label_list = [f'{key}="{val}"' for key, val in labels_config.items()]
return f'{metric_name}{{{",".join(label_list)}}}'
else:
return ''
"""
:returns a query result over the selected metrics based on the given params - a list of metrics
:arguments:
start_time - Start time for the range of the query over the selected metrics
end_time - End time for the range of the query over the selected metrics
step - The steps in time between each sample to align the range according to the step's resolution.
metric_name - The metrics that we want to get from our sensor and we query upon. Can have more than one metric value
label_config - the labels of the metrics we query upon. (Additional parameters for filtering the query)
:reference https://prometheus.io/docs/prometheus/latest/querying/api/#range-queries
"""
def range_query(self, start_time: datetime, end_time: datetime, step: int, metric_name='',
label_config: dict = None):
query = self.build_query(label_config, metric_name)
metric_data = self.custom_query_range(query=query,
start_time=start_time,
end_time=end_time,
step=str(step))
return metric_data
"""
:returns all possible values of for a given label
:reference: https://prometheus.io/docs/prometheus/latest/querying/api/#querying-label-values
"""
def query_label_values(self, label: str, cluster: str = None):
if cluster:
match = {'match[]': self.build_query({'cluster': cluster})}
else:
match = None
response = self._session.get(
'{url}/api/v1/label/{label}/values'.format(url=self.url, label=label),
params=match,
verify=self.ssl_verification,
headers=self.headers,
)
if response.status_code == 200:
data = response.json()["data"]
else:
raise PrometheusApiClientException(
"HTTP Status Code {} ({!r})".format(response.status_code, response.content)
)
return data
@staticmethod
def metric_data_to_df(data):
def _get_values(metric: dict):
return metric["values"] if "values" in metric else [metric["value"]]
if data is not None:
# if just a single json instead of list/set/other sequence of jsons,
# treat as list with single entry
if not isinstance(data, Sequence):
data = [data]
# unpack metric
unpack = [{**i["metric"], **{"timestamp": val[0], "value": val[1]}}
for i in data for val in _get_values(i)]
return DataFrame(unpack)