From e4807d219b59fd7e2e29eb7af098e62635a602cb Mon Sep 17 00:00:00 2001 From: jonmv Date: Tue, 31 May 2022 12:47:29 +0200 Subject: Use hc5 in metrics handlers --- application/pom.xml | 10 +++++++ .../handler/metrics/MetricsV2Handler.java | 34 ++++++++++++++------- .../handler/metrics/PrometheusV1Handler.java | 35 +++++++++++++++------- container-test/pom.xml | 4 --- 4 files changed, 57 insertions(+), 26 deletions(-) diff --git a/application/pom.xml b/application/pom.xml index f1856e8c85c..50563579782 100644 --- a/application/pom.xml +++ b/application/pom.xml @@ -119,6 +119,16 @@ org.apache.commons commons-math3 + + org.apache.httpcomponents.client5 + httpclient5 + + + org.slf4j + slf4j-api + + + com.yahoo.vespa diff --git a/container-core/src/main/java/com/yahoo/container/handler/metrics/MetricsV2Handler.java b/container-core/src/main/java/com/yahoo/container/handler/metrics/MetricsV2Handler.java index e1a14ef541b..0a71489a605 100644 --- a/container-core/src/main/java/com/yahoo/container/handler/metrics/MetricsV2Handler.java +++ b/container-core/src/main/java/com/yahoo/container/handler/metrics/MetricsV2Handler.java @@ -1,25 +1,26 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.container.handler.metrics; -import ai.vespa.util.http.hc4.VespaHttpClientBuilder; +import ai.vespa.util.http.hc5.VespaHttpClientBuilder; import com.google.inject.Inject; import com.yahoo.container.jdisc.HttpResponse; import com.yahoo.restapi.Path; import com.yahoo.yolean.Exceptions; -import org.apache.http.client.HttpClient; -import org.apache.http.client.config.RequestConfig; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.impl.client.BasicResponseHandler; -import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.hc.client5.http.classic.methods.HttpGet; +import org.apache.hc.client5.http.config.RequestConfig; +import org.apache.hc.client5.http.impl.classic.BasicHttpClientResponseHandler; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import java.io.IOException; import java.net.URI; import java.util.List; import java.util.Optional; import java.util.concurrent.Executor; +import java.util.logging.Level; import static com.yahoo.jdisc.Response.Status.INTERNAL_SERVER_ERROR; import static com.yahoo.jdisc.Response.Status.OK; +import static java.util.concurrent.TimeUnit.MILLISECONDS; /** * @author gjoranv @@ -33,7 +34,7 @@ public class MetricsV2Handler extends HttpHandlerBase { private static final int HTTP_SOCKET_TIMEOUT = 30000; private final String metricsProxyUri; - private final HttpClient httpClient = createHttpClient(); + private final CloseableHttpClient httpClient = createHttpClient(); @Inject public MetricsV2Handler(Executor executor, @@ -52,7 +53,7 @@ public class MetricsV2Handler extends HttpHandlerBase { private JsonResponse valuesResponse(String consumer) { try { String uri = metricsProxyUri + consumerQuery(consumer); - String metricsJson = httpClient.execute(new HttpGet(uri), new BasicResponseHandler()); + String metricsJson = httpClient.execute(new HttpGet(uri), new BasicHttpClientResponseHandler()); return new JsonResponse(OK, metricsJson); } catch (IOException e) { log.warning("Unable to retrieve metrics from " + metricsProxyUri + ": " + Exceptions.toMessageString(e)); @@ -64,9 +65,9 @@ public class MetricsV2Handler extends HttpHandlerBase { return VespaHttpClientBuilder.create() .setUserAgent("application-metrics-retriever") .setDefaultRequestConfig(RequestConfig.custom() - .setConnectTimeout(HTTP_CONNECT_TIMEOUT) - .setSocketTimeout(HTTP_SOCKET_TIMEOUT) - .build()) + .setConnectTimeout(HTTP_CONNECT_TIMEOUT, MILLISECONDS) + .setResponseTimeout(HTTP_SOCKET_TIMEOUT, MILLISECONDS) + .build()) .build(); } @@ -74,4 +75,15 @@ public class MetricsV2Handler extends HttpHandlerBase { return (consumer == null || consumer.isEmpty()) ? "" : "?consumer=" + consumer; } + @Override + public void destroy(){ + super.destroy(); + try { + httpClient.close(); + } + catch (IOException e) { + log.log(Level.WARNING, "Failed closing http client", e); + } + } + } diff --git a/container-core/src/main/java/com/yahoo/container/handler/metrics/PrometheusV1Handler.java b/container-core/src/main/java/com/yahoo/container/handler/metrics/PrometheusV1Handler.java index aa99beb3b24..6ea35138606 100644 --- a/container-core/src/main/java/com/yahoo/container/handler/metrics/PrometheusV1Handler.java +++ b/container-core/src/main/java/com/yahoo/container/handler/metrics/PrometheusV1Handler.java @@ -1,31 +1,32 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.container.handler.metrics; -import ai.vespa.util.http.hc4.VespaHttpClientBuilder; +import ai.vespa.util.http.hc5.VespaHttpClientBuilder; import com.google.inject.Inject; import com.yahoo.container.jdisc.HttpResponse; import com.yahoo.restapi.Path; import com.yahoo.restapi.StringResponse; import com.yahoo.yolean.Exceptions; -import org.apache.http.client.HttpClient; -import org.apache.http.client.config.RequestConfig; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.impl.client.BasicResponseHandler; -import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.hc.client5.http.classic.methods.HttpGet; +import org.apache.hc.client5.http.config.RequestConfig; +import org.apache.hc.client5.http.impl.classic.BasicHttpClientResponseHandler; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import java.io.IOException; import java.net.URI; import java.util.List; import java.util.Optional; import java.util.concurrent.Executor; +import java.util.logging.Level; import static com.yahoo.container.handler.metrics.MetricsV2Handler.consumerQuery; import static com.yahoo.jdisc.Response.Status.INTERNAL_SERVER_ERROR; +import static java.util.concurrent.TimeUnit.MILLISECONDS; /** * @author Oracien */ -public class PrometheusV1Handler extends HttpHandlerBase{ +public class PrometheusV1Handler extends HttpHandlerBase { public static final String V1_PATH = "/prometheus/v1"; static final String VALUES_PATH = V1_PATH + "/values"; @@ -34,7 +35,7 @@ public class PrometheusV1Handler extends HttpHandlerBase{ private static final int HTTP_SOCKET_TIMEOUT = 30000; private final String metricsProxyUri; - private final HttpClient httpClient = createHttpClient(); + private final CloseableHttpClient httpClient = createHttpClient(); @Inject public PrometheusV1Handler(Executor executor, @@ -53,7 +54,7 @@ public class PrometheusV1Handler extends HttpHandlerBase{ private HttpResponse valuesResponse(String consumer) { try { String uri = metricsProxyUri + consumerQuery(consumer); - String prometheusText = httpClient.execute(new HttpGet(uri), new BasicResponseHandler()); + String prometheusText = httpClient.execute(new HttpGet(uri), new BasicHttpClientResponseHandler()); return new StringResponse(prometheusText); } catch (IOException e) { log.warning("Unable to retrieve metrics from " + metricsProxyUri + ": " + Exceptions.toMessageString(e)); @@ -65,9 +66,21 @@ public class PrometheusV1Handler extends HttpHandlerBase{ return VespaHttpClientBuilder.create() .setUserAgent("application-prometheus-receiver") .setDefaultRequestConfig(RequestConfig.custom() - .setConnectTimeout(HTTP_CONNECT_TIMEOUT) - .setSocketTimeout(HTTP_SOCKET_TIMEOUT) + .setConnectTimeout(HTTP_CONNECT_TIMEOUT, MILLISECONDS) + .setResponseTimeout(HTTP_SOCKET_TIMEOUT, MILLISECONDS) .build()) .build(); } + + @Override + public void destroy(){ + super.destroy(); + try { + httpClient.close(); + } + catch (IOException e) { + log.log(Level.WARNING, "Failed closing http client", e); + } + } + } diff --git a/container-test/pom.xml b/container-test/pom.xml index 9b5b7ba17a9..961d827a390 100644 --- a/container-test/pom.xml +++ b/container-test/pom.xml @@ -22,10 +22,6 @@ application ${project.version} - - org.apache.httpcomponents.client5 - httpclient5 - biz.aQute.bnd * -- cgit v1.2.3