@Test public void testPush() throws Exception { CollectorRegistry registry = new CollectorRegistry(); Gauge labels = Gauge.build().name("labels").help("help").labelNames("l").register(registry); labels.labels("fo*o").inc(); final ServerSocket ss = new ServerSocket(0); final StringBuilder result = new StringBuilder(); Thread t = new Thread() { public void run() { try { Socket s = ss.accept(); BufferedReader reader = new BufferedReader(new InputStreamReader(s.getInputStream())); result.append(reader.readLine()); s.close(); } catch (Exception e) { e.printStackTrace(); fail(); } } }; t.start(); Graphite g = new Graphite("localhost", ss.getLocalPort()); g.push(registry); t.join(); ss.close(); String[] parts = result.toString().split(" "); assertEquals(3, parts.length); assertEquals("labels.l.fo_o", parts[0]); assertEquals("1.0", parts[1]); Integer.parseInt(parts[2]); }
public void push(CollectorRegistry registry) throws IOException { Socket s = new Socket(host, port); BufferedWriter writer = new BufferedWriter(new PrintWriter(new OutputStreamWriter(s.getOutputStream(), Charset.forName("UTF-8")))); Matcher m = INVALID_GRAPHITE_CHARS.matcher(""); long now = System.currentTimeMillis() / 1000; for (Collector.MetricFamilySamples metricFamilySamples: Collections.list(registry.metricFamilySamples())) { for (Collector.MetricFamilySamples.Sample sample: metricFamilySamples.samples) { m.reset(sample.name); writer.write(m.replaceAll("_")); for (int i = 0; i < sample.labelNames.size(); ++i) { m.reset(sample.labelValues.get(i)); writer.write("." + sample.labelNames.get(i) + "." + m.replaceAll("_")); } writer.write(" " + sample.value + " " + now + "\n"); } } writer.close(); s.close(); }
Graphite { public void push(CollectorRegistry registry) throws IOException { Socket s = new Socket(host, port); BufferedWriter writer = new BufferedWriter(new PrintWriter(new OutputStreamWriter(s.getOutputStream(), Charset.forName("UTF-8")))); Matcher m = INVALID_GRAPHITE_CHARS.matcher(""); long now = System.currentTimeMillis() / 1000; for (Collector.MetricFamilySamples metricFamilySamples: Collections.list(registry.metricFamilySamples())) { for (Collector.MetricFamilySamples.Sample sample: metricFamilySamples.samples) { m.reset(sample.name); writer.write(m.replaceAll("_")); for (int i = 0; i < sample.labelNames.size(); ++i) { m.reset(sample.labelValues.get(i)); writer.write("." + sample.labelNames.get(i) + "." + m.replaceAll("_")); } writer.write(" " + sample.value + " " + now + "\n"); } } writer.close(); s.close(); } }
Graphite { public void push(CollectorRegistry registry) throws IOException { Socket s = new Socket(host, port); BufferedWriter writer = new BufferedWriter(new PrintWriter(new OutputStreamWriter(s.getOutputStream(), Charset.forName("UTF-8")))); Matcher m = INVALID_GRAPHITE_CHARS.matcher(""); long now = System.currentTimeMillis() / 1000; for (Collector.MetricFamilySamples metricFamilySamples: Collections.list(registry.metricFamilySamples())) { for (Collector.MetricFamilySamples.Sample sample: metricFamilySamples.samples) { m.reset(sample.name); writer.write(m.replaceAll("_")); for (int i = 0; i < sample.labelNames.size(); ++i) { m.reset(sample.labelValues.get(i)); writer.write("." + sample.labelNames.get(i) + "." + m.replaceAll("_")); } writer.write(" " + sample.value + " " + now + "\n"); } } writer.close(); s.close(); } Graphite(String host, int port); }
Graphite { public void push(CollectorRegistry registry) throws IOException { Socket s = new Socket(host, port); BufferedWriter writer = new BufferedWriter(new PrintWriter(new OutputStreamWriter(s.getOutputStream(), Charset.forName("UTF-8")))); Matcher m = INVALID_GRAPHITE_CHARS.matcher(""); long now = System.currentTimeMillis() / 1000; for (Collector.MetricFamilySamples metricFamilySamples: Collections.list(registry.metricFamilySamples())) { for (Collector.MetricFamilySamples.Sample sample: metricFamilySamples.samples) { m.reset(sample.name); writer.write(m.replaceAll("_")); for (int i = 0; i < sample.labelNames.size(); ++i) { m.reset(sample.labelValues.get(i)); writer.write("." + sample.labelNames.get(i) + "." + m.replaceAll("_")); } writer.write(" " + sample.value + " " + now + "\n"); } } writer.close(); s.close(); } Graphite(String host, int port); void push(CollectorRegistry registry); Thread start(CollectorRegistry registry); Thread start(CollectorRegistry registry, int intervalSeconds); }
Graphite { public void push(CollectorRegistry registry) throws IOException { Socket s = new Socket(host, port); BufferedWriter writer = new BufferedWriter(new PrintWriter(new OutputStreamWriter(s.getOutputStream(), Charset.forName("UTF-8")))); Matcher m = INVALID_GRAPHITE_CHARS.matcher(""); long now = System.currentTimeMillis() / 1000; for (Collector.MetricFamilySamples metricFamilySamples: Collections.list(registry.metricFamilySamples())) { for (Collector.MetricFamilySamples.Sample sample: metricFamilySamples.samples) { m.reset(sample.name); writer.write(m.replaceAll("_")); for (int i = 0; i < sample.labelNames.size(); ++i) { m.reset(sample.labelValues.get(i)); writer.write("." + sample.labelNames.get(i) + "." + m.replaceAll("_")); } writer.write(" " + sample.value + " " + now + "\n"); } } writer.close(); s.close(); } Graphite(String host, int port); void push(CollectorRegistry registry); Thread start(CollectorRegistry registry); Thread start(CollectorRegistry registry, int intervalSeconds);  }