PrometheusTextFormatParser implements FormatParser { @Override public Iterable<MetricTimeSeries> parse(InputStream stream) throws FormatParseException { Set<String> validMetricNames = new HashSet<>(); Map<Metric, MetricTimeSeries.Builder> metrics = new HashMap<>(); BufferedReader reader = new BufferedReader(new InputStreamReader(stream, UTF_8)); String line; try { while ((line = reader.readLine()) != null) { if (line.isEmpty()) { continue; } if (isHelpLine(line)) { continue; } if (isTypeLine(line)) { TypeLine typeLine = parseTypeLine(line); if (isTypeValid(typeLine)) { validMetricNames.add(typeLine.getMetricName()); } continue; } if (isCommentLine(line)) { continue; } String[] parts = StringUtils.split(line, ' '); if (parts.length < 2) { throw new FormatParseException("Expected at least 2 parts, found " + parts.length + " in line '" + line + "'"); } String metricName = getMetricName(parts); if (!validMetricNames.contains(metricName)) { LOGGER.debug("Ignoring metric {}", metricName); continue; } Instant timestamp = getMetricTimestamp(parts); double value = getMetricValue(parts); Map<String, String> tags = getMetricTags(parts); addPoint(metrics, metricName, timestamp, value, tags); } } catch (IOException e) { throw new FormatParseException("IO exception while parsing OpenTSDB telnet format", e); } return metrics.values().stream().map(MetricTimeSeries.Builder::build).collect(Collectors.toList()); } PrometheusTextFormatParser(Clock clock); @Override Iterable<MetricTimeSeries> parse(InputStream stream);  }
@Test public void testParse() throws Exception { try (InputStream stream = GraphiteFormatParserTest.class.getResourceAsStream("/prometheus-text.txt")) { assertNotNull(stream); List<MetricTimeSeries> series = Lists.newArrayList(sut.parse(stream)); assertThat(series.size(), is(3)); MetricTimeSeries metricWithoutTimestampAndLabel = series.stream().filter(s -> s.getName().equals("metric_without_timestamp_and_labels")).findFirst().get(); assertThat(metricWithoutTimestampAndLabel.getTime(0), is(NOW.toEpochMilli())); assertThat(metricWithoutTimestampAndLabel.getValue(0), is(12.47)); MetricTimeSeries httpRequestsTotal200 = series.stream().filter(s -> s.getName().equals("http_requests_total") && s.getAttributesReference().get("code").equals("200")).findFirst().get(); assertThat(httpRequestsTotal200.getTime(0), is(1395066363000L)); assertThat(httpRequestsTotal200.getValue(0), is(1027.0)); MetricTimeSeries httpRequestsTotal400 = series.stream().filter(s -> s.getName().equals("http_requests_total") && s.getAttributesReference().get("code").equals("400")).findFirst().get(); assertThat(httpRequestsTotal400.getTime(0), is(1395066363000L)); assertThat(httpRequestsTotal400.getValue(0), is(3.0)); } }