summaryrefslogtreecommitdiffstats
path: root/metrics-proxy
diff options
context:
space:
mode:
authorgjoranv <gv@verizonmedia.com>2020-01-23 12:39:19 +0100
committergjoranv <gv@verizonmedia.com>2020-01-23 12:40:04 +0100
commit6be753f4021e5ef8ece22d19898dbb18e0005bf4 (patch)
tree4bd83c8464b07d6234955964f37a2fb38637d895 /metrics-proxy
parentb492c6d6b5f46bc484d249ef5aa3768d7dd91c71 (diff)
Move metrics handler utilities to new package in container-core
Diffstat (limited to 'metrics-proxy')
-rw-r--r--metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/ErrorResponse.java32
-rw-r--r--metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/HttpHandlerBase.java78
-rw-r--r--metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/JsonResponse.java30
-rw-r--r--metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/application/ApplicationMetricsHandler.java6
-rw-r--r--metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/metrics/MetricsV1Handler.java6
-rw-r--r--metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/prometheus/PrometheusHandler.java2
-rw-r--r--metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/yamas/YamasHandler.java8
-rw-r--r--metrics-proxy/src/test/java/ai/vespa/metricsproxy/http/ErrorResponseTest.java25
8 files changed, 11 insertions, 176 deletions
diff --git a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/ErrorResponse.java b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/ErrorResponse.java
deleted file mode 100644
index 9bd30d287d4..00000000000
--- a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/ErrorResponse.java
+++ /dev/null
@@ -1,32 +0,0 @@
-// Copyright 2020 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package ai.vespa.metricsproxy.http;
-
-import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.databind.ObjectMapper;
-
-import java.util.Map;
-import java.util.logging.Logger;
-
-import static java.util.logging.Level.WARNING;
-
-/**
- * @author gjoranv
- */
-public class ErrorResponse extends JsonResponse {
- private static Logger log = Logger.getLogger(ErrorResponse.class.getName());
-
- private static ObjectMapper objectMapper = new ObjectMapper();
-
- public ErrorResponse(int code, String message) {
- super(code, asErrorJson(message));
- }
-
- static String asErrorJson(String message) {
- try {
- return objectMapper.writeValueAsString(Map.of("error", message));
- } catch (JsonProcessingException e) {
- log.log(WARNING, "Could not encode error message to json:", e);
- return "Could not encode error message to json, check the log for details.";
- }
- }
-}
diff --git a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/HttpHandlerBase.java b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/HttpHandlerBase.java
deleted file mode 100644
index aa82a921e1a..00000000000
--- a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/HttpHandlerBase.java
+++ /dev/null
@@ -1,78 +0,0 @@
-// Copyright 2020 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package ai.vespa.metricsproxy.http;
-
-import com.yahoo.container.jdisc.HttpRequest;
-import com.yahoo.container.jdisc.HttpResponse;
-import com.yahoo.container.jdisc.ThreadedHttpRequestHandler;
-import com.yahoo.restapi.Path;
-import org.json.JSONArray;
-import org.json.JSONException;
-import org.json.JSONObject;
-
-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.METHOD_NOT_ALLOWED;
-import static com.yahoo.jdisc.Response.Status.NOT_FOUND;
-import static com.yahoo.jdisc.Response.Status.OK;
-import static com.yahoo.jdisc.http.HttpRequest.Method.GET;
-import static java.util.logging.Level.WARNING;
-
-/**
- * @author gjoranv
- */
-public abstract class HttpHandlerBase extends ThreadedHttpRequestHandler {
-
- protected HttpHandlerBase(Executor executor) {
- super(executor);
- }
-
- protected abstract Optional<HttpResponse> doHandle(URI requestUri, Path apiPath, String consumer);
-
- @Override
- public final HttpResponse handle(HttpRequest request) {
- if (request.getMethod() != GET) return new JsonResponse(METHOD_NOT_ALLOWED, "Only GET is supported");
-
- Path path = new Path(request.getUri());
-
- return doHandle(request.getUri(), path, getConsumer(request))
- .orElse(new ErrorResponse(NOT_FOUND, "No content at given path"));
- }
-
- private String getConsumer(HttpRequest request) {
- return request.getProperty("consumer");
- }
-
- protected JsonResponse resourceListResponse(URI requestUri, List<String> resources) {
- try {
- return new JsonResponse(OK, resourceList(requestUri, resources));
- } catch (JSONException e) {
- log.log(WARNING, "Bad JSON construction in generated resource list for " + requestUri.getPath(), e);
- return new ErrorResponse(INTERNAL_SERVER_ERROR,
- "An error occurred when generating the list of api resources.");
- }
- }
-
- // TODO: Use jackson with a "Resources" class instead of JSONObject
- private static String resourceList(URI requestUri, List<String> resources) throws JSONException {
- int port = requestUri.getPort();
- String host = requestUri.getHost();
- StringBuilder base = new StringBuilder("http://");
- base.append(host);
- if (port >= 0) {
- base.append(":").append(port);
- }
- String uriBase = base.toString();
- JSONArray linkList = new JSONArray();
- for (String api : resources) {
- JSONObject resource = new JSONObject();
- resource.put("url", uriBase + api);
- linkList.put(resource);
- }
- return new JSONObject().put("resources", linkList).toString(4);
- }
-
-}
diff --git a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/JsonResponse.java b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/JsonResponse.java
deleted file mode 100644
index 9de5933bd1f..00000000000
--- a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/JsonResponse.java
+++ /dev/null
@@ -1,30 +0,0 @@
-// Copyright 2020 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package ai.vespa.metricsproxy.http;
-
-import com.yahoo.container.jdisc.HttpResponse;
-
-import java.io.IOException;
-import java.io.OutputStream;
-import java.nio.charset.Charset;
-
-/**
- * @author gjoranv
- */
-public class JsonResponse extends HttpResponse {
- private final byte[] data;
-
- public JsonResponse(int code, String data) {
- super(code);
- this.data = data.getBytes(Charset.forName(DEFAULT_CHARACTER_ENCODING));
- }
-
- @Override
- public String getContentType() {
- return "application/json";
- }
-
- @Override
- public void render(OutputStream outputStream) throws IOException {
- outputStream.write(data);
- }
-}
diff --git a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/application/ApplicationMetricsHandler.java b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/application/ApplicationMetricsHandler.java
index ff6eb4d80b3..af37590a09d 100644
--- a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/application/ApplicationMetricsHandler.java
+++ b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/application/ApplicationMetricsHandler.java
@@ -3,13 +3,13 @@
package ai.vespa.metricsproxy.http.application;
import ai.vespa.metricsproxy.core.MetricsConsumers;
-import ai.vespa.metricsproxy.http.ErrorResponse;
-import ai.vespa.metricsproxy.http.HttpHandlerBase;
-import ai.vespa.metricsproxy.http.JsonResponse;
import ai.vespa.metricsproxy.metric.model.ConsumerId;
import ai.vespa.metricsproxy.metric.model.MetricsPacket;
import ai.vespa.metricsproxy.metric.model.processing.MetricsProcessor;
import com.google.inject.Inject;
+import com.yahoo.container.handler.metrics.ErrorResponse;
+import com.yahoo.container.handler.metrics.HttpHandlerBase;
+import com.yahoo.container.handler.metrics.JsonResponse;
import com.yahoo.container.jdisc.HttpResponse;
import com.yahoo.restapi.Path;
diff --git a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/metrics/MetricsV1Handler.java b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/metrics/MetricsV1Handler.java
index 28a24c5dc25..bf4d7b55989 100644
--- a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/metrics/MetricsV1Handler.java
+++ b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/metrics/MetricsV1Handler.java
@@ -3,13 +3,13 @@ package ai.vespa.metricsproxy.http.metrics;
import ai.vespa.metricsproxy.core.MetricsConsumers;
import ai.vespa.metricsproxy.core.MetricsManager;
-import ai.vespa.metricsproxy.http.ErrorResponse;
-import ai.vespa.metricsproxy.http.HttpHandlerBase;
-import ai.vespa.metricsproxy.http.JsonResponse;
import ai.vespa.metricsproxy.http.ValuesFetcher;
import ai.vespa.metricsproxy.metric.model.MetricsPacket;
import ai.vespa.metricsproxy.service.VespaServices;
import com.google.inject.Inject;
+import com.yahoo.container.handler.metrics.ErrorResponse;
+import com.yahoo.container.handler.metrics.HttpHandlerBase;
+import com.yahoo.container.handler.metrics.JsonResponse;
import com.yahoo.container.jdisc.HttpResponse;
import com.yahoo.restapi.Path;
diff --git a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/prometheus/PrometheusHandler.java b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/prometheus/PrometheusHandler.java
index 5f3723df94d..eeabb1e03ac 100644
--- a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/prometheus/PrometheusHandler.java
+++ b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/prometheus/PrometheusHandler.java
@@ -3,12 +3,12 @@ package ai.vespa.metricsproxy.http.prometheus;
import ai.vespa.metricsproxy.core.MetricsConsumers;
import ai.vespa.metricsproxy.core.MetricsManager;
-import ai.vespa.metricsproxy.http.HttpHandlerBase;
import ai.vespa.metricsproxy.http.TextResponse;
import ai.vespa.metricsproxy.http.ValuesFetcher;
import ai.vespa.metricsproxy.metric.model.MetricsPacket;
import ai.vespa.metricsproxy.service.VespaServices;
import com.google.inject.Inject;
+import com.yahoo.container.handler.metrics.HttpHandlerBase;
import com.yahoo.container.jdisc.HttpResponse;
import com.yahoo.restapi.Path;
diff --git a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/yamas/YamasHandler.java b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/yamas/YamasHandler.java
index 38011d089d4..2b22ea101d8 100644
--- a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/yamas/YamasHandler.java
+++ b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/yamas/YamasHandler.java
@@ -4,17 +4,17 @@ package ai.vespa.metricsproxy.http.yamas;
import ai.vespa.metricsproxy.core.MetricsConsumers;
import ai.vespa.metricsproxy.core.MetricsManager;
import ai.vespa.metricsproxy.http.ValuesFetcher;
-import ai.vespa.metricsproxy.node.NodeMetricGatherer;
-import ai.vespa.metricsproxy.http.ErrorResponse;
-import ai.vespa.metricsproxy.http.HttpHandlerBase;
-import ai.vespa.metricsproxy.http.JsonResponse;
import ai.vespa.metricsproxy.metric.dimensions.ApplicationDimensions;
import ai.vespa.metricsproxy.metric.dimensions.NodeDimensions;
import ai.vespa.metricsproxy.metric.model.MetricsPacket;
import ai.vespa.metricsproxy.metric.model.json.JsonRenderingException;
import ai.vespa.metricsproxy.metric.model.json.YamasJsonUtil;
+import ai.vespa.metricsproxy.node.NodeMetricGatherer;
import ai.vespa.metricsproxy.service.VespaServices;
import com.google.inject.Inject;
+import com.yahoo.container.handler.metrics.ErrorResponse;
+import com.yahoo.container.handler.metrics.HttpHandlerBase;
+import com.yahoo.container.handler.metrics.JsonResponse;
import com.yahoo.container.jdisc.HttpResponse;
import com.yahoo.restapi.Path;
diff --git a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/http/ErrorResponseTest.java b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/http/ErrorResponseTest.java
deleted file mode 100644
index ccf04560906..00000000000
--- a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/http/ErrorResponseTest.java
+++ /dev/null
@@ -1,25 +0,0 @@
-// Copyright 2020 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package ai.vespa.metricsproxy.http;
-
-import org.junit.Test;
-
-import static org.junit.Assert.assertEquals;
-
-/**
- * @author gjoranv
- */
-public class ErrorResponseTest {
-
- @Test
- public void error_message_is_wrapped_in_json_object() {
- var json = ErrorResponse.asErrorJson("bad");
- assertEquals("{\"error\":\"bad\"}", json);
- }
-
- @Test
- public void quotes_are_escaped() {
- var json = ErrorResponse.asErrorJson("Message \" with \" embedded quotes.");
- assertEquals("{\"error\":\"Message \\\" with \\\" embedded quotes.\"}", json);
- }
-
-}