@@ -40,6 +40,7 @@ class PrometheusConnect:
40
40
:param proxy: (Optional) Proxies dictionary to enable connection through proxy.
41
41
Example: {"http_proxy": "<ip_address/hostname:port>", "https_proxy": "<ip_address/hostname:port>"}
42
42
:param session (Optional) Custom requests.Session to enable complex HTTP configuration
43
+ :param timeout: (Optional) A timeout (in seconds) applied to all requests
43
44
"""
44
45
45
46
def __init__ (
@@ -51,6 +52,7 @@ def __init__(
51
52
auth : tuple = None ,
52
53
proxy : dict = None ,
53
54
session : Session = None ,
55
+ timeout : int = None ,
54
56
):
55
57
"""Functions as a Constructor for the class PrometheusConnect."""
56
58
if url is None :
@@ -60,6 +62,7 @@ def __init__(
60
62
self .url = url
61
63
self .prometheus_host = urlparse (self .url ).netloc
62
64
self ._all_metrics = None
65
+ self ._timeout = timeout
63
66
64
67
if retry is None :
65
68
retry = Retry (
@@ -94,7 +97,8 @@ def check_prometheus_connection(self, params: dict = None) -> bool:
94
97
headers = self .headers ,
95
98
params = params ,
96
99
auth = self .auth ,
97
- cert = self ._session .cert
100
+ cert = self ._session .cert ,
101
+ timeout = self ._timeout ,
98
102
)
99
103
return response .ok
100
104
@@ -131,7 +135,8 @@ def get_label_names(self, params: dict = None):
131
135
headers = self .headers ,
132
136
params = params ,
133
137
auth = self .auth ,
134
- cert = self ._session .cert
138
+ cert = self ._session .cert ,
139
+ timeout = self ._timeout ,
135
140
)
136
141
137
142
if response .status_code == 200 :
@@ -161,7 +166,8 @@ def get_label_values(self, label_name: str, params: dict = None):
161
166
headers = self .headers ,
162
167
params = params ,
163
168
auth = self .auth ,
164
- cert = self ._session .cert
169
+ cert = self ._session .cert ,
170
+ timeout = self ._timeout ,
165
171
)
166
172
167
173
if response .status_code == 200 :
@@ -212,7 +218,8 @@ def get_current_metric_value(
212
218
verify = self ._session .verify ,
213
219
headers = self .headers ,
214
220
auth = self .auth ,
215
- cert = self ._session .cert
221
+ cert = self ._session .cert ,
222
+ timeout = self ._timeout ,
216
223
)
217
224
218
225
if response .status_code == 200 :
@@ -304,7 +311,8 @@ def get_metric_range_data(
304
311
verify = self ._session .verify ,
305
312
headers = self .headers ,
306
313
auth = self .auth ,
307
- cert = self ._session .cert
314
+ cert = self ._session .cert ,
315
+ timeout = self ._timeout ,
308
316
)
309
317
if response .status_code == 200 :
310
318
data += response .json ()["data" ]["result" ]
@@ -377,7 +385,7 @@ def _metric_filename(self, metric_name: str, end_timestamp: int):
377
385
)
378
386
return object_path
379
387
380
- def custom_query (self , query : str , params : dict = None ):
388
+ def custom_query (self , query : str , params : dict = None , timeout : int = None ):
381
389
"""
382
390
Send a custom query to a Prometheus Host.
383
391
@@ -388,6 +396,7 @@ def custom_query(self, query: str, params: dict = None):
388
396
at https://prometheus.io/docs/prometheus/latest/querying/examples/
389
397
:param params: (dict) Optional dictionary containing GET parameters to be
390
398
sent along with the API request, such as "time"
399
+ :param timeout: (Optional) A timeout (in seconds) applied to the request
391
400
:returns: (list) A list of metric data received in response of the query sent
392
401
:raises:
393
402
(RequestException) Raises an exception in case of a connection error
@@ -396,14 +405,16 @@ def custom_query(self, query: str, params: dict = None):
396
405
params = params or {}
397
406
data = None
398
407
query = str (query )
408
+ timeout = self ._timeout if timeout is None else timeout
399
409
# using the query API to get raw data
400
410
response = self ._session .get (
401
411
"{0}/api/v1/query" .format (self .url ),
402
412
params = {** {"query" : query }, ** params },
403
413
verify = self ._session .verify ,
404
414
headers = self .headers ,
405
415
auth = self .auth ,
406
- cert = self ._session .cert
416
+ cert = self ._session .cert ,
417
+ timeout = timeout ,
407
418
)
408
419
if response .status_code == 200 :
409
420
data = response .json ()["data" ]["result" ]
@@ -415,7 +426,7 @@ def custom_query(self, query: str, params: dict = None):
415
426
return data
416
427
417
428
def custom_query_range (
418
- self , query : str , start_time : datetime , end_time : datetime , step : str , params : dict = None
429
+ self , query : str , start_time : datetime , end_time : datetime , step : str , params : dict = None , timeout : int = None
419
430
):
420
431
"""
421
432
Send a query_range to a Prometheus Host.
@@ -430,6 +441,7 @@ def custom_query_range(
430
441
:param step: (str) Query resolution step width in duration format or float number of seconds
431
442
:param params: (dict) Optional dictionary containing GET parameters to be
432
443
sent along with the API request, such as "timeout"
444
+ :param timeout: (Optional) A timeout (in seconds) applied to the request
433
445
:returns: (dict) A dict of metric data received in response of the query sent
434
446
:raises:
435
447
(RequestException) Raises an exception in case of a connection error
@@ -440,14 +452,16 @@ def custom_query_range(
440
452
params = params or {}
441
453
data = None
442
454
query = str (query )
455
+ timeout = self ._timeout if timeout is None else timeout
443
456
# using the query_range API to get raw data
444
457
response = self ._session .get (
445
458
"{0}/api/v1/query_range" .format (self .url ),
446
459
params = {** {"query" : query , "start" : start , "end" : end , "step" : step }, ** params },
447
460
verify = self ._session .verify ,
448
461
headers = self .headers ,
449
462
auth = self .auth ,
450
- cert = self ._session .cert
463
+ cert = self ._session .cert ,
464
+ timeout = timeout ,
451
465
)
452
466
if response .status_code == 200 :
453
467
data = response .json ()["data" ]["result" ]
0 commit comments