Skip to content

Conversation

@FRosner
Copy link
Contributor

@FRosner FRosner commented Feb 28, 2024

Summary

I am adding the ability to configure request timeouts.

Details

  • Add a global request timeout object to be used for all requests (a0dcfcf)
  • Add request timeouts for custom queries, which overwrite the global timeout if set (e0dd4a6)

References

@yashvardhannanavati
Copy link

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

@FRosner
Copy link
Contributor Author

FRosner commented Feb 29, 2024

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?

@4n4nd
Copy link
Owner

4n4nd commented Feb 29, 2024

@yashvardhannanavati

@yashvardhannanavati
Copy link

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

@FRosner
Copy link
Contributor Author

FRosner commented Mar 19, 2024

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

@FRosner
Copy link
Contributor Author

FRosner commented Mar 26, 2024

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?

@4n4nd
Copy link
Owner

4n4nd commented Apr 13, 2024

@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

@Mo-to
Copy link

Mo-to commented Jun 21, 2024

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

@4n4nd
Copy link
Owner

4n4nd commented Feb 11, 2025

Until this PR is merged, I recommend creating a session with the desired timeout and passing it to PrometheusConnect.

@kaushikthedeveloper
Copy link

@4n4nd just came across this and needed the timeout functionality as one of our queries to Prometheus never responded back and made the program go into a hung state. Need this fix, but until then, referencing to your previous comment, can you please give an example of how to do this?

Until this PR is merged, I recommend creating a session with the desired timeout and passing it to PrometheusConnect.

@4n4nd 4n4nd merged commit 6cec539 into 4n4nd:master Feb 12, 2025
@4n4nd
Copy link
Owner

4n4nd commented Feb 12, 2025

@kaushikthedeveloper with the latest release (v0.5.7) you should be able to pass a timeout value in your PrometheusConnect object. Thanks to @FRosner!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow setting request timeout

5 participants