Learnitweb

Setting Up Prometheus to Monitor Spring Boot Metrics

What Is Prometheus?

Prometheus is an open-source monitoring system and time-series database. It performs two main tasks:

  • Scraping Metrics: It gathers metrics at regular intervals from the configured targets (in our case, the /actuator/prometheus endpoint).
  • Storing Metrics: It persists these metrics into its own time-series database for querying and visualization.

Prometheus is an open-source monitoring system and time-series database. It performs two main tasks:

  • Scraping Metrics: It gathers metrics at regular intervals from the configured targets (in our case, the /actuator/prometheus endpoint).
  • Storing Metrics: It persists these metrics into its own time-series database for querying and visualization.

Additionally, Prometheus can:

  • Evaluate rules: You can define conditions (rules) like “if a request takes more than 1 minute, trigger an alert.”
  • Trigger alerts: Prometheus works with Alertmanager to send notifications (emails, Slack messages, etc.) when those rules are violated.

How Prometheus Works with Spring Boot and Micrometer

Here’s the basic flow:

  1. Micrometer exposes metrics on the /actuator/prometheus endpoint.
  2. Prometheus scrapes the metrics from that endpoint at a specified scrape interval.
  3. Prometheus stores the metrics in its database.
  4. (Optional) Rules and alerts can be configured based on metric conditions.
  5. Alertmanager can then send notifications if necessary.

Prometheus Configuration (prometheus.yml)

To tell Prometheus where and how often to scrape metrics, you must create a configuration file, typically named prometheus.yml.

Here’s an example prometheus.yml file:

global:
  scrape_interval: 5s  # Collect metrics every 5 seconds

scrape_configs:
  - job_name: 'spring-boot-app'
    metrics_path: '/actuator/prometheus'
    static_configs:
      - targets: ['localhost:8080']
    basic_auth:
      username: 'admin'
      password: 'password'

  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']

Explanation:

  • global.scrape_interval: Tells Prometheus to pull metrics every 5 seconds.
  • scrape_configs:
    • spring-boot-app: This tells Prometheus to scrape metrics from the Spring Boot app running on localhost:9191.
      • It fetches metrics from /actuator/prometheus.
      • Basic authentication is configured if your API is secured.
    • prometheus: Prometheus also monitors itself (its own metrics are available at localhost:9090).

Note:
If your Spring Boot app is secured, you must configure authentication. If it’s not secured, you can omit the basic_auth section.

Installing Prometheus Using Docker

We will now install and run Prometheus server using Docker for simplicity.

Step 1: Pull the Prometheus Image

In your terminal, run:

docker pull prom/prometheus

This command pulls the latest Prometheus image from Docker Hub.

Step 2: Run the Prometheus Container

After pulling the image, start the container with the following command:

docker run -d \
  --name prometheus \
  -p 9090:9090 \
  -v /path/to/your/prometheus.yml:/etc/prometheus/prometheus.yml \
  prom/prometheus

Explanation of the command:

  • -d: Runs the container in detached mode (in the background).
  • --name prometheus: Names the container prometheus.
  • -p 9090:9090: Maps container’s port 9090 to your local machine’s port 9090.
  • -v /path/to/your/prometheus.yml:/etc/prometheus/prometheus.yml: Mounts your custom prometheus.yml configuration file into the container.

Make sure to replace /path/to/your/prometheus.yml with the actual path where you have saved your configuration file.

Step 3: Verify Prometheus is Running

After running the container, open your browser and go to:

http://localhost:9090

You should see the Prometheus UI!

Checking the Status of Targets

Once Prometheus is up, you can:

  • Click on Status → Targets from the top menu.
  • You will see the list of configured targets.

For example:

  • Your Spring Boot application (e.g., Bible API on port 9191) should appear as UP.
  • The Prometheus server itself should also appear as UP.

If you stop your Spring Boot app (from IntelliJ or wherever it’s running), you’ll see the state of that target change to DOWN in the Prometheus UI.

A Few Important Notes

  • Multiple Targets: Prometheus can monitor many applications at once by adding more targets in prometheus.yml.
  • Authentication: If your Spring Boot app is protected (Basic Auth, Token Auth, etc.), you must configure Prometheus accordingly.
  • Real-world configuration: The example provided is basic. In production, your prometheus.yml might have:
    • Alertmanager configurations.
    • Relabeling rules.
    • External storage settings.
    • Clustered Prometheus servers (for high availability).

Install Prometheus Using Native Windows Binary

Prometheus also provides Windows binaries that you can run natively without Docker or WSL.

Step-by-Step Guide:

  • Download the Prometheus Binary for Windows:
    • Go to the Prometheus Downloads page.
    • Download the latest Windows binary (prometheus-*.windows-amd64.zip).
  • Extract the Binary:
    • After downloading, extract the .zip file to a location of your choice (e.g., C:\prometheus).
  • Create a Configuration File: You’ll need a prometheus.yml configuration file. You can create it using the following content (same as in the Docker method):
global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']

Save this file as prometheus.yml inside the C:\prometheus directory.

  • Run Prometheus: Open Command Prompt and navigate to the Prometheus folder:
cd C:\prometheus

Run the Prometheus server:

prometheus.exe --config.file=prometheus.yml

Prometheus will start, and you should see output indicating that it is running.

  • Access Prometheus: Open your browser and navigate to http://localhost:9090 to access the Prometheus UI.