NodeMetricGatherer { 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)); } } builders.add(builder); } catch (JSONException e) { Exceptions.toMessageString(e); } } @Inject  NodeMetricGatherer(MetricsManager metricsManager, VespaServices vespaServices, ApplicationDimensions applicationDimensions, NodeDimensions nodeDimensions); List<MetricsPacket> gatherMetrics();  }
@Test public void testJSONObjectIsCorrectlyConvertedToMetricsPacket() throws JSONException { List<MetricsPacket.Builder> builders = new ArrayList<>(); JSONObject hostLifePacket = generateHostLifePacket(); NodeMetricGatherer.addObjectToBuilders(builders, hostLifePacket); MetricsPacket packet = builders.remove(0).build(); assertEquals("host_life", packet.service.id); assertEquals(0, packet.statusCode); assertEquals("OK", packet.statusMessage); assertEquals(123, packet.timestamp); assertEquals(12l, packet.metrics().get(MetricId.toMetricId("uptime"))); assertEquals(1l, packet.metrics().get(MetricId.toMetricId("alive"))); }