Featured resource
2025 Tech Upskilling Playbook
Tech Upskilling Playbook

Build future-ready tech teams and hit key business milestones with seven proven plays from industry leaders.

Check it out
  • Lab
    • Libraries: If you want this lab, consider one of these libraries.
    • Cloud
Google Cloud Platform icon
Labs

Using the Java Client Library for Prometheus

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

Google Cloud Platform icon
Lab platform
Lab Info
Level
Intermediate
Last updated
Jun 13, 2025
Duration
1h 0m

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
Table of Contents
  1. Challenge

    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
    
  2. Challenge

    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
    
  3. Challenge

    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.

  4. Challenge

    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
    
About the author

Pluralsight Skills gives leaders confidence they have the skills needed to execute technology strategy. Technology teams can benchmark expertise across roles, speed up release cycles and build reliable, secure products. By leveraging our expert content, skill assessments and one-of-a-kind analytics, keep up with the pace of change, put the right people on the right projects and boost productivity. It's the most effective path to developing tech skills at scale.

Real skill practice before real-world application

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.

Learn by doing

Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.

Follow your guide

All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.

Turn time into mastery

On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.

Get started with Pluralsight