Monitoring Java Applications with Prometheus
Professional
You can instrument your Java application with the official Prometheus Java client library and let the Glouton agent scrape its /metrics endpoint.
Install the client library
Section titled “Install the client library”Add the dependencies to your pom.xml (Maven):
<dependency> <groupId>io.prometheus</groupId> <artifactId>prometheus-metrics-core</artifactId> <version>1.3.6</version></dependency><dependency> <groupId>io.prometheus</groupId> <artifactId>prometheus-metrics-exporter-httpserver</artifactId> <version>1.3.6</version></dependency>Minimal example
Section titled “Minimal example”The snippet below defines a counter and exposes it on http://localhost:8080/metrics:
import io.prometheus.metrics.core.metrics.Counter;import io.prometheus.metrics.exporter.httpserver.HTTPServer;
public class App { static final Counter requestsTotal = Counter.builder() .name("myapp_requests_total") .help("Total number of processed requests.") .register();
public static void main(String[] args) throws Exception { HTTPServer server = HTTPServer.builder().port(8080).buildAndStart(); requestsTotal.inc(); Thread.currentThread().join(); }}Scrape with Glouton
Section titled “Scrape with Glouton”Configure Glouton to scrape your application by adding the following to /etc/glouton/conf.d/50-prometheus-metrics.conf:
metric: prometheus: targets: - url: "http://localhost:8080/metrics" name: "my_java_application" allow_metrics: - "myapp_requests_total"Restart the agent to apply the configuration. See Prometheus Monitoring for the full list of options, including Docker label auto-discovery and Kubernetes annotations.