From f3775f6b4fc496d3b553eca168224bd0b1d61bd6 Mon Sep 17 00:00:00 2001 From: Ola Aunrønning Date: Thu, 19 Sep 2019 15:30:48 +0200 Subject: Support array formatted metrics response --- .../jdisc/state/MetricsPacketsHandler.java | 25 +++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) (limited to 'container-core/src/main') 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 e341b66e0f8..d7760ac9c91 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 @@ -13,6 +13,7 @@ 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; @@ -81,33 +82,47 @@ public class MetricsPacketsHandler extends AbstractRequestHandler { @Override protected Iterable responseContent() { - return Collections.singleton(ByteBuffer.wrap(buildMetricOutput())); + return Collections.singleton(ByteBuffer.wrap(buildMetricOutput(request.getUri().getQuery()))); } }.dispatch(handler); return null; } - private byte[] buildMetricOutput() { + private byte[] buildMetricOutput(String query) { try { - String output = getStatusPacket() + getAllMetricsPackets() + "\n"; + if (query != null && query.equals("array-formatted")) { + return getMetricsArray(); + } + String output = jsonToString(getStatusPacket()) + getAllMetricsPackets() + "\n"; return output.getBytes(StandardCharsets.UTF_8); } catch (JSONException e) { throw new RuntimeException("Bad JSON construction.", e); } } + private byte[] getMetricsArray() throws JSONException { + JSONObject root = new JSONObject(); + JSONArray jsonArray = new JSONArray(); + jsonArray.put(getStatusPacket()); + getPacketsForSnapshot(getSnapshot(), applicationName, timer.currentTimeMillis()) + .forEach(jsonArray::put); + root.put("metrics", jsonArray); + return jsonToString(root) + .getBytes(StandardCharsets.UTF_8); + } + /** * Exactly one status packet is added to the response. */ - private String getStatusPacket() throws JSONException { + private JSONObject getStatusPacket() throws JSONException { JSONObject packet = new JSONObjectWithLegibleException(); packet.put(APPLICATION_KEY, applicationName); StateMonitor.Status status = monitor.status(); packet.put(STATUS_CODE_KEY, status.ordinal()); packet.put(STATUS_MSG_KEY, status.name()); - return jsonToString(packet); + return packet; } private String jsonToString(JSONObject jsonObject) throws JSONException { -- cgit v1.2.3