summaryrefslogtreecommitdiffstats
path: root/container-core
diff options
context:
space:
mode:
authorAmund Bergland Kvalsvik <akvalsvik@verizonmedia.com>2020-06-26 12:01:42 +0200
committerAmund Bergland Kvalsvik <akvalsvik@verizonmedia.com>2020-06-26 12:01:42 +0200
commit2132ab285831f368f204210be8de409714573df3 (patch)
treef749c2b6540578564140b4cd8b99867c24cc425c /container-core
parent17d9107ee01a679ccbe591efbff959f603baebdc (diff)
Updated ApplicationMetricsHandler. Removed PrometheusV1Handler
Diffstat (limited to 'container-core')
-rw-r--r--container-core/src/main/java/com/yahoo/container/handler/metrics/PrometheusV1Handler.java90
-rw-r--r--container-core/src/test/java/com/yahoo/container/handler/metrics/PrometheusV1HandlerTest.java116
2 files changed, 0 insertions, 206 deletions
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
deleted file mode 100644
index 46bdb388cd1..00000000000
--- a/container-core/src/main/java/com/yahoo/container/handler/metrics/PrometheusV1Handler.java
+++ /dev/null
@@ -1,90 +0,0 @@
-package com.yahoo.container.handler.metrics;
-
-import ai.vespa.metricsproxy.http.TextResponse;
-import ai.vespa.metricsproxy.metric.model.MetricsPacket;
-import ai.vespa.metricsproxy.metric.model.json.GenericJsonUtil;
-import ai.vespa.metricsproxy.metric.model.prometheus.PrometheusUtil;
-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.restapi.StringResponse;
-import com.yahoo.vespa.jdk8compat.List;
-import com.yahoo.yolean.Exceptions;
-import java.io.IOException;
-import java.net.URI;
-import java.util.Optional;
-import java.util.concurrent.Executor;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
-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.json.JSONException;
-import org.json.JSONObject;
-
-import static com.yahoo.jdisc.Response.Status.INTERNAL_SERVER_ERROR;
-
-public class PrometheusV1Handler extends HttpHandlerBase {
-
- public static final String V1_PATH = "/prometheus/v1";
- static final String VALUES_PATH = V1_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
- protected PrometheusV1Handler(Executor executor, MetricsProxyApiConfig config) {
- super(executor);
- metricsProxyUri = "http://localhost:+" + config.metricsPort() + config.metricsApiPath();
- }
-
- private static CloseableHttpClient createHttpClient() {
- return VespaHttpClientBuilder.create()
- .setUserAgent("application-prometheus-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;
- }
-
- @Override
- protected Optional<HttpResponse> doHandle(URI requestUri, Path apiPath, String consumer) {
- if (apiPath.matches(V1_PATH)) return Optional.of(resourceListResponse(requestUri, List.of(VALUES_PATH)));
- if (apiPath.matches(VALUES_PATH)) return Optional.of(valuesResponse(consumer));
- return Optional.empty();
- }
-
- private HttpResponse valuesResponse(String consumer) {
- try {
- String uri = metricsProxyUri + consumerQuery(consumer);
- String metricsJson = httpClient.execute(new HttpGet(uri), new BasicResponseHandler());
- return new TextResponse(200, convertToPrometheus(metricsJson));
- } catch (IOException e) {
- log.warning(String.format("Unable to retrieve prometheus metrics from %s: %s", metricsProxyUri, Exceptions.toMessageString(e)));
- return new ErrorResponse(INTERNAL_SERVER_ERROR, e.getMessage());
- }
- }
-
- private String convertToPrometheus(String metricsJson) {
- return PrometheusUtil.toPrometheusModel(
- GenericJsonUtil
- .toMetricsPackets(metricsJson)
- .stream()
- .map(MetricsPacket.Builder::build)
- .collect(Collectors.toList()))
- .serialize();
- }
-
-}
diff --git a/container-core/src/test/java/com/yahoo/container/handler/metrics/PrometheusV1HandlerTest.java b/container-core/src/test/java/com/yahoo/container/handler/metrics/PrometheusV1HandlerTest.java
deleted file mode 100644
index ece2e37085c..00000000000
--- a/container-core/src/test/java/com/yahoo/container/handler/metrics/PrometheusV1HandlerTest.java
+++ /dev/null
@@ -1,116 +0,0 @@
-package com.yahoo.container.handler.metrics;
-
-import com.github.tomakehurst.wiremock.junit.WireMockRule;
-import com.yahoo.container.jdisc.RequestHandlerTestDriver;
-import java.io.BufferedReader;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.util.concurrent.Executors;
-import java.util.stream.Collectors;
-import org.json.JSONArray;
-import org.json.JSONException;
-import org.json.JSONObject;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-
-import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
-import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
-import static com.github.tomakehurst.wiremock.client.WireMock.get;
-import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
-import static com.yahoo.container.handler.metrics.PrometheusV1Handler.consumerQuery;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-
-public class PrometheusV1HandlerTest {
-
- private static final String URI_BASE = "http://localhost";
-
- private static final String V1_URI = URI_BASE + PrometheusV1Handler.V1_PATH;
- private static final String VALUES_URI = URI_BASE + PrometheusV1Handler.VALUES_PATH;
-
- // Mock applicationmetrics api
- private static final String MOCK_METRICS_PATH = "/node0";
-
- private static final String TEST_FILE = "application-metrics.json";
- private static final String RESPONSE = getFileContents(TEST_FILE);
- private static final String CPU_METRIC = "cpu";
- private static final String REPLACED_CPU_METRIC = "cpu.util";
- private static final String CUSTOM_CONSUMER = "custom-consumer";
-
- private static RequestHandlerTestDriver testDriver;
-
- @Rule
- public WireMockRule wireMockRule = new WireMockRule(80);
-
- private static String getFileContents(String filename) {
- InputStream in = PrometheusV1HandlerTest.class.getClassLoader().getResourceAsStream(filename);
- if (in == null) {
- throw new RuntimeException("File not found: " + filename);
- }
- return new BufferedReader(new InputStreamReader(in)).lines().collect(Collectors.joining("\n"));
- }
-
- @Before
- public void setup() {
- setupWireMock();
- var handler = new PrometheusV1Handler(Executors.newSingleThreadExecutor(),
- new MetricsProxyApiConfig.Builder()
- .metricsPort(wireMockRule.port())
- .metricsApiPath(MOCK_METRICS_PATH)
- .build());
- testDriver = new RequestHandlerTestDriver(handler);
- }
-
- private void setupWireMock() {
- wireMockRule.stubFor(get(urlPathEqualTo(MOCK_METRICS_PATH))
- .willReturn(aResponse().withBody(RESPONSE)));
-
- // Add a slightly different response for a custom consumer.
- String myConsumerResponse = RESPONSE.replace(CPU_METRIC, REPLACED_CPU_METRIC);
- wireMockRule.stubFor(get(urlPathEqualTo(MOCK_METRICS_PATH))
- .withQueryParam("consumer", equalTo(CUSTOM_CONSUMER))
- .willReturn(aResponse().withBody(myConsumerResponse)));
-
- }
-
- @Test
- public void v1_response_contains_values_uri() throws Exception {
- String response = testDriver.sendRequest(V1_URI).readAll();
- JSONObject root = new JSONObject(response);
- assertTrue(root.has("resources"));
-
- JSONArray resources = root.getJSONArray("resources");
- assertEquals(1, resources.length());
-
- JSONObject valuesUri = resources.getJSONObject(0);
- assertEquals(VALUES_URI, valuesUri.getString("url"));
- }
-
- @Test
- public void invalid_path_yields_error_response() throws Exception {
- String response = testDriver.sendRequest(V1_URI + "/invalid").readAll();
- JSONObject root = new JSONObject(response);
- assertTrue(root.has("error"));
- assertTrue(root.getString("error").startsWith("No content"));
- }
-
- @Test
- public void values_response_is_equal_to_test_file() {
- String response = testDriver.sendRequest(VALUES_URI).readAll();
- assertEquals(RESPONSE, response);
- }
-
- @Test
- public void consumer_is_propagated_to_metrics_proxy_api() {
- String response = getResponseAsString(CUSTOM_CONSUMER);
-
- assertTrue(response.contains(REPLACED_CPU_METRIC));
-
-
- }
-
- private String getResponseAsString(String consumer) {
- return testDriver.sendRequest(VALUES_URI + consumerQuery(consumer)).readAll();
- }
-}