Integrating Micrometer, Prometheus, and Grafana into a Spring Boot Application
This tutorial demonstrates how to add Micrometer to a Spring Boot project, configure JVM and custom metrics, expose them via Actuator, and then integrate Prometheus and Grafana to collect and visualize the monitoring data, providing a complete end‑to‑end observability solution.
In this guide a senior architect walks through the process of adding Micrometer monitoring to a Spring Boot backend, exposing metrics through Actuator, and then visualizing them with Prometheus and Grafana.
1. Spring Boot Project Integration with Micrometer
1.1 Add Dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>1.2 Configuration
management.server.port=9003
management.endpoints.web.exposure.include=*
management.endpoint.metrics.enabled=true
management.endpoint.health.show-details=always
management.endpoint.health.probes.enabled=true
management.endpoint.prometheus.enabled=true
management.metrics.export.prometheus.enabled=true
management.metrics.tags.application=voice-qc-backendThe management.endpoints.web.exposure.include=* line enables all Actuator endpoints, including the /actuator/prometheus endpoint that Prometheus will scrape.
The management.metrics.tags.application property adds an application tag to all exported metrics, making it easier to differentiate services in Prometheus.
1.3 Monitor JVM Information
Add a bean in the main application class to expose common tags:
@SpringBootApplication
public class GatewayDatumApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayDatumApplication.class, args);
}
@Bean
MeterRegistryCustomizer<MeterRegistry> configurer(@Value("${spring.application.name}") String applicationName) {
return registry -> registry.config().commonTags("application", applicationName);
}
}1.4 Create Custom Metrics
Define a component that registers a counter and a timer for a specific business operation:
package com.lianxin.gobot.api.monitor;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Timer;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component
public class PrometheusCustomMonitor {
@Getter
private Counter reportDialRequestCount;
@Value("${lx.call-result-report.url}")
private String callReportUrl;
@Getter
private Timer reportDialResponseTime;
private final MeterRegistry registry;
@Autowired
public PrometheusCustomMonitor(MeterRegistry registry) {
this.registry = registry;
}
@PostConstruct
private void init() {
reportDialRequestCount = registry.counter("go_api_report_dial_request_count", "url", callReportUrl);
reportDialResponseTime = registry.timer("go_api_report_dial_response_time", "url", callReportUrl);
}
}1.5 Add Business‑Level Monitoring
Instrument the business code to count requests and record response time:
// Increment request counter
prometheusCustomMonitor.getReportDialRequestCount().increment();
long startTime = System.currentTimeMillis();
String company = HttpUtils.post(companyUrl, "");
// Record response time
long endTime = System.currentTimeMillis();
prometheusCustomMonitor.getReportDialResponseTime().record(endTime - startTime, TimeUnit.MILLISECONDS);After starting the application, visit http://127.0.0.1:9001/actuator/prometheus to see metrics such as jvm_memory_used_bytes , jvm_gc_memory_promoted_bytes_total , and the custom go_api_report_dial_request_count .
2. Integrate Prometheus
2.1 Installation
docker pull prom/prometheus mkdir /usr/local/prometheus vi prometheus.yml # my global config
global:
scrape_interval: 15s # Every 15 seconds
evaluation_interval: 15s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['192.168.136.129:9090']
- job_name: "metricsLocalTest"
metrics_path: "/actuator/prometheus"
static_configs:
- targets: ["localhost:9003"] docker run -d --name prometheus -p 9090:9090 -v /usr/local/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus2.2 Configuration
The localhost:9003 target points to the Spring Boot Actuator endpoint that exposes Micrometer metrics. Adding labels (e.g., application name) helps differentiate metrics when querying with PromQL.
3. Visualize Metrics with Grafana Dashboard
3.1 Install Grafana
docker pull grafana/grafana docker run -d --name grafana -p 3000:3000 -v /usr/local/grafana:/var/lib/grafana grafana/grafanaDefault login is admin/admin .
3.2 Configure Prometheus Data Source
In Grafana UI, add a new data source of type Prometheus pointing to http://localhost:9090 .
3.3 Add JVM Dashboard
Import dashboard template ID 4701 to display JVM metrics.
3.4 Add Business Interface Monitoring Panel
Create custom panels that query the custom counters and timers, such as go_api_report_dial_request_count and go_api_report_dial_response_time .
With Micrometer, Prometheus, and Grafana fully integrated, the Spring Boot service now provides comprehensive observability for JVM health and business‑level performance.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.