14
14
# set up logging
15
15
_LOGGER = logging .getLogger (__name__ )
16
16
17
- DEBUG = False
17
+ # In case of a connection failure try 2 more times
18
18
MAX_REQUEST_RETRIES = 3
19
19
CONNECTION_RETRY_WAIT_TIME = 1000 # wait 1 second before retrying in case of an error
20
20
@@ -24,9 +24,7 @@ class PrometheusConnect:
24
24
"""
25
25
def __init__ (self , url = 'http://127.0.0.1:9090' , headers = None , disable_ssl = False ):
26
26
"""
27
- A short description.
28
-
29
- A bit longer description.
27
+ Constructor for the class PrometheusConnect
30
28
31
29
Args:
32
30
url (str): url for the prometheus host
@@ -50,10 +48,8 @@ def all_metrics(self):
50
48
list: list of names of all the metrics available from the specified prometheus host
51
49
52
50
Raises:
53
- Exception: description
54
-
51
+ Http Response error: Raises an exception in case of a connection error
55
52
"""
56
-
57
53
response = requests .get ('{0}/api/v1/label/__name__/values' .format (self .url ),
58
54
verify = self .ssl_verification ,
59
55
headers = self .headers )
@@ -88,9 +84,7 @@ def get_current_metric_value(self, metric_name, label_config=None):
88
84
89
85
Raises:
90
86
Http Response error: Raises an exception in case of a connection error
91
-
92
87
"""
93
-
94
88
data = []
95
89
if label_config :
96
90
label_list = [str (key + "=" + "'" + label_config [key ]+ "'" ) for key in label_config ]
@@ -135,8 +129,21 @@ def get_metric_range_data(self,
135
129
Args:
136
130
metric_name (str): The name of the metric
137
131
label_config (dict): A dictionary that specifies metric labels and their values
138
- start_time (str):
139
- end_time (str):
132
+ start_time (str): A string that specifies the metric range start time.
133
+ It uses the dateparser (https://dateparser.readthedocs.io/en/v0.3.4/)
134
+ module.
135
+ Example:
136
+ start_time='15m' will set the start time to
137
+ 15 mins before the current time
138
+ start_time='1553092437' will set the start time
139
+ to the given unix timestamp
140
+ start_time='12 May 2018' will set the start time to
141
+ '2018-05-12 00:00:00'
142
+ end_time (str): A string that specifies the metric range end time.
143
+ It follows the same rules as parameter start_time, it just
144
+ needs to be a time later than the start_time.
145
+ Example:
146
+ end_time='now' will set the end time to the current time
140
147
chunk_size (str): Duration of metric data downloaded in one request.
141
148
example, setting it to '3h' will download 3 hours
142
149
worth of data in each request made to the prometheus host
@@ -148,7 +155,6 @@ def get_metric_range_data(self,
148
155
149
156
Raises:
150
157
Http Response error: Raises an exception in case of a connection error
151
-
152
158
"""
153
159
data = []
154
160
@@ -230,15 +236,50 @@ def _metric_filename(self, metric_name, end_timestamp):
230
236
metric_name + "/" + directory_name + "/" + timestamp + ".json"
231
237
return object_path
232
238
239
+ @retry (stop_max_attempt_number = MAX_REQUEST_RETRIES , wait_fixed = CONNECTION_RETRY_WAIT_TIME )
240
+ def custom_query (self , query : str ):
241
+ """
242
+ A method to send a custom query to a Prometheus Host.
243
+
244
+ This method takes as input a string which will be sent as a query to
245
+ the specified Prometheus Host. This query is a PromQL query.
246
+
247
+ Args:
248
+ query (str): This is a PromQL query, a few examples can be found
249
+ at https://prometheus.io/docs/prometheus/latest/querying/examples/
250
+
251
+ Returns:
252
+ list: A list of metric data received in response of the query sent
253
+
254
+ Raises:
255
+ Http Response error: Raises an exception in case of a connection error
256
+ """
257
+ data = None
258
+ query = str (query )
259
+ # using the query API to get raw data
260
+ response = requests .get ('{0}/api/v1/query' .format (self .url ),
261
+ params = {'query' : query
262
+ },
263
+ verify = self .ssl_verification ,
264
+ headers = self .headers )
265
+ if response .status_code == 200 :
266
+ data = response .json ()['data' ]['result' ]
267
+ else :
268
+ raise Exception ("HTTP Status Code {} ({})" .format (
269
+ response .status_code ,
270
+ response .content
271
+ ))
272
+
273
+ return data
274
+
233
275
def pretty_print_metric (metric_data ):
234
276
"""
235
277
A function to pretty print the metric data downloaded using class PrometheusConnect.
236
278
237
279
Args:
238
- metric_data (list): This is the metric data returned from functions
280
+ metric_data (list): This is the metric data list returned from methods
239
281
get_metric_range_data and get_current_metric_value
240
282
"""
241
-
242
283
data = metric_data
243
284
for metric in data :
244
285
print (json .dumps (metric , indent = 4 , sort_keys = True ))
0 commit comments