<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient</artifactId>
<version>0.12.0</version>
</dependency>
import io.prometheus.client.Counter;
import io.prometheus.client.Gauge;
public class MyMetrics {
public static final Counter requestsTotal = Counter.build()
.name("myapp_requests_total")
.help("Total number of requests made.")
.register();
public static final Gauge requestsInProgress = Gauge.build()
.name("myapp_requests_in_progress")
.help("Number of requests in progress.")
.register();
}
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.exporter.HTTPServer;
public class MyApp {
public static void main(String[] args) throws IOException {
CollectorRegistry registry = CollectorRegistry.defaultRegistry;
HTTPServer server = new HTTPServer(1234);
registry.register(MyMetrics.requestsTotal);
registry.register(MyMetrics.requestsInProgress);
server.stop();
}
}
public class MyService {
public void processRequest() {
MyMetrics.requestsTotal.inc();
MyMetrics.requestsInProgress.set(getNumberOfRequestsInProgress());
}
private int getNumberOfRequestsInProgress() {
return 10;
}
}