diff options
author | gjoranv <gv@verizonmedia.com> | 2020-01-23 12:38:55 +0100 |
---|---|---|
committer | gjoranv <gv@verizonmedia.com> | 2020-01-24 03:32:11 +0100 |
commit | 8f7d794b2da0803e7d7d917b8b10eca7b1e376e5 (patch) | |
tree | cf6687c3ac9bdb65ee86da5ba842b83e50a32dc9 /container-core/src/main/java/com/yahoo/container/handler/metrics | |
parent | f484a3a339992f07ee54be7d3d128b48ffbb6a98 (diff) |
Add MetricsV2Handler to application containers.
Diffstat (limited to 'container-core/src/main/java/com/yahoo/container/handler/metrics')
-rw-r--r-- | container-core/src/main/java/com/yahoo/container/handler/metrics/MetricsV2Handler.java | 77 |
1 files changed, 77 insertions, 0 deletions
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 new file mode 100644 index 00000000000..bfa825f0404 --- /dev/null +++ b/container-core/src/main/java/com/yahoo/container/handler/metrics/MetricsV2Handler.java @@ -0,0 +1,77 @@ +// Copyright 2019 Oath Inc. 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.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.http.impl.conn.PoolingHttpClientConnectionManager; + +import java.io.IOException; +import java.net.URI; +import java.util.List; +import java.util.Optional; +import java.util.concurrent.Executor; + +import static com.yahoo.jdisc.Response.Status.INTERNAL_SERVER_ERROR; +import static com.yahoo.jdisc.Response.Status.OK; + +/** + * @author gjoranv + */ +public class MetricsV2Handler extends HttpHandlerBase { + + public static final String V2_PATH = "/metrics/v2"; + static final String VALUES_PATH = V2_PATH + "/values"; + + private static final int HTTP_CONNECT_TIMEOUT = 5000; + private static final int HTTP_SOCKET_TIMEOUT = 30000; + + private final String metricsProxyUri; + private final HttpClient httpClient = createHttpClient(); + + @Inject + public MetricsV2Handler(Executor executor, + MetricsProxyApiConfig config) { + super(executor); + metricsProxyUri = "http://localhost:" + config.metricsPort() + config.metricsApiPath(); + } + + @Override + protected Optional<HttpResponse> doHandle(URI requestUri, Path apiPath, String consumer) { + if (apiPath.matches(V2_PATH)) return Optional.of(resourceListResponse(requestUri, List.of(VALUES_PATH))); + if (apiPath.matches(VALUES_PATH)) return Optional.of(valuesResponse(consumer)); + return Optional.empty(); + } + + private JsonResponse valuesResponse(String consumer) { + try { + String uri = metricsProxyUri + consumerQuery(consumer); + String metricsJson = httpClient.execute(new HttpGet(uri), new BasicResponseHandler()); + return new JsonResponse(OK, metricsJson); + } catch (IOException e) { + log.warning("Unable to retrieve metrics from " + metricsProxyUri + ": " + Exceptions.toMessageString(e)); + return new ErrorResponse(INTERNAL_SERVER_ERROR, e.getMessage()); + } + } + + private static CloseableHttpClient createHttpClient() { + return VespaHttpClientBuilder.create(PoolingHttpClientConnectionManager::new) + .setUserAgent("application-metrics-retriever") + .setDefaultRequestConfig(RequestConfig.custom() + .setConnectTimeout(HTTP_CONNECT_TIMEOUT) + .setSocketTimeout(HTTP_SOCKET_TIMEOUT) + .build()) + .build(); + } + + static String consumerQuery(String consumer) { + return (consumer == null || consumer.isEmpty()) ? "" : "?consumer=" + consumer; + } +} |