GraphiteFormatParser implements FormatParser { @Override public Iterable<MetricTimeSeries> parse(InputStream stream) throws FormatParseException { Map<String, MetricTimeSeries.Builder> metrics = new HashMap<>(); BufferedReader reader = new BufferedReader(new InputStreamReader(stream, UTF_8)); String line; try { while ((line = reader.readLine()) != null) { String[] parts = StringUtils.split(line, ' '); if (parts.length != 3) { throw new FormatParseException("Expected 3 parts, found " + parts.length + " in line '" + line + "'"); } String metricName = getMetricName(parts); double value = getMetricValue(parts); Instant timestamp = getMetricTimestamp(parts); MetricTimeSeries.Builder metricBuilder = metrics.get(metricName); if (metricBuilder == null) { metricBuilder = new MetricTimeSeries.Builder(metricName, METRIC_TYPE); metrics.put(metricName, metricBuilder); } metricBuilder.point(timestamp.toEpochMilli(), value); } } catch (IOException e) { throw new FormatParseException("IO exception while parsing Graphite format", e); } return metrics.values().stream().map(MetricTimeSeries.Builder::build).collect(Collectors.toList()); }  @Override Iterable<MetricTimeSeries> parse(InputStream stream);  }
@Test public void testParse() throws Exception { try (InputStream stream = GraphiteFormatParserTest.class.getResourceAsStream("/graphite.txt")) { assertNotNull(stream); List<MetricTimeSeries> series = Lists.newArrayList(sut.parse(stream)); assertThat(series.size(), is(2)); MetricTimeSeries bashSeries = series.get(0); assertThat(bashSeries.getName(), is("test.bash.stats")); assertThat(bashSeries.getTimestamps().size(), is(5)); assertThat(bashSeries.getTimestamps().get(0), is(1475754111000L)); assertThat(bashSeries.getTimestamps().get(1), is(1475754112000L)); assertThat(bashSeries.getTimestamps().get(2), is(1475754113000L)); assertThat(bashSeries.getTimestamps().get(3), is(1475754114000L)); assertThat(bashSeries.getTimestamps().get(4), is(1475754115000L)); assertThat(bashSeries.getValues().size(), is(5)); assertThat(bashSeries.getValues().get(0), is(1.0)); assertThat(bashSeries.getValues().get(1), is(2.0)); assertThat(bashSeries.getValues().get(2), is(3.0)); assertThat(bashSeries.getValues().get(3), is(4.0)); assertThat(bashSeries.getValues().get(4), is(5.0)); MetricTimeSeries psSeries = series.get(1); assertThat(psSeries.getName(), is("test.ps.stats")); assertThat(psSeries.getTimestamps().size(), is(4)); assertThat(psSeries.getTimestamps().get(0), is(1475754116000L)); assertThat(psSeries.getTimestamps().get(1), is(1475754117000L)); assertThat(psSeries.getTimestamps().get(2), is(1475754118000L)); assertThat(psSeries.getTimestamps().get(3), is(1475754119000L)); assertThat(psSeries.getValues().size(), is(4)); assertThat(psSeries.getValues().get(0), is(6.0)); assertThat(psSeries.getValues().get(1), is(7.0)); assertThat(psSeries.getValues().get(2), is(8.0)); assertThat(psSeries.getValues().get(3), is(9.0)); } }