Created
March 19, 2019 13:24
-
-
Save sysdig-blog/0b0558dc8189781bf1ddd453a5864ef3 to your computer and use it in GitHub Desktop.
Prometheus metrics code instrumentation Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io.prometheus.client.Counter; | |
import io.prometheus.client.Gauge; | |
import io.prometheus.client.Histogram; | |
import io.prometheus.client.Summary; | |
import io.prometheus.client.exporter.HTTPServer; | |
import java.io.IOException; | |
import java.util.Random; | |
public class Main { | |
private static double rand(double min, double max) { | |
return min + (Math.random() * (max - min)); | |
} | |
public static void main(String[] args) { | |
Counter counter = Counter.build().namespace("java").name("my_counter").help("This is my counter").register(); | |
Gauge gauge = Gauge.build().namespace("java").name("my_gauge").help("This is my gauge").register(); | |
Histogram histogram = Histogram.build().namespace("java").name("my_histogram").help("This is my histogram").register(); | |
Summary summary = Summary.build().namespace("java").name("my_summary").help("This is my summary").register(); | |
Thread bgThread = new Thread(() -> { | |
while (true) { | |
try { | |
counter.inc(rand(0, 5)); | |
gauge.set(rand(-5, 10)); | |
histogram.observe(rand(0, 5)); | |
summary.observe(rand(0, 5)); | |
Thread.sleep(1000); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
}); | |
bgThread.start(); | |
try { | |
HTTPServer server = new HTTPServer(8080); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment