Operations 10 min read

Spring Boot Monitoring with Prometheus and Grafana: A Step‑by‑Step Guide

This article provides a comprehensive tutorial on setting up Spring Boot application monitoring using Prometheus and Grafana, covering project creation, dependency configuration, security setup, Prometheus server installation, Grafana dashboard creation, email alerting configuration, and testing the end‑to‑end alert workflow.

Top Architect
Top Architect
Top Architect
Spring Boot Monitoring with Prometheus and Grafana: A Step‑by‑Step Guide

Spring Boot offers many monitoring solutions; the combination of Spring Boot, Prometheus, and Grafana is a popular choice. The article begins by creating a Spring Boot project and adding the necessary Maven dependencies for Actuator, Web, Lombok, Prometheus client, and security:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>
<dependency>
    <groupId>io.prometheus</groupId>
    <artifactId>simpleclient_spring_boot</artifactId>
    <version>0.8.1</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

Because the tutorial uses Spring Boot 1.5.7.RELEASE (required for the chosen Prometheus client), the main application class is annotated with @EnablePrometheusEndpoint, @EnableSpringBootMetricsCollector, and @SpringBootApplication:

@EnablePrometheusEndpoint
@EnableSpringBootMetricsCollector
@SpringBootApplication
public class MonitorDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(MonitorDemoApplication.class, args);
    }
}

The application.yml configures the server port, application name, basic security (admin/1234), and separates the Actuator management endpoints on port 8888 under the /admin context path:

server:
  port: 8848
spring:
  application:
    name: monitor-demo
security:
  user:
    name: admin
    password: 1234
  basic:
    enabled: true
    path: /admin
management:
  context-path: /admin
  port: 8888
  security:
    enabled: true
    roles: SUPERUSER

A test controller is added to generate heap usage for alert testing:

@RequestMapping("/heap/test")
@RestController
public class TestController {
    public static final Map<String, Object> map = new ConcurrentHashMap<>();
    @RequestMapping("")
    public String testHeapUsed() {
        for (int i = 0; i < 10000000; i++) {
            map.put(i + "", new Object());
        }
        return "ok";
    }
}

After starting the application, the Actuator endpoints become visible in IDE IDEA (requires version 2017.2 or later). Accessing http://localhost:8888/admin/prometheus shows the exposed metrics.

Next, Prometheus is installed (Windows version prometheus‑2.17.2.windows‑amd64.tar.gz) and its prometheus.yml is edited to scrape the Spring Boot metrics:

scrape_configs:
  - job_name: 'monitor-demo'
    scrape_interval: 5s
    scrape_timeout: 5s
    metrics_path: /admin/prometheus
    scheme: http
    basic_auth:
      username: admin
      password: 1234
    static_configs:
      - targets: ['127.0.0.1:8888']

Running prometheus.exe --config.file=prometheus.yml and visiting http://localhost:9090/targets confirms that the Spring Boot job is being scraped.

Grafana is then downloaded ( grafana‑6.3.3.windows‑amd64.zip), started with grafana-server.exe, and accessed at http://localhost:3000 (default admin/admin). A Prometheus data source pointing to http://localhost:9090 is added, followed by the creation of a dashboard that visualises the collected metrics.

To enable email alerts, a notification channel of type Email is created in Grafana. The SMTP settings are placed in a custom custom.ini (example uses Tencent enterprise mail):

[smtp]
enabled = true
host = smtp.exmail.qq.com:465
user = [email protected]
password = XXX
skip_verify = true
from_address = [email protected]
from_name = Grafana
ehlo_identity = ininin.com

After restarting Grafana, alert rules are defined on the dashboard (e.g., trigger when heap usage exceeds a threshold). The rule’s evaluation interval is set to 1 second for testing, and the “For” clause ensures alerts fire only after the condition persists. Notification settings specify the previously created email channel.

Finally, invoking http://localhost:8848/heap/test raises heap usage, causing the alert to transition to the “Alerting” state and an email is sent, demonstrating the complete monitoring‑alerting pipeline.

Additional resources such as the official Prometheus and Grafana documentation and the GitHub repository https://github.com/2YSP/monitor-demo are provided for further reference.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendjavamonitoringAlertingSpring Boot
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.