/** * Serializes the collection of metric data points to json * * @param timeSeries - the time series whose points should be serialized. * @return a json serialized collection of metric data points */ public byte[] toJson(MetricTimeSeries timeSeries) { if (!timeSeries.isEmpty()) { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); JsonWriter writer = new JsonWriter(new OutputStreamWriter(baos, UTF_8)); List[] data = new List[]{new ArrayList<>(timeSeries.size()), new ArrayList<>(timeSeries.size())}; for (int i = 0; i < timeSeries.size(); i++) { data[0].add(timeSeries.getTime(i)); data[1].add(timeSeries.getValue(i)); } gson.toJson(data, List[].class, writer); writer.close(); baos.flush(); return baos.toByteArray(); } catch (IOException e) { LOGGER.error("Could not serialize data to json", e); } } return EMPTY_JSON; }
/** * Serializes the collection of metric data points to json * * @param timeSeries - the time series whose points should be serialized. * @return a json serialized collection of metric data points */ public byte[] toJson(MetricTimeSeries timeSeries) { if (!timeSeries.isEmpty()) { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); JsonWriter writer = new JsonWriter(new OutputStreamWriter(baos, UTF_8)); List[] data = new List[]{new ArrayList<>(timeSeries.size()), new ArrayList<>(timeSeries.size())}; for (int i = 0; i < timeSeries.size(); i++) { data[0].add(timeSeries.getTime(i)); data[1].add(timeSeries.getValue(i)); } gson.toJson(data, List[].class, writer); writer.close(); baos.flush(); return baos.toByteArray(); } catch (IOException e) { LOGGER.error("Could not serialize data to json", e); } } return EMPTY_JSON; }
long formerTimestamp = timeSeries.getTime(0); double formerValue = timeSeries.getValue(0); int timesSameTimestamp = 0; if (formerTimestamp == timeSeries.getTime(i)) { formerValue += timeSeries.getValue(i); timesSameTimestamp++; formerTimestamp = timeSeries.getTime(i); formerValue = timeSeries.getValue(i);
@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)); } } }
timeList.add(timeSeries.getTime(i)); valueList.add(value); distinct.add(value);