Skip to content

Commit 6cec539

Browse files
authored
#277 Add the ability to specify request timeouts (#280)
* #277 allow setting a default timeout for all requests * #277 allow custom timeout for custom_query and custom_query_range * Apply suggestions from code review
1 parent cd638cb commit 6cec539

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

prometheus_api_client/prometheus_connect.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class PrometheusConnect:
4040
:param proxy: (Optional) Proxies dictionary to enable connection through proxy.
4141
Example: {"http_proxy": "<ip_address/hostname:port>", "https_proxy": "<ip_address/hostname:port>"}
4242
:param session (Optional) Custom requests.Session to enable complex HTTP configuration
43+
:param timeout: (Optional) A timeout (in seconds) applied to all requests
4344
"""
4445

4546
def __init__(
@@ -51,6 +52,7 @@ def __init__(
5152
auth: tuple = None,
5253
proxy: dict = None,
5354
session: Session = None,
55+
timeout: int = None,
5456
):
5557
"""Functions as a Constructor for the class PrometheusConnect."""
5658
if url is None:
@@ -60,6 +62,7 @@ def __init__(
6062
self.url = url
6163
self.prometheus_host = urlparse(self.url).netloc
6264
self._all_metrics = None
65+
self._timeout = timeout
6366

6467
if retry is None:
6568
retry = Retry(
@@ -94,7 +97,8 @@ def check_prometheus_connection(self, params: dict = None) -> bool:
9497
headers=self.headers,
9598
params=params,
9699
auth=self.auth,
97-
cert=self._session.cert
100+
cert=self._session.cert,
101+
timeout=self._timeout,
98102
)
99103
return response.ok
100104

@@ -131,7 +135,8 @@ def get_label_names(self, params: dict = None):
131135
headers=self.headers,
132136
params=params,
133137
auth=self.auth,
134-
cert=self._session.cert
138+
cert=self._session.cert,
139+
timeout=self._timeout,
135140
)
136141

137142
if response.status_code == 200:
@@ -161,7 +166,8 @@ def get_label_values(self, label_name: str, params: dict = None):
161166
headers=self.headers,
162167
params=params,
163168
auth=self.auth,
164-
cert=self._session.cert
169+
cert=self._session.cert,
170+
timeout=self._timeout,
165171
)
166172

167173
if response.status_code == 200:
@@ -212,7 +218,8 @@ def get_current_metric_value(
212218
verify=self._session.verify,
213219
headers=self.headers,
214220
auth=self.auth,
215-
cert=self._session.cert
221+
cert=self._session.cert,
222+
timeout=self._timeout,
216223
)
217224

218225
if response.status_code == 200:
@@ -304,7 +311,8 @@ def get_metric_range_data(
304311
verify=self._session.verify,
305312
headers=self.headers,
306313
auth=self.auth,
307-
cert=self._session.cert
314+
cert=self._session.cert,
315+
timeout=self._timeout,
308316
)
309317
if response.status_code == 200:
310318
data += response.json()["data"]["result"]
@@ -377,7 +385,7 @@ def _metric_filename(self, metric_name: str, end_timestamp: int):
377385
)
378386
return object_path
379387

380-
def custom_query(self, query: str, params: dict = None):
388+
def custom_query(self, query: str, params: dict = None, timeout: int = None):
381389
"""
382390
Send a custom query to a Prometheus Host.
383391
@@ -388,6 +396,7 @@ def custom_query(self, query: str, params: dict = None):
388396
at https://prometheus.io/docs/prometheus/latest/querying/examples/
389397
:param params: (dict) Optional dictionary containing GET parameters to be
390398
sent along with the API request, such as "time"
399+
:param timeout: (Optional) A timeout (in seconds) applied to the request
391400
:returns: (list) A list of metric data received in response of the query sent
392401
:raises:
393402
(RequestException) Raises an exception in case of a connection error
@@ -396,14 +405,16 @@ def custom_query(self, query: str, params: dict = None):
396405
params = params or {}
397406
data = None
398407
query = str(query)
408+
timeout = self._timeout if timeout is None else timeout
399409
# using the query API to get raw data
400410
response = self._session.get(
401411
"{0}/api/v1/query".format(self.url),
402412
params={**{"query": query}, **params},
403413
verify=self._session.verify,
404414
headers=self.headers,
405415
auth=self.auth,
406-
cert=self._session.cert
416+
cert=self._session.cert,
417+
timeout=timeout,
407418
)
408419
if response.status_code == 200:
409420
data = response.json()["data"]["result"]
@@ -415,7 +426,7 @@ def custom_query(self, query: str, params: dict = None):
415426
return data
416427

417428
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
419430
):
420431
"""
421432
Send a query_range to a Prometheus Host.
@@ -430,6 +441,7 @@ def custom_query_range(
430441
:param step: (str) Query resolution step width in duration format or float number of seconds
431442
:param params: (dict) Optional dictionary containing GET parameters to be
432443
sent along with the API request, such as "timeout"
444+
:param timeout: (Optional) A timeout (in seconds) applied to the request
433445
:returns: (dict) A dict of metric data received in response of the query sent
434446
:raises:
435447
(RequestException) Raises an exception in case of a connection error
@@ -440,14 +452,16 @@ def custom_query_range(
440452
params = params or {}
441453
data = None
442454
query = str(query)
455+
timeout = self._timeout if timeout is None else timeout
443456
# using the query_range API to get raw data
444457
response = self._session.get(
445458
"{0}/api/v1/query_range".format(self.url),
446459
params={**{"query": query, "start": start, "end": end, "step": step}, **params},
447460
verify=self._session.verify,
448461
headers=self.headers,
449462
auth=self.auth,
450-
cert=self._session.cert
463+
cert=self._session.cert,
464+
timeout=timeout,
451465
)
452466
if response.status_code == 200:
453467
data = response.json()["data"]["result"]

0 commit comments

Comments
 (0)