Monday, 9 June 2025

Getting Started with Prometheus in Java: A Hello World Example Using client_java

Before we dive into tracking custom application metrics like request counts or memory usage, we first need to expose an HTTP endpoint that Prometheus can scrape. This post helps you to start wtith a minimal Java application using the official client_java library.

 

Step 1: Create new maven project ‘prometheus-demos’ and update pom.xml with all the necessary prometheus dependencies.

 

pom.xml 

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.sample.app</groupId>
    <artifactId>prometheus-demos</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <prometheus.version>0.16.0</prometheus.version>
    </properties>

    <dependencies>
        <!-- Core Prometheus client -->
        <dependency>
            <groupId>io.prometheus</groupId>
            <artifactId>simpleclient</artifactId>
            <version>${prometheus.version}</version>
        </dependency>

        <!-- HTTPServer to expose /metrics -->
        <dependency>
            <groupId>io.prometheus</groupId>
            <artifactId>simpleclient_httpserver</artifactId>
            <version>${prometheus.version}</version>
        </dependency>

        <!-- JVM default metrics -->
        <dependency>
            <groupId>io.prometheus</groupId>
            <artifactId>simpleclient_hotspot</artifactId>
            <version>${prometheus.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.10.1</version>
                <configuration>
                    <release>21</release>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Step 2: Create package ‘com.sample.app’ and define PrometheusHelloWorld application.

 

PrometheusHelloWorld.java

 

package com.sample.app;

import io.prometheus.client.exporter.HTTPServer;
import io.prometheus.client.hotspot.DefaultExports;

public class PrometheusHelloWorld {
    public static void main(String[] args) throws Exception {
        // Expose default JVM metrics (optional)
        DefaultExports.initialize();

        // Start Prometheus HTTP server on port 8080
        HTTPServer server = new HTTPServer(8080);

        System.out.println("Prometheus endpoint is available at http://localhost:8080/metrics");

        // Keep the app alive
        while (true) {
            Thread.sleep(1000);
        }
    }
}

Run PrometheusHelloWorld class and open the url http://localhost:8080/metrics to see the metrics for this application. 


 

Add this configuration to prometheus.yml file.

 

prometheus.yml

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']
  - job_name: 'node_exporter'
    static_configs:
      - targets: ['localhost:9100']
  - job_name: 'java_app'
    static_configs:
      - targets: ['localhost:8080']

Start Prometheus instance by executing following command.

 

prometheus --config.file=./prometheus.yml

 

Open the url ‘http://localhost:9090/’ in browser. Navigate to Status -> Target health

 


You can observe that the target java_app is active.

 


You can even query the metrics like jvm_memory_pool_bytes_init for java_app.  


 

Previous                                                    Next                                                    Home

No comments:

Post a Comment