@Test public void metricsAreFormattedCorrectly() throws IOException { LibratoWriter2 libratoWriter = new LibratoWriter2(new JsonFactory(), ImmutableList.<String>of()); StringWriter writer = new StringWriter(); libratoWriter.write(writer, dummyServer(), dummyQuery(), dummyResults()); String json = writer.toString(); assertThatJson(json) .node("counters").isArray().ofLength(0); assertThatJson(json) .node("gauges").isArray().ofLength(1); assertThatJson(json) .node("gauges[0].name").isEqualTo("MemoryAlias.ObjectPendingFinalizationCount") .node("gauges[0].source").isEqualTo("host_example_net") .node("gauges[0].measure_time").isEqualTo(0) .node("gauges[0].value").isEqualTo(10); }
@Override public void write(@Nonnull Writer writer, @Nonnull Server server, @Nonnull Query query, @Nonnull Iterable<Result> results) throws IOException { Closer closer = Closer.create(); try { JsonGenerator g = closer.register(jsonFactory.createGenerator(writer)); g.writeStartObject(); g.writeArrayFieldStart("counters"); g.writeEndArray(); String source = getSource(server); g.writeArrayFieldStart("gauges"); for (Result result : results) { if (isNumeric(result.getValue())) { g.writeStartObject(); g.writeStringField("name", KeyUtils.getKeyString(query, result, typeNames)); if (source != null && !source.isEmpty()) { g.writeStringField("source", source); } g.writeNumberField("measure_time", SECONDS.convert(result.getEpoch(), MILLISECONDS)); Object value = result.getValue(); if (value instanceof Integer) { g.writeNumberField("value", (Integer) value); } else if (value instanceof Long) { g.writeNumberField("value", (Long) value); } else if (value instanceof Float) { g.writeNumberField("value", (Float) value); } else if (value instanceof Double) { g.writeNumberField("value", (Double) value); } g.writeEndObject(); } } g.writeEndArray(); g.writeEndObject(); g.flush(); } catch (Throwable t) { throw closer.rethrow(t); } finally { closer.close(); } }
LibratoWriter2 implements WriterBasedOutputWriter { @Override public void write(@Nonnull Writer writer, @Nonnull Server server, @Nonnull Query query, @Nonnull Iterable<Result> results) throws IOException { Closer closer = Closer.create(); try { JsonGenerator g = closer.register(jsonFactory.createGenerator(writer)); g.writeStartObject(); g.writeArrayFieldStart("counters"); g.writeEndArray(); String source = getSource(server); g.writeArrayFieldStart("gauges"); for (Result result : results) { if (isNumeric(result.getValue())) { g.writeStartObject(); g.writeStringField("name", KeyUtils.getKeyString(query, result, typeNames)); if (source != null && !source.isEmpty()) { g.writeStringField("source", source); } g.writeNumberField("measure_time", SECONDS.convert(result.getEpoch(), MILLISECONDS)); Object value = result.getValue(); if (value instanceof Integer) { g.writeNumberField("value", (Integer) value); } else if (value instanceof Long) { g.writeNumberField("value", (Long) value); } else if (value instanceof Float) { g.writeNumberField("value", (Float) value); } else if (value instanceof Double) { g.writeNumberField("value", (Double) value); } g.writeEndObject(); } } g.writeEndArray(); g.writeEndObject(); g.flush(); } catch (Throwable t) { throw closer.rethrow(t); } finally { closer.close(); } } }
LibratoWriter2 implements WriterBasedOutputWriter { @Override public void write(@Nonnull Writer writer, @Nonnull Server server, @Nonnull Query query, @Nonnull Iterable<Result> results) throws IOException { Closer closer = Closer.create(); try { JsonGenerator g = closer.register(jsonFactory.createGenerator(writer)); g.writeStartObject(); g.writeArrayFieldStart("counters"); g.writeEndArray(); String source = getSource(server); g.writeArrayFieldStart("gauges"); for (Result result : results) { if (isNumeric(result.getValue())) { g.writeStartObject(); g.writeStringField("name", KeyUtils.getKeyString(query, result, typeNames)); if (source != null && !source.isEmpty()) { g.writeStringField("source", source); } g.writeNumberField("measure_time", SECONDS.convert(result.getEpoch(), MILLISECONDS)); Object value = result.getValue(); if (value instanceof Integer) { g.writeNumberField("value", (Integer) value); } else if (value instanceof Long) { g.writeNumberField("value", (Long) value); } else if (value instanceof Float) { g.writeNumberField("value", (Float) value); } else if (value instanceof Double) { g.writeNumberField("value", (Double) value); } g.writeEndObject(); } } g.writeEndArray(); g.writeEndObject(); g.flush(); } catch (Throwable t) { throw closer.rethrow(t); } finally { closer.close(); } } LibratoWriter2(@Nonnull JsonFactory jsonFactory, @Nonnull ImmutableList<String> typeNames); }
LibratoWriter2 implements WriterBasedOutputWriter { @Override public void write(@Nonnull Writer writer, @Nonnull Server server, @Nonnull Query query, @Nonnull Iterable<Result> results) throws IOException { Closer closer = Closer.create(); try { JsonGenerator g = closer.register(jsonFactory.createGenerator(writer)); g.writeStartObject(); g.writeArrayFieldStart("counters"); g.writeEndArray(); String source = getSource(server); g.writeArrayFieldStart("gauges"); for (Result result : results) { if (isNumeric(result.getValue())) { g.writeStartObject(); g.writeStringField("name", KeyUtils.getKeyString(query, result, typeNames)); if (source != null && !source.isEmpty()) { g.writeStringField("source", source); } g.writeNumberField("measure_time", SECONDS.convert(result.getEpoch(), MILLISECONDS)); Object value = result.getValue(); if (value instanceof Integer) { g.writeNumberField("value", (Integer) value); } else if (value instanceof Long) { g.writeNumberField("value", (Long) value); } else if (value instanceof Float) { g.writeNumberField("value", (Float) value); } else if (value instanceof Double) { g.writeNumberField("value", (Double) value); } g.writeEndObject(); } } g.writeEndArray(); g.writeEndObject(); g.flush(); } catch (Throwable t) { throw closer.rethrow(t); } finally { closer.close(); } } LibratoWriter2(@Nonnull JsonFactory jsonFactory, @Nonnull ImmutableList<String> typeNames); @Override void write(@Nonnull Writer writer, @Nonnull Server server, @Nonnull Query query, @Nonnull Iterable<Result> results); }
LibratoWriter2 implements WriterBasedOutputWriter { @Override public void write(@Nonnull Writer writer, @Nonnull Server server, @Nonnull Query query, @Nonnull Iterable<Result> results) throws IOException { Closer closer = Closer.create(); try { JsonGenerator g = closer.register(jsonFactory.createGenerator(writer)); g.writeStartObject(); g.writeArrayFieldStart("counters"); g.writeEndArray(); String source = getSource(server); g.writeArrayFieldStart("gauges"); for (Result result : results) { if (isNumeric(result.getValue())) { g.writeStartObject(); g.writeStringField("name", KeyUtils.getKeyString(query, result, typeNames)); if (source != null && !source.isEmpty()) { g.writeStringField("source", source); } g.writeNumberField("measure_time", SECONDS.convert(result.getEpoch(), MILLISECONDS)); Object value = result.getValue(); if (value instanceof Integer) { g.writeNumberField("value", (Integer) value); } else if (value instanceof Long) { g.writeNumberField("value", (Long) value); } else if (value instanceof Float) { g.writeNumberField("value", (Float) value); } else if (value instanceof Double) { g.writeNumberField("value", (Double) value); } g.writeEndObject(); } } g.writeEndArray(); g.writeEndObject(); g.flush(); } catch (Throwable t) { throw closer.rethrow(t); } finally { closer.close(); } } LibratoWriter2(@Nonnull JsonFactory jsonFactory, @Nonnull ImmutableList<String> typeNames); @Override void write(@Nonnull Writer writer, @Nonnull Server server, @Nonnull Query query, @Nonnull Iterable<Result> results);  }