prometheus-api-client-python icon indicating copy to clipboard operation
prometheus-api-client-python copied to clipboard

#277 Add the ability to specify request timeouts

Open FRosner opened this issue 1 year ago • 8 comments

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 avatar Feb 28 '24 08:02 FRosner

@FRosner Could you please add a couple of unit tests for this change?

yashvardhannanavati avatar Feb 28 '24 22:02 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 avatar Feb 29 '24 08:02 FRosner

@yashvardhannanavati

4n4nd avatar Feb 29 '24 12:02 4n4nd

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

yashvardhannanavati avatar Mar 07 '24 16:03 yashvardhannanavati

Thanks! I got distracted a bit. I will try to get to this by next week.

FRosner avatar Mar 19 '24 09:03 FRosner

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 avatar Mar 26 '24 10:03 FRosner

@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

4n4nd avatar Apr 13 '24 00:04 4n4nd

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

The changes are in this fork in this commit

Mo-to avatar Jun 21 '24 00:06 Mo-to