|
| 1 | +package io.prometheus.client.bridge; |
| 2 | + |
| 3 | +import io.prometheus.client.Collector; |
| 4 | +import io.prometheus.client.CollectorRegistry; |
| 5 | + |
| 6 | +import java.io.BufferedWriter; |
| 7 | +import java.io.IOException; |
| 8 | +import java.io.PrintWriter; |
| 9 | +import java.net.Socket; |
| 10 | +import java.util.Collections; |
| 11 | +import java.util.logging.Level; |
| 12 | +import java.util.logging.Logger; |
| 13 | +import java.util.regex.Pattern; |
| 14 | +import java.util.regex.Matcher; |
| 15 | + |
| 16 | +/** |
| 17 | + * Export metrics in the Graphite plaintext format. |
| 18 | + * <p> |
| 19 | + * <pre> |
| 20 | + * {@code |
| 21 | + * Graphite g = new Graphite("localhost", 2003); |
| 22 | + * // Push the default registry once. |
| 23 | + * g.push(CollectorRegistry.defaultRegistry); |
| 24 | + * |
| 25 | + * // Push the default registry every 60 seconds. |
| 26 | + * Thread thread = g.start(CollectorRegistry.defaultRegistry, 60); |
| 27 | + * // Stop pushing. |
| 28 | + * thread.interrupt(); |
| 29 | + * thread.joi(); |
| 30 | + * } |
| 31 | + * </pre> |
| 32 | + * <p> |
| 33 | + * See <a href="https://github.com/prometheus/pushgateway">https://github.com/prometheus/pushgateway</a> |
| 34 | + */ |
| 35 | +public class Graphite { |
| 36 | + private static final Logger logger = Logger.getLogger(Graphite.class.getName()); |
| 37 | + |
| 38 | + private final String host; |
| 39 | + private final int port; |
| 40 | + private static final Pattern INVALID_GRAPHITE_CHARS = Pattern.compile("[^a-zA-Z0-9_-]"); |
| 41 | + /** |
| 42 | + * Construct a Graphite Bridge with the given host:port. |
| 43 | + */ |
| 44 | + public Graphite(String host, int port) { |
| 45 | + this.host = host; |
| 46 | + this.port = port; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Push samples from the given registry to Graphite. |
| 51 | + */ |
| 52 | + public void push(CollectorRegistry registry) throws IOException { |
| 53 | + Socket s = new Socket(host, port); |
| 54 | + BufferedWriter writer = new BufferedWriter(new PrintWriter(s.getOutputStream())); |
| 55 | + Matcher m = INVALID_GRAPHITE_CHARS.matcher(""); |
| 56 | + long now = System.currentTimeMillis() / 1000; |
| 57 | + for (Collector.MetricFamilySamples metricFamilySamples: Collections.list(registry.metricFamilySamples())) { |
| 58 | + for (Collector.MetricFamilySamples.Sample sample: metricFamilySamples.samples) { |
| 59 | + m.reset(sample.name); |
| 60 | + writer.write(m.replaceAll("_")); |
| 61 | + for (int i = 0; i < sample.labelNames.size(); ++i) { |
| 62 | + m.reset(sample.labelValues.get(i)); |
| 63 | + writer.write("." + sample.labelNames.get(i) + "." + m.replaceAll("_")); |
| 64 | + } |
| 65 | + writer.write(" " + sample.value + " " + now + "\n"); |
| 66 | + } |
| 67 | + } |
| 68 | + writer.close(); |
| 69 | + s.close(); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Push samples from the given registry to Graphite every minute. |
| 74 | + */ |
| 75 | + public Thread start(CollectorRegistry registry) { |
| 76 | + return start(registry, 60); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Push samples from the given registry to Graphite at the given interval. |
| 81 | + */ |
| 82 | + public Thread start(CollectorRegistry registry, int intervalSeconds) { |
| 83 | + Thread thread = new PushThread(registry, intervalSeconds); |
| 84 | + thread.setDaemon(true); |
| 85 | + thread.start(); |
| 86 | + return thread; |
| 87 | + } |
| 88 | + |
| 89 | + private class PushThread extends Thread { |
| 90 | + private final CollectorRegistry registry; |
| 91 | + private final int intervalSeconds; |
| 92 | + |
| 93 | + PushThread(CollectorRegistry registry, int intervalSeconds) { |
| 94 | + this.registry = registry; |
| 95 | + this.intervalSeconds = intervalSeconds; |
| 96 | + } |
| 97 | + |
| 98 | + public void run() { |
| 99 | + long waitUntil = System.currentTimeMillis(); |
| 100 | + while (true) { |
| 101 | + try { |
| 102 | + push(registry); |
| 103 | + } catch (IOException e) { |
| 104 | + logger.log(Level.WARNING, "Exception " + e + " pushing to " + host + ":" + port, e); |
| 105 | + } |
| 106 | + |
| 107 | + long now = System.currentTimeMillis(); |
| 108 | + // We may skip some pushes if we're falling behind. |
| 109 | + while (now >= waitUntil) { |
| 110 | + waitUntil += intervalSeconds * 1000; |
| 111 | + } |
| 112 | + try { |
| 113 | + Thread.sleep(waitUntil - now); |
| 114 | + } catch (InterruptedException e) { |
| 115 | + return; |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments