CodeGym /Courses /Module 5. Spring /Lecture 117: Integrating Actuator with monitoring systems...

Lecture 117: Integrating Actuator with monitoring systems (Prometheus, Grafana)

Module 5. Spring
Level 19 , Lesson 6
Available

We already know that Spring Boot Actuator gives us a convenient and extensive set of metrics. That said, if you're working on a big project, just exposing metrics via Actuator endpoints might not cut it. In real systems you want centralized monitoring across all microservices, nice visualizations, and the ability to react quickly when stuff breaks.

This is where Prometheus — a system for collecting and storing metrics — and Grafana — a data visualization tool — come into play. Prometheus integrates with Actuator to scrape metrics, and Grafana helps you visualize them on dashboards.

These tools will let you:

  • Consolidate monitoring for all your apps in one place.
  • Track long-term performance trends.
  • Get alerted on failures or when critical thresholds are breached.

So buckle up — we're heading into the world of integrations!


Setting up Prometheus

Installing Prometheus

First, you need to have Prometheus. If you don't have it yet, Docker is the fastest way to spin it up:

docker run -d --name=prometheus --network=host prom/prometheus

Preparing your Spring Boot app

Prometheus scrapes metrics from a special endpoint at /actuator/prometheus. To expose that endpoint you need to add the Micrometer Prometheus library to your app.

In pom.xml add the following dependency:

<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
    <version>1.10.3</version>
</dependency>

For Gradle it looks like this:

implementation 'io.micrometer:micrometer-registry-prometheus:1.10.3'

Then, in your application.properties tell Spring to expose the Prometheus endpoint:

management.endpoints.web.exposure.include=health,info,prometheus

After you start the app, you should see that the /actuator/prometheus endpoint is available.


2.3 Configuring Prometheus to scrape metrics

Now that the endpoint is exposed, you need to tell Prometheus where to fetch the metrics from. In the prometheus.yml config add your Spring Boot service to the targets list:

scrape_configs:
  - job_name: 'spring-boot-app'
    scrape_interval: 5s
    static_configs:
      - targets: ['localhost:8080']
        labels:
          service: 'my-spring-boot-app'

Hit pause and check: if Prometheus connected successfully, you can view the metrics at http://localhost:9090.


3. Why Grafana?

Prometheus is great at collecting data, but its UI isn't exactly user-friendly. For visualization we'll use Grafana — it gives you beautiful interactive dashboards. With Grafana you can:

  • Create dashboards with line charts, histograms, tables, and other visualizations.
  • Set up alerts (for example, when CPU usage exceeds a threshold).
  • Analyze data from seconds up to months.

Setting up Grafana

Installing Grafana

If you don't have Grafana installed yet, again Docker is the easiest option:

docker run -d -p 3000:3000 grafana/grafana

Grafana will be available at http://localhost:3000.

Default login and password: admin/admin.


Connecting Prometheus to Grafana

  1. Log into the Grafana UI.
  2. Go to Configuration > Data Sources.
  3. Click "Add data source" and pick Prometheus.
  4. In the URL field enter your Prometheus instance address (for example, http://localhost:9090).
  5. Click Save & Test to make sure the connection works.

Creating a dashboard

Now let's create a simple dashboard to track, for example, memory usage and CPU load.

Step 1: create a new dashboard 1. Go to Dashboards and choose New Dashboard. 2. Click Add New Panel to add a chart.

Step 2: configure the panel 1. In the Query field enter a PromQL query. For example:

process_cpu_usage

This query will show CPU usage for our app.

  1. Pick the chart type (line, histogram, etc.) and tweak other settings.
  2. Add another panel to show memory usage:
    jvm_memory_used_bytes
    
  3. Save your changes.

Step 3: View the dashboard Now you can enjoy a pretty visualization! Here's an example of how it might look:


+------------------------------------+
| Memory Usage       | CPU Usage     |
+------------------------------------+
| JVM: 512 MB        | CPU: 12%      |
| ...dynamic graphs...| ...updates...|
|                    |               |
+------------------------------------+

5. Setting up alerts

Grafana lets you set up alerts so you get notified when critical values are reached (e.g., via email or Slack). Here's how to do it:

  1. Open any panel on your dashboard.
  2. Click the panel and choose Edit > Alerts.
  3. Add a new alert policy. For example, create a rule: if process_cpu_usage > 80%, then send a notification.
  4. Configure the notification endpoint (for example, email or Slack).

6. Hands-on task

To get some hands-on experience, do the following:

  1. Configure Prometheus to scrape metrics from your Spring Boot application.
  2. Connect Grafana to Prometheus and create a dashboard with two panels:
    • JVM memory usage.
    • CPU load.
  3. Set up an alert policy to notify you when CPU usage exceeds 90%.

Example outcome: your Grafana dashboard with live metric charts.


Now that you've got Spring Boot Actuator talking to Prometheus and Grafana, ops life gets easier and your app becomes way more transparent. A little setup and you're basically the monitoring overlord!

Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION