Using the Java Client Library for Prometheus

1 hour
  • 4 Learning Objectives

About this Hands-on Lab

Prometheus is a powerful tool for monitoring your applications. However, before Prometheus can provide useful data about your applications, your applications need to be instrumented to provide that data. Luckily, client libraries are available to make this significantly easier in a number of programming languages. In this lab, you will be able to get hands-on with the Prometheus Java client library. You will use the client library to instrument a Java application to provide metrics to a Prometheus server.x

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Add Prometheus client library dependencies to the Java project.
  1. Log in to the Prometheus server.

  2. Change the directory to the root directory for the Java project:

cd /home/cloud_user/content-prometheusdd-limedrop-svc
  1. Run the project to verify that it is able to compile and run before making any changes:
./gradlew clean bootRun
  1. Once you see the text Started App in X seconds, the application is running. Use control + c to stop it.

  2. Edit build.gradle:

vi build.gradle
  1. Locate the dependencies block and add the Prometheus client library dependencies:
dependencies {
  implementation 'io.prometheus:simpleclient:0.8.1'
  implementation 'io.prometheus:simpleclient_httpserver:0.8.1'

  ...

}
  1. If you want, you can run the project again to automatically download the dependencies and make sure it still works:
./gradlew clean bootRun
Add instrumentation to the /limesAvailable endpoint.
  1. Edit the controller class:
vi src/main/java/com/limedrop/svc/LimeDropController.java
  1. Add the requested counter and gauge metrics:
package com.limedrop.svc;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
public class LimeDropController {

    static final Counter totalRequests = Counter.build()
      .name("total_requests").help("Total requests.").register();
    static final Gauge inprogressRequests = Gauge.build()
      .name("inprogress_requests").help("Inprogress requests.").register();

    @GetMapping(path="/limesAvailable", produces = "application/json")
    public String checkAvailability() {
        inprogressRequests.inc();
        totalRequests.inc();

        String response = "{"warehouse_1": "58534", "warehouse_2": "72399"}";

        inprogressRequests.dec();
        return response;
    }

}
  1. Run the application again to make sure it compiles:
./gradlew clean bootRun
Add a scrape endpoint to the application.
  1. Edit the main application class:
vi src/main/java/com/limedrop/svc/App.java
  1. Use the Prometheus HTTPServer to set up a scrape endpoint on port 8081:
package com.limedrop.svc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import io.prometheus.client.exporter.HTTPServer;
import java.io.IOException;

@SpringBootApplication
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);

                try {
                    HTTPServer server = new HTTPServer(8081);
                } catch (IOException e) {
                    e.printStackTrace();
                }
    }

}
  1. Rerun the application to make sure it compiles:
./gradlew clean bootRun

While the application is still running, you should be able to access the /limesAvailable endpoint at http://<Prometheus Server Public IP>:8080. You can view the metrics at http://<Prometheus Server Public IP>:8081/metrics.

You should be able to access the /limesAvailable endpoint and see the total_requests counter increase each time you access the endpoint.

Test your setup by configuring Prometheus to scrape from your application.
  1. Edit the Prometheus config:
sudo vi /etc/prometheus/prometheus.yml
  1. Add a scrape config to scrape metrics for your app:
scrape_configs:

  ...

  - job_name: 'LimeDrop Java Svc'
    static_configs:
    - targets: ['localhost:8081']
  1. Restart Prometheus to reload the config:
sudo systemctl restart prometheus
  1. Run your Java app and leave it running to allow Prometheus to collect metrics:
./gradlew clean bootRun
  1. Access Prometheus in a browser at http://<Prometheus Server Public IP>:9090. Run queries to see the metrics you are collecting for your Java app:
total_requests

inprogress_requests

Additional Resources

Your company, LimeDrop, is building a RESTful web service using Java. They would like to implement Prometheus monitoring for this application. Since you are the Prometheus expert, the developers have asked you to implement some basic Prometheus monitoring in the Java code. Currently, the application has only one endpoint called /limesAvailable. You will need to implement monitoring around the usage of this endpoint.

Use the Prometheus Java client libraries to collect metrics for the /limesAvailable endpoint and expose a metrics endpoint that Prometheus can scrape. Then, test it by configuring Prometheus to scrape metrics from the app.

Add instrumentation to track the following metrics for the /limesAvailable endpoint:

  • total_requests (counter) — The total number of requests processed by the /limesAvailable endpoint during the lifetime of the application process.
  • inprogress_requests (gauge) — The number of requests currently being processed by the limesAvailable endpoint.

Some additional information you will need to be aware of:

  • The Java source code can be found on the Prometheus server at /home/cloud_user/content-prometheusdd-limedrop-svc.
  • From the project directory, you can run the application with the command ./gradlew clean bootRun.
  • While the application is running, you can access it using port 8080 on the Prometheus server.
  • Inside the Java project, the logic for the /limesAvailable endpoint can be found in src/main/java/com/limedrop/svc/LimeDropController.java.
  • The main Application class is src/main/java/com/limedrop/svc/App.java.
  • You can also find the application source code in GitHub at https://github.com/linuxacademy/content-prometheusdd-limedrop-svc. Check the example-solution branch for an example of the code changes needed to complete this lab.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Get Started
Who’s going to be learning?

How many seats do you need?

  • $499 USD per seat per year
  • Billed Annually
  • Renews in 12 months

Ready to accelerate learning?

For over 25 licenses, a member of our sales team will walk you through a custom tailored solution for your business.


$2,495.00

Checkout
Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!