GraphiteFormatter implements Function<String, String> { @Override public String apply(String mesg) { StringBuilder finalMesg = new StringBuilder(); String[] regions = mesg.trim().split(" "); final MetricMangler.MetricComponents components = metricMangler.extractComponents(regions[0]); finalMesg.append(components.metric); finalMesg.append(" "); for (int index = 1; index < regions.length; index++) { finalMesg.append(regions[index]); finalMesg.append(" "); } if (components.source != null) { finalMesg.append("source="); finalMesg.append(components.source); } if (components.annotations != null) { for (int index = 0; index < components.annotations.length; index++) { finalMesg.append(" "); finalMesg.append(components.annotations[index]); } } ops.incrementAndGet(); return finalMesg.toString(); } GraphiteFormatter(String strFields, String strDelimiters, String strFieldsToRemove); long getOps(); MetricMangler getMetricMangler(); @Override String apply(String mesg);  }
@Test public void testFieldsToRemove() { String format = "2"; String delimiter = "_"; String remove = "1,3"; String testString1 = "hosts.host1.collectd.cpu.loadavg.1m 40"; String testString2 = "hosts.host1.collectd.cpu.loadavg.1m 7 1459527231"; String expected1 = "cpu.loadavg.1m 40 source=host1"; String expected2 = "cpu.loadavg.1m 7 1459527231 source=host1"; GraphiteFormatter formatter = new GraphiteFormatter(format, delimiter, remove); assertEquals(expected1, formatter.apply(testString1)); assertEquals(expected2, formatter.apply(testString2)); }
@Test public void testFieldsToRemoveInvalidFormats() { String format = "2"; String delimiter = "_"; String testString1 = "hosts.host1.collectd.cpu.loadavg.1m 40"; String testString2 = "hosts.host1.collectd.cpu.loadavg.1m 7 1459527231"; GraphiteFormatter formatter = new GraphiteFormatter(format, delimiter, ","); String expected1 = "hosts.collectd.cpu.loadavg.1m 40 source=host1"; String expected2 = "hosts.collectd.cpu.loadavg.1m 7 1459527231 source=host1"; assertEquals(expected1, formatter.apply(testString1)); assertEquals(expected2, formatter.apply(testString2)); try { new GraphiteFormatter(format, delimiter, "0"); fail("Expected fields to remove value of '0' to fail"); } catch (IllegalArgumentException e) { } }