Skip to content

Commit e0dd4a6

Browse files
committed
4n4nd#277 allow custom timeout for custom_query and custom_query_range
1 parent a0dcfcf commit e0dd4a6

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

prometheus_api_client/prometheus_connect.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ def custom_query(self, query: str, params: dict = None, timeout: int = None):
396396
at https://prometheus.io/docs/prometheus/latest/querying/examples/
397397
:param params: (dict) Optional dictionary containing GET parameters to be
398398
sent along with the API request, such as "time"
399+
:param timeout: (int) A timeout (in seconds) applied to the request
399400
:returns: (list) A list of metric data received in response of the query sent
400401
:raises:
401402
(RequestException) Raises an exception in case of a connection error
@@ -404,6 +405,7 @@ def custom_query(self, query: str, params: dict = None, timeout: int = None):
404405
params = params or {}
405406
data = None
406407
query = str(query)
408+
timeout = self._timeout if timeout is None else timeout
407409
# using the query API to get raw data
408410
response = self._session.get(
409411
"{0}/api/v1/query".format(self.url),
@@ -412,7 +414,7 @@ def custom_query(self, query: str, params: dict = None, timeout: int = None):
412414
headers=self.headers,
413415
auth=self.auth,
414416
cert=self._session.cert,
415-
timeout=self._timeout,
417+
timeout=timeout,
416418
)
417419
if response.status_code == 200:
418420
data = response.json()["data"]["result"]
@@ -424,7 +426,7 @@ def custom_query(self, query: str, params: dict = None, timeout: int = None):
424426
return data
425427

426428
def custom_query_range(
427-
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
428430
):
429431
"""
430432
Send a query_range to a Prometheus Host.
@@ -439,6 +441,7 @@ def custom_query_range(
439441
:param step: (str) Query resolution step width in duration format or float number of seconds
440442
:param params: (dict) Optional dictionary containing GET parameters to be
441443
sent along with the API request, such as "timeout"
444+
:param timeout: (int) A timeout (in seconds) applied to the request
442445
:returns: (dict) A dict of metric data received in response of the query sent
443446
:raises:
444447
(RequestException) Raises an exception in case of a connection error
@@ -449,6 +452,7 @@ def custom_query_range(
449452
params = params or {}
450453
data = None
451454
query = str(query)
455+
timeout = self._timeout if timeout is None else timeout
452456
# using the query_range API to get raw data
453457
response = self._session.get(
454458
"{0}/api/v1/query_range".format(self.url),
@@ -457,7 +461,7 @@ def custom_query_range(
457461
headers=self.headers,
458462
auth=self.auth,
459463
cert=self._session.cert,
460-
timeout=self._timeout,
464+
timeout=timeout,
461465
)
462466
if response.status_code == 200:
463467
data = response.json()["data"]["result"]

0 commit comments

Comments
 (0)