Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
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
Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 1h 0m
Published
Clock icon Jun 12, 2020

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
    

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

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.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans