diff options
author | Bjørn Christian Seime <bjorn.christian@seime.no> | 2021-02-01 23:51:00 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-01 23:51:00 +0100 |
commit | 42794e42e8ce223705e0a8e0e3e9293d65adaf6c (patch) | |
tree | a337188686fe568fd0dc04db5234c7ffa2ccbb63 /container-core/src/main/java/com/yahoo/container/jdisc/state/MetricsPacketsHandler.java | |
parent | 7f04da806204b7727f377b4099bd10571c61e5da (diff) |
Revert "Remove org.json usage [run-systemtest]"
Diffstat (limited to 'container-core/src/main/java/com/yahoo/container/jdisc/state/MetricsPacketsHandler.java')
-rw-r--r-- | container-core/src/main/java/com/yahoo/container/jdisc/state/MetricsPacketsHandler.java | 61 |
1 files changed, 28 insertions, 33 deletions
diff --git a/container-core/src/main/java/com/yahoo/container/jdisc/state/MetricsPacketsHandler.java b/container-core/src/main/java/com/yahoo/container/jdisc/state/MetricsPacketsHandler.java index e8d829eafb1..3d3f0e4b677 100644 --- a/container-core/src/main/java/com/yahoo/container/jdisc/state/MetricsPacketsHandler.java +++ b/container-core/src/main/java/com/yahoo/container/jdisc/state/MetricsPacketsHandler.java @@ -1,11 +1,6 @@ // Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.container.jdisc.state; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ArrayNode; -import com.fasterxml.jackson.databind.node.ObjectNode; import com.google.inject.Inject; import com.yahoo.collections.Tuple2; import com.yahoo.component.provider.ComponentRegistry; @@ -18,6 +13,9 @@ import com.yahoo.jdisc.handler.ResponseDispatch; import com.yahoo.jdisc.handler.ResponseHandler; import com.yahoo.jdisc.http.HttpHeaders; import com.yahoo.metrics.MetricsPresentationConfig; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; @@ -46,8 +44,6 @@ import static com.yahoo.container.jdisc.state.StateHandler.getSnapshotPreprocess */ public class MetricsPacketsHandler extends AbstractRequestHandler { - private static final ObjectMapper jsonMapper = new ObjectMapper(); - static final String APPLICATION_KEY = "application"; static final String TIMESTAMP_KEY = "timestamp"; static final String STATUS_CODE_KEY = "status_code"; @@ -101,19 +97,19 @@ public class MetricsPacketsHandler extends AbstractRequestHandler { } String output = jsonToString(getStatusPacket()) + getAllMetricsPackets() + "\n"; return output.getBytes(StandardCharsets.UTF_8); - } catch (JsonProcessingException e) { + } catch (JSONException e) { throw new RuntimeException("Bad JSON construction.", e); } } - private byte[] getMetricsArray() throws JsonProcessingException { - ObjectNode root = jsonMapper.createObjectNode(); - ArrayNode jsonArray = jsonMapper.createArrayNode(); - jsonArray.add(getStatusPacket()); + private byte[] getMetricsArray() throws JSONException { + JSONObject root = new JSONObject(); + JSONArray jsonArray = new JSONArray(); + jsonArray.put(getStatusPacket()); getPacketsForSnapshot(getSnapshot(), applicationName, timer.currentTimeMillis()) - .forEach(jsonArray::add); - MetricGatherer.getAdditionalMetrics().forEach(jsonArray::add); - root.set("metrics", jsonArray); + .forEach(jsonArray::put); + MetricGatherer.getAdditionalMetrics().forEach(jsonArray::put); + root.put("metrics", jsonArray); return jsonToString(root) .getBytes(StandardCharsets.UTF_8); } @@ -121,8 +117,8 @@ public class MetricsPacketsHandler extends AbstractRequestHandler { /** * Exactly one status packet is added to the response. */ - private JsonNode getStatusPacket() { - ObjectNode packet = jsonMapper.createObjectNode(); + private JSONObject getStatusPacket() throws JSONException { + JSONObject packet = new JSONObjectWithLegibleException(); packet.put(APPLICATION_KEY, applicationName); StateMonitor.Status status = monitor.status(); @@ -131,15 +127,14 @@ public class MetricsPacketsHandler extends AbstractRequestHandler { return packet; } - private static String jsonToString(JsonNode jsonObject) throws JsonProcessingException { - return jsonMapper.writerWithDefaultPrettyPrinter() - .writeValueAsString(jsonObject); + private String jsonToString(JSONObject jsonObject) throws JSONException { + return jsonObject.toString(4); } - private String getAllMetricsPackets() throws JsonProcessingException { + private String getAllMetricsPackets() throws JSONException { StringBuilder ret = new StringBuilder(); - List<JsonNode> metricsPackets = getPacketsForSnapshot(getSnapshot(), applicationName, timer.currentTimeMillis()); - for (JsonNode packet : metricsPackets) { + List<JSONObject> metricsPackets = getPacketsForSnapshot(getSnapshot(), applicationName, timer.currentTimeMillis()); + for (JSONObject packet : metricsPackets) { ret.append(PACKET_SEPARATOR); // For legibility and parsing in unit tests ret.append(jsonToString(packet)); } @@ -155,16 +150,16 @@ public class MetricsPacketsHandler extends AbstractRequestHandler { } } - private List<JsonNode> getPacketsForSnapshot(MetricSnapshot metricSnapshot, String application, long timestamp) { + private List<JSONObject> getPacketsForSnapshot(MetricSnapshot metricSnapshot, String application, long timestamp) throws JSONException { if (metricSnapshot == null) return Collections.emptyList(); - List<JsonNode> packets = new ArrayList<>(); + List<JSONObject> packets = new ArrayList<>(); for (Map.Entry<MetricDimensions, MetricSet> snapshotEntry : metricSnapshot) { MetricDimensions metricDimensions = snapshotEntry.getKey(); MetricSet metricSet = snapshotEntry.getValue(); - ObjectNode packet = jsonMapper.createObjectNode(); + JSONObjectWithLegibleException packet = new JSONObjectWithLegibleException(); addMetaData(timestamp, application, packet); addDimensions(metricDimensions, packet); addMetrics(metricSet, packet); @@ -173,27 +168,27 @@ public class MetricsPacketsHandler extends AbstractRequestHandler { return packets; } - private void addMetaData(long timestamp, String application, ObjectNode packet) { + private void addMetaData(long timestamp, String application, JSONObjectWithLegibleException packet) { packet.put(APPLICATION_KEY, application); packet.put(TIMESTAMP_KEY, TimeUnit.MILLISECONDS.toSeconds(timestamp)); } - private void addDimensions(MetricDimensions metricDimensions, ObjectNode packet) { + private void addDimensions(MetricDimensions metricDimensions, JSONObjectWithLegibleException packet) throws JSONException { if (metricDimensions == null) return; Iterator<Map.Entry<String, String>> dimensionsIterator = metricDimensions.iterator(); if (dimensionsIterator.hasNext()) { - ObjectNode jsonDim = jsonMapper.createObjectNode(); - packet.set(DIMENSIONS_KEY, jsonDim); + JSONObject jsonDim = new JSONObjectWithLegibleException(); + packet.put(DIMENSIONS_KEY, jsonDim); for (Map.Entry<String, String> dimensionEntry : metricDimensions) { jsonDim.put(dimensionEntry.getKey(), dimensionEntry.getValue()); } } } - private void addMetrics(MetricSet metricSet, ObjectNode packet) { - ObjectNode metrics = jsonMapper.createObjectNode(); - packet.set(METRICS_KEY, metrics); + private void addMetrics(MetricSet metricSet, JSONObjectWithLegibleException packet) throws JSONException { + JSONObjectWithLegibleException metrics = new JSONObjectWithLegibleException(); + packet.put(METRICS_KEY, metrics); for (Map.Entry<String, MetricValue> metric : metricSet) { String name = metric.getKey(); MetricValue value = metric.getValue(); |