<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient</artifactId>
<version>0.11.0</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_hotspot</artifactId>
<version>0.11.0</version>
</dependency>
import io.prometheus.client.Counter;
public class MyMetrics {
private static final Counter requestsTotal = Counter.build()
.name("requests_total")
.help("Total number of requests handled")
.register();
public void handleRequest() {
requestsTotal.inc();
// your code to handle the request
}
}
import io.prometheus.client.exporter.HTTPServer;
public class MyApplication {
public static void main(String[] args) {
// your application code
// start Prometheus HTTP server
try {
HTTPServer server = new HTTPServer(8080);
} catch (IOException e) {
e.printStackTrace();
}
}
}
yaml
scrape_configs:
- job_name: 'my-java-app'
metrics_path: /metrics
static_configs:
- targets: ['your-java-app-host:8080']