summaryrefslogtreecommitdiffstats
path: root/metrics-proxy
diff options
context:
space:
mode:
authorBjørn Christian Seime <bjorncs@verizonmedia.com>2021-02-02 16:04:50 +0100
committerBjørn Christian Seime <bjorncs@verizonmedia.com>2021-02-02 16:04:50 +0100
commit3ab3ce18ca6c6ef721cba5c11fde3d987816f56f (patch)
treec2a7ff592f3f665b87f4ee4c9182b2bdd555b3cb /metrics-proxy
parent10a9d8ec4d23b0ec8d1fa58d1ba148258f5f2bcb (diff)
Remove usage of org.json
Diffstat (limited to 'metrics-proxy')
-rw-r--r--metrics-proxy/pom.xml5
-rw-r--r--metrics-proxy/src/main/java/ai/vespa/metricsproxy/node/NodeMetricGatherer.java36
-rw-r--r--metrics-proxy/src/main/java/ai/vespa/metricsproxy/service/RemoteHealthMetricFetcher.java16
-rw-r--r--metrics-proxy/src/main/java/ai/vespa/metricsproxy/service/RemoteMetricsFetcher.java55
-rw-r--r--metrics-proxy/src/test/java/ai/vespa/metricsproxy/http/application/ApplicationMetricsHandlerTest.java25
-rw-r--r--metrics-proxy/src/test/java/ai/vespa/metricsproxy/http/metrics/MetricsHandlerTestBase.java18
-rw-r--r--metrics-proxy/src/test/java/ai/vespa/metricsproxy/http/prometheus/PrometheusHandlerTest.java17
-rw-r--r--metrics-proxy/src/test/java/ai/vespa/metricsproxy/node/NodeMetricGathererTest.java19
-rw-r--r--metrics-proxy/src/test/java/ai/vespa/metricsproxy/rpc/RpcMetricsTest.java79
-rw-r--r--metrics-proxy/src/test/java/ai/vespa/metricsproxy/service/ContainerServiceTest.java3
10 files changed, 142 insertions, 131 deletions
diff --git a/metrics-proxy/pom.xml b/metrics-proxy/pom.xml
index 8bf5a30e584..90d9f093da1 100644
--- a/metrics-proxy/pom.xml
+++ b/metrics-proxy/pom.xml
@@ -101,11 +101,6 @@
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
- <dependency>
- <groupId>org.json</groupId>
- <artifactId>json</artifactId>
- <scope>provided</scope>
- </dependency>
<!-- compile scope -->
diff --git a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/node/NodeMetricGatherer.java b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/node/NodeMetricGatherer.java
index 3cd9f526387..0e0511967a3 100644
--- a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/node/NodeMetricGatherer.java
+++ b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/node/NodeMetricGatherer.java
@@ -9,13 +9,11 @@ import ai.vespa.metricsproxy.metric.model.MetricsPacket;
import ai.vespa.metricsproxy.metric.model.ServiceId;
import ai.vespa.metricsproxy.service.SystemPollerProvider;
import ai.vespa.metricsproxy.service.VespaServices;
+import com.fasterxml.jackson.databind.JsonNode;
import com.google.inject.Inject;
import com.yahoo.container.jdisc.state.CoredumpGatherer;
import com.yahoo.container.jdisc.state.FileWrapper;
import com.yahoo.container.jdisc.state.HostLifeGatherer;
-import com.yahoo.yolean.Exceptions;
-import org.json.JSONException;
-import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Iterator;
@@ -54,10 +52,10 @@ public class NodeMetricGatherer {
List<MetricsPacket.Builder> metricPacketBuilders = new ArrayList<>();
metricPacketBuilders.addAll(gatherServiceHealthMetrics(vespaServices));
- JSONObject coredumpPacket = CoredumpGatherer.gatherCoredumpMetrics(fileWrapper);
+ JsonNode coredumpPacket = CoredumpGatherer.gatherCoredumpMetrics(fileWrapper);
addObjectToBuilders(metricPacketBuilders, coredumpPacket);
if (SystemPollerProvider.runningOnLinux()) {
- JSONObject packet = HostLifeGatherer.getHostLifePacket(fileWrapper);
+ JsonNode packet = HostLifeGatherer.getHostLifePacket(fileWrapper);
addObjectToBuilders(metricPacketBuilders, packet);
}
@@ -69,24 +67,20 @@ public class NodeMetricGatherer {
).collect(Collectors.toList());
}
- protected static void addObjectToBuilders(List<MetricsPacket.Builder> builders, JSONObject object) {
- try {
- MetricsPacket.Builder builder = new MetricsPacket.Builder(ServiceId.toServiceId(object.getString("application")));
- builder.timestamp(object.getLong("timestamp"));
- if (object.has("status_code")) builder.statusCode(object.getInt("status_code"));
- if (object.has("status_msg")) builder.statusMessage(object.getString("status_msg"));
- if (object.has("metrics")) {
- JSONObject metrics = object.getJSONObject("metrics");
- Iterator<?> keys = metrics.keys();
- while(keys.hasNext()) {
- String key = (String) keys.next();
- builder.putMetric(MetricId.toMetricId(key), metrics.getLong(key));
- }
+ protected static void addObjectToBuilders(List<MetricsPacket.Builder> builders, JsonNode object) {
+ MetricsPacket.Builder builder = new MetricsPacket.Builder(ServiceId.toServiceId(object.get("application").textValue()));
+ builder.timestamp(object.get("timestamp").longValue());
+ if (object.has("status_code")) builder.statusCode(object.get("status_code").intValue());
+ if (object.has("status_msg")) builder.statusMessage(object.get("status_msg").textValue());
+ if (object.has("metrics")) {
+ JsonNode metrics = object.get("metrics");
+ Iterator<?> keys = metrics.fieldNames();
+ while(keys.hasNext()) {
+ String key = (String) keys.next();
+ builder.putMetric(MetricId.toMetricId(key), metrics.get(key).asLong());
}
- builders.add(builder);
- } catch (JSONException e) {
- Exceptions.toMessageString(e);
}
+ builders.add(builder);
}
}
diff --git a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/service/RemoteHealthMetricFetcher.java b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/service/RemoteHealthMetricFetcher.java
index 1cab1a859a9..827f513a418 100644
--- a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/service/RemoteHealthMetricFetcher.java
+++ b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/service/RemoteHealthMetricFetcher.java
@@ -2,9 +2,10 @@
package ai.vespa.metricsproxy.service;
import ai.vespa.metricsproxy.metric.HealthMetric;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
import java.util.logging.Level;
-import org.json.JSONException;
-import org.json.JSONObject;
import java.io.IOException;
import java.util.logging.Logger;
@@ -15,6 +16,7 @@ import java.util.logging.Logger;
* @author Jo Kristian Bergum
*/
public class RemoteHealthMetricFetcher extends HttpMetricFetcher {
+ private static final ObjectMapper jsonMapper = new ObjectMapper();
private final static Logger log = Logger.getLogger(RemoteHealthMetricFetcher.class.getPackage().getName());
private final static String HEALTH_PATH = STATE_PATH + "health";
@@ -54,16 +56,16 @@ public class RemoteHealthMetricFetcher extends HttpMetricFetcher {
return HealthMetric.getUnknown("Empty response from status page");
}
try {
- JSONObject o = new JSONObject(data);
- JSONObject status = o.getJSONObject("status");
- String code = status.getString("code");
+ JsonNode o = jsonMapper.readTree(data);
+ JsonNode status = o.get("status");
+ String code = status.get("code").asText();
String message = "";
if (status.has("message")) {
- message = status.getString("message");
+ message = status.get("message").textValue();
}
return HealthMetric.get(code, message);
- } catch (JSONException e) {
+ } catch (IOException e) {
log.log(Level.FINE, "Failed to parse json response from metrics page:" + e + ":" + data);
return HealthMetric.getUnknown("Not able to parse json from status page");
}
diff --git a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/service/RemoteMetricsFetcher.java b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/service/RemoteMetricsFetcher.java
index 442ebc0d38d..464f215edc4 100644
--- a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/service/RemoteMetricsFetcher.java
+++ b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/service/RemoteMetricsFetcher.java
@@ -4,9 +4,9 @@ package ai.vespa.metricsproxy.service;
import ai.vespa.metricsproxy.metric.Metric;
import ai.vespa.metricsproxy.metric.Metrics;
import ai.vespa.metricsproxy.metric.model.DimensionId;
-import org.json.JSONArray;
-import org.json.JSONException;
-import org.json.JSONObject;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
import java.io.IOException;
import java.util.Collections;
@@ -23,6 +23,8 @@ import static ai.vespa.metricsproxy.metric.model.DimensionId.toDimensionId;
*/
public class RemoteMetricsFetcher extends HttpMetricFetcher {
+ private static final ObjectMapper jsonMapper = new ObjectMapper();
+
final static String METRICS_PATH = STATE_PATH + "metrics";
RemoteMetricsFetcher(VespaService service, int port) {
@@ -57,21 +59,21 @@ public class RemoteMetricsFetcher extends HttpMetricFetcher {
return remoteMetrics;
}
- private Metrics parse(String data) throws JSONException {
- JSONObject o = new JSONObject(data);
+ private Metrics parse(String data) throws IOException {
+ JsonNode o = jsonMapper.readTree(data);
if (!(o.has("metrics"))) {
return new Metrics(); //empty
}
- JSONObject metrics = o.getJSONObject("metrics");
- JSONArray values;
+ JsonNode metrics = o.get("metrics");
+ ArrayNode values;
long timestamp;
try {
- JSONObject snapshot = metrics.getJSONObject("snapshot");
- timestamp = (long) snapshot.getDouble("to");
- values = metrics.getJSONArray("values");
- } catch (JSONException e) {
+ JsonNode snapshot = metrics.get("snapshot");
+ timestamp = snapshot.get("to").asLong();
+ values = (ArrayNode) metrics.get("values");
+ } catch (Exception e) {
// snapshot might not have been produced. Do not throw exception into log
return new Metrics();
}
@@ -81,29 +83,29 @@ public class RemoteMetricsFetcher extends HttpMetricFetcher {
Map<DimensionId, String> noDims = Collections.emptyMap();
Map<String, Map<DimensionId, String>> uniqueDimensions = new HashMap<>();
- for (int i = 0; i < values.length(); i++) {
- JSONObject metric = values.getJSONObject(i);
- String name = metric.getString("name");
+ for (int i = 0; i < values.size(); i++) {
+ JsonNode metric = values.get(i);
+ String name = metric.get("name").textValue();
String description = "";
if (metric.has("description")) {
- description = metric.getString("description");
+ description = metric.get("description").textValue();
}
Map<DimensionId, String> dim = noDims;
if (metric.has("dimensions")) {
- JSONObject dimensions = metric.getJSONObject("dimensions");
+ JsonNode dimensions = metric.get("dimensions");
StringBuilder sb = new StringBuilder();
- for (Iterator<?> it = dimensions.keys(); it.hasNext(); ) {
+ for (Iterator<?> it = dimensions.fieldNames(); it.hasNext(); ) {
String k = (String) it.next();
- String v = dimensions.getString(k);
+ String v = dimensions.get(k).asText();
sb.append(toDimensionId(k)).append(v);
}
if ( ! uniqueDimensions.containsKey(sb.toString())) {
dim = new HashMap<>();
- for (Iterator<?> it = dimensions.keys(); it.hasNext(); ) {
+ for (Iterator<?> it = dimensions.fieldNames(); it.hasNext(); ) {
String k = (String) it.next();
- String v = dimensions.getString(k);
+ String v = dimensions.get(k).textValue();
dim.put(toDimensionId(k), v);
}
uniqueDimensions.put(sb.toString(), Collections.unmodifiableMap(dim));
@@ -111,10 +113,17 @@ public class RemoteMetricsFetcher extends HttpMetricFetcher {
dim = uniqueDimensions.get(sb.toString());
}
- JSONObject aggregates = metric.getJSONObject("values");
- for (Iterator<?> it = aggregates.keys(); it.hasNext(); ) {
+ JsonNode aggregates = metric.get("values");
+ for (Iterator<?> it = aggregates.fieldNames(); it.hasNext(); ) {
String aggregator = (String) it.next();
- Number value = (Number) aggregates.get(aggregator);
+ JsonNode aggregatorValue = aggregates.get(aggregator);
+ if (aggregatorValue == null) {
+ throw new IllegalArgumentException("Value for aggregator '" + aggregator + "' is missing");
+ }
+ Number value = aggregatorValue.numberValue();
+ if (value == null) {
+ throw new IllegalArgumentException("Value for aggregator '" + aggregator + "' is not a number");
+ }
StringBuilder metricName = (new StringBuilder()).append(name).append(".").append(aggregator);
m.add(new Metric(metricName.toString(), value, timestamp, dim, description));
}
diff --git a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/http/application/ApplicationMetricsHandlerTest.java b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/http/application/ApplicationMetricsHandlerTest.java
index d7576718e8a..cf1eac3c691 100644
--- a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/http/application/ApplicationMetricsHandlerTest.java
+++ b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/http/application/ApplicationMetricsHandlerTest.java
@@ -8,13 +8,11 @@ import ai.vespa.metricsproxy.metric.model.json.GenericApplicationModel;
import ai.vespa.metricsproxy.metric.model.json.GenericJsonModel;
import ai.vespa.metricsproxy.metric.model.json.GenericMetrics;
import ai.vespa.metricsproxy.metric.model.json.GenericService;
+import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.yahoo.container.jdisc.RequestHandlerTestDriver;
-import java.util.regex.Pattern;
-
-import org.json.JSONArray;
-import org.json.JSONObject;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
@@ -24,6 +22,7 @@ import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.Executors;
+import java.util.regex.Pattern;
import static ai.vespa.metricsproxy.TestUtil.getFileContents;
import static ai.vespa.metricsproxy.http.ValuesFetcher.defaultMetricsConsumerId;
@@ -49,6 +48,8 @@ import static org.junit.Assert.fail;
@SuppressWarnings("UnstableApiUsage")
public class ApplicationMetricsHandlerTest {
+ private static final ObjectMapper jsonMapper = new ObjectMapper();
+
private static final String HOST = "localhost";
private static final String URI_BASE = "http://" + HOST;
private static final String METRICS_V1_URI = URI_BASE + METRICS_V1_PATH;
@@ -102,16 +103,16 @@ public class ApplicationMetricsHandlerTest {
@Test
public void v1_response_contains_values_uri() throws Exception {
String response = testDriver.sendRequest(METRICS_V1_URI).readAll();
- JSONObject root = new JSONObject(response);
+ JsonNode root = jsonMapper.readTree(response);
assertTrue(root.has("resources"));
- JSONArray resources = root.getJSONArray("resources");
- assertEquals(2, resources.length());
+ ArrayNode resources = (ArrayNode) root.get("resources");
+ assertEquals(2, resources.size());
- JSONObject valuesUrl = resources.getJSONObject(0);
- assertEquals(METRICS_VALUES_URI, valuesUrl.getString("url"));
- JSONObject prometheusUrl = resources.getJSONObject(1);
- assertEquals(PROMETHEUS_VALUES_URI, prometheusUrl.getString("url"));
+ JsonNode valuesUrl = resources.get(0);
+ assertEquals(METRICS_VALUES_URI, valuesUrl.get("url").textValue());
+ JsonNode prometheusUrl = resources.get(1);
+ assertEquals(PROMETHEUS_VALUES_URI, prometheusUrl.get("url").textValue());
}
@Ignore
@@ -199,7 +200,7 @@ public class ApplicationMetricsHandlerTest {
@Test
public void invalid_path_yields_error_response() throws Exception {
String response = testDriver.sendRequest(METRICS_V1_URI + "/invalid").readAll();
- JSONObject root = new JSONObject(response);
+ JsonNode root = jsonMapper.readTree(response);
assertTrue(root.has("error"));
}
diff --git a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/http/metrics/MetricsHandlerTestBase.java b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/http/metrics/MetricsHandlerTestBase.java
index 1c5ce695155..379ef04d38d 100644
--- a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/http/metrics/MetricsHandlerTestBase.java
+++ b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/http/metrics/MetricsHandlerTestBase.java
@@ -6,9 +6,9 @@ import ai.vespa.metricsproxy.metric.model.json.GenericJsonModel;
import ai.vespa.metricsproxy.metric.model.json.GenericMetrics;
import ai.vespa.metricsproxy.metric.model.json.GenericService;
import ai.vespa.metricsproxy.service.DownService;
+import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
-import org.json.JSONArray;
-import org.json.JSONObject;
+import com.fasterxml.jackson.databind.node.ArrayNode;
import org.junit.Ignore;
import org.junit.Test;
@@ -32,6 +32,8 @@ import static org.junit.Assert.fail;
*/
public abstract class MetricsHandlerTestBase<MODEL> extends HttpHandlerTestBase {
+ private static final ObjectMapper jsonMapper = new ObjectMapper();
+
static String rootUri;
static String valuesUri;
@@ -56,21 +58,21 @@ public abstract class MetricsHandlerTestBase<MODEL> extends HttpHandlerTestBase
@Test
public void invalid_path_yields_error_response() throws Exception {
String response = testDriver.sendRequest(rootUri + "/invalid").readAll();
- JSONObject root = new JSONObject(response);
+ JsonNode root = jsonMapper.readTree(response);
assertTrue(root.has("error"));
}
@Test
public void root_response_contains_values_uri() throws Exception {
String response = testDriver.sendRequest(rootUri).readAll();
- JSONObject root = new JSONObject(response);
+ JsonNode root = jsonMapper.readTree(response);
assertTrue(root.has("resources"));
- JSONArray resources = root.getJSONArray("resources");
- assertEquals(1, resources.length());
+ ArrayNode resources = (ArrayNode) root.get("resources");
+ assertEquals(1, resources.size());
- JSONObject valuesUrl = resources.getJSONObject(0);
- assertEquals(valuesUri, valuesUrl.getString("url"));
+ JsonNode valuesUrl = resources.get(0);
+ assertEquals(valuesUri, valuesUrl.get("url").textValue());
}
@Ignore
diff --git a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/http/prometheus/PrometheusHandlerTest.java b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/http/prometheus/PrometheusHandlerTest.java
index a224c4090b3..89186e63b93 100644
--- a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/http/prometheus/PrometheusHandlerTest.java
+++ b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/http/prometheus/PrometheusHandlerTest.java
@@ -3,9 +3,10 @@ package ai.vespa.metricsproxy.http.prometheus;
import ai.vespa.metricsproxy.http.HttpHandlerTestBase;
import ai.vespa.metricsproxy.service.DummyService;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
import com.yahoo.container.jdisc.RequestHandlerTestDriver;
-import org.json.JSONArray;
-import org.json.JSONObject;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
@@ -22,6 +23,8 @@ import static org.junit.Assert.assertTrue;
@SuppressWarnings("UnstableApiUsage")
public class PrometheusHandlerTest extends HttpHandlerTestBase {
+ private static final ObjectMapper jsonMapper = new ObjectMapper();
+
private static final String V1_URI = URI_BASE + PrometheusHandler.V1_PATH;
private static final String VALUES_URI = URI_BASE + PrometheusHandler.VALUES_PATH;
@@ -40,14 +43,14 @@ public class PrometheusHandlerTest extends HttpHandlerTestBase {
@Test
public void v1_response_contains_values_uri() throws Exception {
String response = testDriver.sendRequest(V1_URI).readAll();
- JSONObject root = new JSONObject(response);
+ JsonNode root = jsonMapper.readTree(response);
assertTrue(root.has("resources"));
- JSONArray resources = root.getJSONArray("resources");
- assertEquals(1, resources.length());
+ ArrayNode resources = (ArrayNode) root.get("resources");
+ assertEquals(1, resources.size());
- JSONObject valuesUrl = resources.getJSONObject(0);
- assertEquals(VALUES_URI, valuesUrl.getString("url"));
+ JsonNode valuesUrl = resources.get(0);
+ assertEquals(VALUES_URI, valuesUrl.get("url").textValue());
}
@Ignore
diff --git a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/node/NodeMetricGathererTest.java b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/node/NodeMetricGathererTest.java
index e2ad0ccd504..c2fc23a878d 100644
--- a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/node/NodeMetricGathererTest.java
+++ b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/node/NodeMetricGathererTest.java
@@ -3,8 +3,9 @@ package ai.vespa.metricsproxy.node;
import ai.vespa.metricsproxy.metric.model.MetricId;
import ai.vespa.metricsproxy.metric.model.MetricsPacket;
-import org.json.JSONException;
-import org.json.JSONObject;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
import org.junit.Test;
import java.util.ArrayList;
@@ -17,10 +18,12 @@ import static org.junit.Assert.assertEquals;
*/
public class NodeMetricGathererTest {
+ private static final ObjectMapper jsonMapper = new ObjectMapper();
+
@Test
- public void testJSONObjectIsCorrectlyConvertedToMetricsPacket() throws JSONException {
+ public void testJSONObjectIsCorrectlyConvertedToMetricsPacket() {
List<MetricsPacket.Builder> builders = new ArrayList<>();
- JSONObject hostLifePacket = generateHostLifePacket();
+ JsonNode hostLifePacket = generateHostLifePacket();
NodeMetricGatherer.addObjectToBuilders(builders, hostLifePacket);
MetricsPacket packet = builders.remove(0).build();
@@ -32,17 +35,17 @@ public class NodeMetricGathererTest {
assertEquals(1l, packet.metrics().get(MetricId.toMetricId("alive")));
}
- private JSONObject generateHostLifePacket() throws JSONException {
+ private JsonNode generateHostLifePacket() {
- JSONObject jsonObject = new JSONObject();
+ ObjectNode jsonObject = jsonMapper.createObjectNode();
jsonObject.put("status_code", 0);
jsonObject.put("status_msg", "OK");
jsonObject.put("timestamp", 123);
jsonObject.put("application", "host_life");
- JSONObject metrics = new JSONObject();
+ ObjectNode metrics = jsonMapper.createObjectNode();
metrics.put("uptime", 12);
metrics.put("alive", 1);
- jsonObject.put("metrics", metrics);
+ jsonObject.set("metrics", metrics);
return jsonObject;
}
}
diff --git a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/rpc/RpcMetricsTest.java b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/rpc/RpcMetricsTest.java
index 8d5bba77844..70970bfe8da 100644
--- a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/rpc/RpcMetricsTest.java
+++ b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/rpc/RpcMetricsTest.java
@@ -5,17 +5,18 @@ import ai.vespa.metricsproxy.metric.Metric;
import ai.vespa.metricsproxy.metric.Metrics;
import ai.vespa.metricsproxy.metric.model.ConsumerId;
import ai.vespa.metricsproxy.service.VespaService;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
import com.yahoo.jrt.Request;
import com.yahoo.jrt.Spec;
import com.yahoo.jrt.StringValue;
import com.yahoo.jrt.Supervisor;
import com.yahoo.jrt.Target;
import com.yahoo.jrt.Transport;
-import org.json.JSONArray;
-import org.json.JSONException;
-import org.json.JSONObject;
import org.junit.Test;
+import java.io.IOException;
import java.util.List;
import static ai.vespa.metricsproxy.TestUtil.getFileContents;
@@ -40,6 +41,8 @@ import static org.junit.Assert.assertTrue;
*/
public class RpcMetricsTest {
+ private static final ObjectMapper jsonMapper = new ObjectMapper();
+
private static final String METRICS_RESPONSE = getFileContents("metrics-storage-simple.json").trim();
private static final String EXTRA_APP = "extra";
@@ -67,9 +70,9 @@ public class RpcMetricsTest {
String allServicesResponse = getMetricsForYamas(ALL_SERVICES, rpcClient).trim();
// Verify that application is used as serviceId, and that metric exists.
- JSONObject extraMetrics = findExtraMetricsObject(allServicesResponse);
- assertThat(extraMetrics.getJSONObject("metrics").getInt("foo.count"), is(3));
- assertThat(extraMetrics.getJSONObject("dimensions").getString("role"), is("extra-role"));
+ JsonNode extraMetrics = findExtraMetricsObject(allServicesResponse);
+ assertThat(extraMetrics.get("metrics").get("foo.count").intValue(), is(3));
+ assertThat(extraMetrics.get("dimensions").get("role").textValue(), is("extra-role"));
}
}
}
@@ -85,7 +88,7 @@ public class RpcMetricsTest {
// Verify that no extra metrics exists
String allServicesResponse = getMetricsForYamas(ALL_SERVICES, rpcClient).trim();
- JSONObject extraMetrics = findExtraMetricsObject(allServicesResponse);
+ JsonNode extraMetrics = findExtraMetricsObject(allServicesResponse);
assertEquals(extraMetrics.toString(), "{}");
}
}
@@ -130,28 +133,28 @@ public class RpcMetricsTest {
}
}
- private static void verifyMetricsFromRpcRequest(VespaService service, RpcClient client) throws JSONException {
+ private static void verifyMetricsFromRpcRequest(VespaService service, RpcClient client) throws IOException {
String jsonResponse = getMetricsForYamas(service.getMonitoringName(), client).trim();
- JSONArray metrics = new JSONObject(jsonResponse).getJSONArray("metrics");
- assertThat("Expected 3 metric messages", metrics.length(), is(3));
- for (int i = 0; i < metrics.length() - 1; i++) { // The last "metric message" contains only status code/message
- JSONObject jsonObject = metrics.getJSONObject(i);
+ ArrayNode metrics = (ArrayNode) jsonMapper.readTree(jsonResponse).get("metrics");
+ assertThat("Expected 3 metric messages", metrics.size(), is(3));
+ for (int i = 0; i < metrics.size() - 1; i++) { // The last "metric message" contains only status code/message
+ JsonNode jsonObject = metrics.get(i);
assertFalse(jsonObject.has("status_code"));
assertFalse(jsonObject.has("status_msg"));
- assertThat(jsonObject.getJSONObject("dimensions").getString("foo"), is("bar"));
- assertThat(jsonObject.getJSONObject("dimensions").getString("bar"), is("foo"));
- assertThat(jsonObject.getJSONObject("dimensions").getString("serviceDim"), is("serviceDimValue"));
- assertThat(jsonObject.getJSONObject("routing").getJSONObject("yamas").getJSONArray("namespaces").length(), is(1));
- if (jsonObject.getJSONObject("metrics").has("foo_count")) {
- assertThat(jsonObject.getJSONObject("metrics").getInt("foo_count"), is(1));
- assertThat(jsonObject.getJSONObject("routing").getJSONObject("yamas").getJSONArray("namespaces").get(0), is(vespaMetricsConsumerId.id));
+ assertThat(jsonObject.get("dimensions").get("foo").textValue(), is("bar"));
+ assertThat(jsonObject.get("dimensions").get("bar").textValue(), is("foo"));
+ assertThat(jsonObject.get("dimensions").get("serviceDim").textValue(), is("serviceDimValue"));
+ assertThat(jsonObject.get("routing").get("yamas").get("namespaces").size(), is(1));
+ if (jsonObject.get("metrics").has("foo_count")) {
+ assertThat(jsonObject.get("metrics").get("foo_count").intValue(), is(1));
+ assertThat(jsonObject.get("routing").get("yamas").get("namespaces").get(0).textValue(), is(vespaMetricsConsumerId.id));
} else {
- assertThat(jsonObject.getJSONObject("metrics").getInt("foo.count"), is(1));
- assertThat(jsonObject.getJSONObject("routing").getJSONObject("yamas").getJSONArray("namespaces").get(0), is(CUSTOM_CONSUMER_ID.id));
+ assertThat(jsonObject.get("metrics").get("foo.count").intValue(), is(1));
+ assertThat(jsonObject.get("routing").get("yamas").get("namespaces").get(0).textValue(), is(CUSTOM_CONSUMER_ID.id));
}
}
- verifyStatusMessage(metrics.getJSONObject(metrics.length() - 1));
+ verifyStatusMessage(metrics.get(metrics.size() - 1));
}
private void verfiyMetricsFromServiceObject(VespaService service) {
@@ -166,15 +169,15 @@ public class RpcMetricsTest {
assertThat("Metric foo did not contain correct dimension for key = bar", foo.getDimensions().get(toDimensionId("bar")), is("foo"));
}
- private void verifyMetricsFromRpcRequestForAllServices(RpcClient client) throws JSONException {
+ private void verifyMetricsFromRpcRequestForAllServices(RpcClient client) throws IOException {
// Verify that metrics for all services can be retrieved in one request.
String allServicesResponse = getMetricsForYamas(ALL_SERVICES, client).trim();
- JSONArray allServicesMetrics = new JSONObject(allServicesResponse).getJSONArray("metrics");
- assertThat(allServicesMetrics.length(), is(5));
+ ArrayNode allServicesMetrics = (ArrayNode) jsonMapper.readTree(allServicesResponse).get("metrics");
+ assertThat(allServicesMetrics.size(), is(5));
}
@Test
- public void testGetAllMetricNames() throws Exception {
+ public void testGetAllMetricNames() {
try (IntegrationTester tester = new IntegrationTester()) {
tester.httpServer().setResponse(METRICS_RESPONSE);
@@ -205,14 +208,14 @@ public class RpcMetricsTest {
invoke(req, rpcClient, false);
}
- private JSONObject findExtraMetricsObject(String jsonResponse) throws JSONException {
- JSONArray metrics = new JSONObject(jsonResponse).getJSONArray("metrics");
- for (int i = 0; i < metrics.length(); i++) {
- JSONObject jsonObject = metrics.getJSONObject(i);
+ private JsonNode findExtraMetricsObject(String jsonResponse) throws IOException {
+ ArrayNode metrics = (ArrayNode) jsonMapper.readTree(jsonResponse).get("metrics");
+ for (int i = 0; i < metrics.size(); i++) {
+ JsonNode jsonObject = metrics.get(i);
assertTrue(jsonObject.has("application"));
- if (jsonObject.getString("application").equals(EXTRA_APP)) return jsonObject;
+ if (jsonObject.get("application").textValue().equals(EXTRA_APP)) return jsonObject;
}
- return new JSONObject();
+ return jsonMapper.createObjectNode();
}
private static String getMetricsForYamas(String service, RpcClient client) {
@@ -250,12 +253,12 @@ public class RpcMetricsTest {
return returnValue;
}
- private static void verifyStatusMessage(JSONObject jsonObject) throws JSONException {
- assertThat(jsonObject.getInt("status_code"), is(0));
- assertThat(jsonObject.getString("status_msg"), notNullValue());
- assertThat(jsonObject.getString("application"), notNullValue());
- assertThat(jsonObject.getString("routing"), notNullValue());
- assertThat(jsonObject.length(), is(4));
+ private static void verifyStatusMessage(JsonNode jsonObject) {
+ assertThat(jsonObject.get("status_code").intValue(), is(0));
+ assertThat(jsonObject.get("status_msg").textValue(), notNullValue());
+ assertThat(jsonObject.get("application").textValue(), notNullValue());
+ assertThat(jsonObject.get("routing"), notNullValue());
+ assertThat(jsonObject.size(), is(4));
}
}
diff --git a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/service/ContainerServiceTest.java b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/service/ContainerServiceTest.java
index 0d53f988ac7..7ff179e5528 100644
--- a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/service/ContainerServiceTest.java
+++ b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/service/ContainerServiceTest.java
@@ -2,7 +2,6 @@
package ai.vespa.metricsproxy.service;
import ai.vespa.metricsproxy.metric.Metric;
-import org.json.JSONException;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
@@ -37,7 +36,7 @@ public class ContainerServiceTest {
}
@Test
- public void testMultipleQueryDimensions() throws JSONException {
+ public void testMultipleQueryDimensions() {
int count = 0;
VespaService service = VespaService.create("service1", "id", httpServer.port());
for (Metric m : service.getMetrics().getMetrics()) {