Skip to content

Commit a0dcfcf

Browse files
committed
#277 allow setting a default timeout for all requests
1 parent 4ce551d commit a0dcfcf

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

prometheus_api_client/prometheus_connect.py

Lines changed: 18 additions & 8 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: (int) 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
@@ -403,7 +411,8 @@ def custom_query(self, query: str, params: dict = None):
403411
verify=self._session.verify,
404412
headers=self.headers,
405413
auth=self.auth,
406-
cert=self._session.cert
414+
cert=self._session.cert,
415+
timeout=self._timeout,
407416
)
408417
if response.status_code == 200:
409418
data = response.json()["data"]["result"]
@@ -447,7 +456,8 @@ def custom_query_range(
447456
verify=self._session.verify,
448457
headers=self.headers,
449458
auth=self.auth,
450-
cert=self._session.cert
459+
cert=self._session.cert,
460+
timeout=self._timeout,
451461
)
452462
if response.status_code == 200:
453463
data = response.json()["data"]["result"]

0 commit comments

Comments
 (0)