<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient</artifactId>
<version>0.10.0</version>
</dependency>
import io.prometheus.client.Gauge;
public class MyMetrics {
private static final Gauge myMetric = Gauge.build()
.name("my_metric")
.help("This is my custom metric")
.labelNames("label1", "label2")
.register();
public static void setMetricValue(double value, String labelValue1, String labelValue2) {
myMetric.labels(labelValue1, labelValue2).set(value);
}
}
import io.prometheus.client.exporter.HTTPServer;
import java.io.IOException;
public class MyMetricsServer {
public static void main(String[] args) throws IOException {
HTTPServer server = new HTTPServer(9090);
System.out.println("Metrics server started on port 9090");
server.stop();
}
}
import java.util.Timer;
import java.util.TimerTask;
public class MyMetricsCollector {
public static void main(String[] args) {
TimerTask task = new TimerTask() {
@Override
public void run() {
double metricValue = Math.random() * 100;
String labelValue1 = "label1";
String labelValue2 = "label2";
MyMetrics.setMetricValue(metricValue, labelValue1, labelValue2);
}
};
Timer timer = new Timer();
timer.schedule(task, 0, 5000);
}
}