#277 Add the ability to specify request timeouts
Summary
I am adding the ability to configure request timeouts.
Details
- Add a global request timeout object to be used for all requests (https://github.com/4n4nd/prometheus-api-client-python/commit/a0dcfcf8448ed5a0815f1fd15e5fd6d975bf0f71)
- Add request timeouts for custom queries, which overwrite the global timeout if set (https://github.com/4n4nd/prometheus-api-client-python/commit/e0dd4a623bb9659cc27a7be51abc9b05668696b1)
References
- Fixes https://github.com/4n4nd/prometheus-api-client-python/issues/277
@FRosner Could you please add a couple of unit tests for this change?
Could you please add a couple of unit tests for this change?
Sure @4n4nd! Do you have a pointer for me on which file to add this to? Do you have any tests that simulate making requests to Prom where I can add a case that times out?
@yashvardhannanavati
Could you please add a couple of unit tests for this change?
Sure @4n4nd! Do you have a pointer for me on which file to add this to? Do you have any tests that simulate making requests to Prom where I can add a case that times out?
@FRosner You could add a test to https://github.com/4n4nd/prometheus-api-client-python/blob/master/tests/test_prometheus_connect.py
Thanks! I got distracted a bit. I will try to get to this by next week.
Sorry, I'm back after a bit. I looked into the test and it seems I have to bring my own Prometheus? Is there a specific reason you are not using testcontainers?
Either way, do you have a preference on how I can delay the response so that I hit a timeout? A mock server? Toxy proxy?
@FRosner you can see how we are testing other functions here using a mocked network. In addition to that, we use this public prometheus instance to run some of our tests.
You can basically execute the following command to run the test suite:
PROM_URL="http://demo.robustperception.io:9090/" pytest
I respect check have to be passed, but for anyone who is searching for a quick solution i will provide a fork that adds an addtional request_args parameter to every function. This solution is not perfect at all, because the parameters get parsed differently on different levels and functions, but i thought i could may help someone in special cases.
Usage
Just put the additional parameters into the request_args parameter. These kwargs get mostly passed to requests.Session.request
example_data = prometheus_connection.get_metric_range_data(
metric_name="example",
start_time=start_time,
end_time=end_time,
request_args={'timeout': 1} #<-- Added parameter
)
request_args argument equals **kwargs in the named method.
Installation
set PYTHONUTF8=1#<-- needed for the build on windows
pip install git+https://github.com/Mo-to/prometheus-api-client-python.git