Integration of Prometheus Metrics in Java Applications: simpleclient Library

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Prometheus Metrics in Java: simpleclient Library

Prometheus is a powerful open-source monitoring and alerting system that collects metrics from various sources. For Java applications, there are several libraries for integrating with Prometheus. The main and most popular one is simpleclient from the Prometheus project itself. It provides a minimalistic, thread-safe, and high-performance way to create, register, and export metrics.



What is simpleclient?

simpleclient is the official Prometheus Java library that allows developers to add metrics to applications with minimal overhead. It supports four main metric types:

  • Counter — a counter that only increases (e.g., number of requests).
  • Gauge — a value that can both increase and decrease (e.g., number of active connections, temperature).
  • Histogram — a histogram measuring the distribution of values (e.g., request execution time).
  • Summary — a summary similar to a histogram but calculating quantiles on the client side.


Installation and Setup

To work with simpleclient, add the following dependencies to your pom.xml (Maven):

<dependency>    <groupId>io.prometheus</groupId>    <artifactId>simpleclient</artifactId>    <version>0.16.0</version></dependency><dependency>    <groupId>io.prometheus</groupId>    <artifactId>simpleclient_hotspot</artifactId>    <version>0.16.0</version></dependency><dependency>    <groupId>io.prometheus</groupId>    <artifactId>simpleclient_servlet</artifactId>    <version>0.16.0</version></dependency>

For Gradle:

implementation 'io.prometheus:simpleclient:0.16.0'implementation 'io.prometheus:simpleclient_hotspot:0.16.0'implementation 'io.prometheus:simpleclient_servlet:0.16.0'


Creating and Registering Metrics

Let's look at creating a simple request counter. Metrics are registered in the global registry CollectorRegistry.defaultRegistry.

import io.prometheus.client.Counter;import io.prometheus.client.Gauge;

public class MetricsExample { // Create a counter named "http_requests_total" with labels "method" and "endpoint" static final Counter requests = Counter.build() .name("http_requests_total") .help("Total number of HTTP requests.") .labelNames("method", "endpoint") .register();

// Create a gauge for the number of active sessions static final Gauge activeSessions = Gauge.build() .name("active_sessions") .help("Current number of active sessions.") .register();

public static void processRequest(String method, String endpoint) { // Increment the counter requests.labels(method, endpoint).inc(); // Set the gauge value (e.g., queue size) activeSessions.set(42); }}


Exporting Metrics via HTTP

For Prometheus to collect metrics, you need to provide a /metrics endpoint. The easiest way is to use the built-in servlet. If you are using Spring Boot, add the simpleclient_spring_boot dependency.

import io.prometheus.client.exporter.MetricsServlet;import io.prometheus.client.hotspot.DefaultExports;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;

@WebServlet("/metrics"

Recommendations