/** Encode the measurements to a JSON payload that can be sent to the aggregator. */ static byte[] encode( Map<String, String> commonTags, List<Measurement> measurements) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); JsonGenerator gen = FACTORY.createGenerator(baos); gen.writeStartArray(); Map<String, Integer> strings = buildStringTable(gen, commonTags, measurements); for (Measurement m : measurements) { appendMeasurement(gen, strings, commonTags, m.id(), m.value()); } gen.writeEndArray(); gen.close(); return baos.toByteArray(); }
private static void appendMeasurement( JsonGenerator gen, Map<String, Integer> strings, Map<String, String> commonTags, Id id, double value) throws IOException { int op = operation(id); if (shouldSend(op, value)) { // Number of tag entries, commonTags + name + tags int n = commonTags.size() + 1 + Utils.size(id.tags()); gen.writeNumber(n); // Write out the key/value pairs for the tags for (Map.Entry<String, String> entry : commonTags.entrySet()) { gen.writeNumber(strings.get(entry.getKey())); gen.writeNumber(strings.get(entry.getValue())); } for (Tag t : id.tags()) { gen.writeNumber(strings.get(t.key())); gen.writeNumber(strings.get(t.value())); } gen.writeNumber(strings.get("name")); gen.writeNumber(strings.get(id.name())); // Write out the operation and delta value gen.writeNumber(op); gen.writeNumber(value); } }
@Test public void encodeSendsMax0() throws Exception { List<Measurement> ms = new ArrayList<>(); ms.add(max(0, "test")); Map<Id, Delta> values = decode(JsonUtils.encode(Collections.emptyMap(), ms)); Assertions.assertEquals(1, values.size()); ms.forEach(m -> { Id id = m.id(); Assertions.assertEquals(0.0, values.get(id).value, 1e-12); }); }
private static int operation(Id id) { for (Tag t : id.tags()) { if ("statistic".equals(t.key())) { return operation(t.value()); } } LOGGER.warn("invalid statistic for {}, value will be dropped", id); return UNKNOWN; }
@Test public void encodeIgnoresNaN() throws Exception { List<Measurement> ms = new ArrayList<>(); ms.add(count(Double.NaN, "test")); Map<Id, Delta> values = decode(JsonUtils.encode(Collections.emptyMap(), ms)); Assertions.assertEquals(0, values.size()); }
private static int operation(Id id) { for (Tag t : id.tags()) { if ("statistic".equals(t.key())) { return operation(t.value()); } } LOGGER.warn("invalid statistic for {}, value will be dropped", id); return UNKNOWN; }
@Test public void encodeIgnoresAdd0() throws Exception { List<Measurement> ms = new ArrayList<>(); ms.add(count(0, "test")); Map<Id, Delta> values = decode(JsonUtils.encode(Collections.emptyMap(), ms)); Assertions.assertEquals(0, values.size()); }
/** Encode the measurements to a JSON payload that can be sent to the aggregator. */ static byte[] encode( Map<String, String> commonTags, List<Measurement> measurements) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); JsonGenerator gen = FACTORY.createGenerator(baos); gen.writeStartArray(); Map<String, Integer> strings = buildStringTable(gen, commonTags, measurements); for (Measurement m : measurements) { appendMeasurement(gen, strings, commonTags, m.id(), m.value()); } gen.writeEndArray(); gen.close(); return baos.toByteArray(); }
private static void appendMeasurement( JsonGenerator gen, Map<String, Integer> strings, Map<String, String> commonTags, Id id, double value) throws IOException { int op = operation(id); if (shouldSend(op, value)) { // Number of tag entries, commonTags + name + tags int n = commonTags.size() + 1 + Utils.size(id.tags()); gen.writeNumber(n); // Write out the key/value pairs for the tags for (Map.Entry<String, String> entry : commonTags.entrySet()) { gen.writeNumber(strings.get(entry.getKey())); gen.writeNumber(strings.get(entry.getValue())); } for (Tag t : id.tags()) { gen.writeNumber(strings.get(t.key())); gen.writeNumber(strings.get(t.value())); } gen.writeNumber(strings.get("name")); gen.writeNumber(strings.get(id.name())); // Write out the operation and delta value gen.writeNumber(op); gen.writeNumber(value); } }
@Test public void encodeIgnoresInvalidStatistic() throws Exception { List<Measurement> ms = new ArrayList<>(); ms.add(count(42, "test", "statistic", "foo")); Map<Id, Delta> values = decode(JsonUtils.encode(Collections.emptyMap(), ms)); Assertions.assertEquals(0, values.size()); }
@Test public void encodeIgnoresNoStatistic() throws Exception { List<Measurement> ms = new ArrayList<>(); ms.add(unknown(42, "test")); Map<Id, Delta> values = decode(JsonUtils.encode(Collections.emptyMap(), ms)); Assertions.assertEquals(0, values.size()); }
@Test public void encodeNoCommonTags() throws Exception { List<Measurement> ms = new ArrayList<>(); ms.add(count(42.0, "test")); Map<Id, Delta> values = decode(JsonUtils.encode(Collections.emptyMap(), ms)); Assertions.assertEquals(1, values.size()); ms.forEach(m -> { Assertions.assertEquals(42.0, values.get(m.id()).value, 1e-12); }); }
@Test public void encodeCommonTags() throws Exception { Map<String, String> commonTags = new HashMap<>(); commonTags.put("a", "1"); commonTags.put("b", "2"); List<Measurement> ms = new ArrayList<>(); ms.add(count(42.0, "test")); Map<Id, Delta> values = decode(JsonUtils.encode(commonTags, ms)); Assertions.assertEquals(1, values.size()); ms.forEach(m -> { Id id = m.id().withTags(commonTags); Assertions.assertEquals(42.0, values.get(id).value, 1e-12); }); }
@Test public void encodeSupportsKnownStats() throws Exception { for (Statistic stat : Statistic.values()) { List<Measurement> ms = new ArrayList<>(); ms.add(count(42, "test", "statistic", stat.value())); Map<Id, Delta> values = decode(JsonUtils.encode(Collections.emptyMap(), ms)); Assertions.assertEquals(1, values.size()); ms.forEach(m -> { Id id = m.id(); Assertions.assertEquals(42.0, values.get(id).value, 1e-12); }); } }
private void collectData() { try { for (List<Measurement> batch : getBatches()) { byte[] payload = JsonUtils.encode(commonTags, batch); HttpResponse res = client.post(uri) .withConnectTimeout(connectTimeout) .withReadTimeout(readTimeout) .withContent("application/json", payload) .compress(Deflater.BEST_SPEED) .send(); if (res.status() != 200) { logger.warn("failed to send metrics, status {}: {}", res.status(), res.entityAsString()); } } removeExpiredMeters(); } catch (Exception e) { logger.warn("failed to send metrics", e); } }
private void collectData() { try { for (List<Measurement> batch : getBatches()) { byte[] payload = JsonUtils.encode(commonTags, batch); HttpResponse res = client.post(uri) .withConnectTimeout(connectTimeout) .withReadTimeout(readTimeout) .withContent("application/json", payload) .compress(Deflater.BEST_SPEED) .send(); if (res.status() != 200) { logger.warn("failed to send metrics, status {}: {}", res.status(), res.entityAsString()); } } removeExpiredMeters(); } catch (Exception e) { logger.warn("failed to send metrics", e); } }