Fix timeout for prometheus exporter for HTTP/1.1 (due to keep-alive)

Before:

    $ time curl -s --http1.1 127.1:9363/metrics > /dev/null
    real    0m10.018s # default keep_alive_timeout is 10 seconds
    user    0m0.005s
    sys     0m0.001s

After

    $ time curl -s --http1.1 127.1:9363/metrics > /dev/null
    real    0m0.008s
    user    0m0.006s
    sys     0m0.000s

And if you will look at the test_prometheus_endpoint, you will see that
it takes > 30 seconds (it obtains metrics 3 times), after this patch it
should be finished more or less instantly.

Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
This commit is contained in:
Azat Khuzhin 2024-01-19 13:17:28 +03:00
parent 90057c6f39
commit 38ad4ef493
2 changed files with 6 additions and 0 deletions

View File

@ -23,6 +23,10 @@ void PrometheusRequestHandler::handleRequest(HTTPServerRequest & request, HTTPSe
const auto & config = server.config(); const auto & config = server.config();
unsigned keep_alive_timeout = config.getUInt("keep_alive_timeout", DEFAULT_HTTP_KEEP_ALIVE_TIMEOUT); unsigned keep_alive_timeout = config.getUInt("keep_alive_timeout", DEFAULT_HTTP_KEEP_ALIVE_TIMEOUT);
/// In order to make keep-alive works.
if (request.getVersion() == HTTPServerRequest::HTTP_1_1)
response.setChunkedTransferEncoding(true);
setResponseDefaultHeaders(response, keep_alive_timeout); setResponseDefaultHeaders(response, keep_alive_timeout);
response.setContentType("text/plain; version=0.0.4; charset=UTF-8"); response.setContentType("text/plain; version=0.0.4; charset=UTF-8");

View File

@ -40,6 +40,8 @@ def get_and_check_metrics(retries):
response = requests.get( response = requests.get(
"http://{host}:{port}/metrics".format(host=node.ip_address, port=8001), "http://{host}:{port}/metrics".format(host=node.ip_address, port=8001),
allow_redirects=False, allow_redirects=False,
# less then default keep-alive timeout (10 seconds)
timeout=5,
) )
if response.status_code != 200: if response.status_code != 200: