OpenTsdbHttpFormatParser implements FormatParser { @Override public Iterable<MetricTimeSeries> parse(InputStream stream) throws FormatParseException { Map<Metric, MetricTimeSeries.Builder> metrics = new HashMap<>(); List<TsdbMetric> tsdbMetrics = parseJson(stream); for (TsdbMetric tsdbMetric : tsdbMetrics) { Metric metric = new Metric(tsdbMetric.getMetric(), tsdbMetric.getTags()); MetricTimeSeries.Builder metricBuilder = metrics.get(metric); if (metricBuilder == null) { metricBuilder = new MetricTimeSeries.Builder(tsdbMetric.getMetric(), METRIC_TYPE); for (Map.Entry<String, String> tagEntry : tsdbMetric.getTags().entrySet()) { metricBuilder.attribute(tagEntry.getKey().concat("_s"), tagEntry.getValue()); } metrics.put(metric, metricBuilder); } Instant timestamp = convertTimestamp(tsdbMetric.getTimestamp()); metricBuilder.point(timestamp.toEpochMilli(), tsdbMetric.getValue()); } return metrics.values().stream().map(MetricTimeSeries.Builder::build).collect(Collectors.toList()); }  @Override Iterable<MetricTimeSeries> parse(InputStream stream);  }
@Test public void testSingle() throws Exception { try (InputStream stream = GraphiteFormatParserTest.class.getResourceAsStream("/opentsdb-http-api-single.json")) { assertNotNull(stream); List<MetricTimeSeries> series = Lists.newArrayList(sut.parse(stream)); assertThat(series.size(), is(1)); MetricTimeSeries metric = series.get(0); assertThat(metric.getName(), is("sys.cpu.nice")); assertThat(metric.getTimestamps().size(), is(1)); assertThat(metric.getValues().get(0), is(18.0)); assertThat(metric.getTimestamps().get(0), is(1346846400000L)); } }
@Test public void testMultiple() throws Exception { try (InputStream stream = GraphiteFormatParserTest.class.getResourceAsStream("/opentsdb-http-api-multiple.json")) { assertNotNull(stream); List<MetricTimeSeries> series = Lists.newArrayList(sut.parse(stream)); assertThat(series.size(), is(2)); MetricTimeSeries first = series.get(0); assertThat(first.getName(), is("sys.cpu.nice")); assertThat(first.getTimestamps().size(), is(2)); assertThat(first.getValues().get(0), is(1.0)); assertThat(first.getTimestamps().get(0), is(1346846400000L)); assertThat(first.getValues().get(1), is(2.0)); assertThat(first.getTimestamps().get(1), is(1346846400100L)); MetricTimeSeries second = series.get(1); assertThat(second.getName(), is("sys.cpu.load")); assertThat(second.getTimestamps().size(), is(1)); assertThat(second.getValues().get(0), is(2.0)); assertThat(second.getTimestamps().get(0), is(1346846400000L)); } }