In the era of microservices and cloud-native applications, observability is not optional β it’s essential. While logs and traces are common, metrics are the fastest way to understand the real-time state of your system. Thatβs where Micrometer and Prometheus shine.
This post dives deep into how to implement Micrometer Prometheus Observability in Spring Boot, enabling developers and DevOps teams to collect, monitor, and visualize metrics effectively. We’ll set up a complete pipeline from exposing metrics with Micrometer to scraping and visualizing them with Prometheus.

π What is Observability?
Observability is the capability to understand the internal states of a system by examining the outputs. It’s composed of:
- Metrics β quantitative data (e.g., request count, memory usage)
- Logs β timestamped records of events
- Traces β the flow of requests across services
In this post, we focus on metrics, leveraging Micrometer and Prometheus.
π§© What is Micrometer?
Micrometer is a metrics facade in Spring Boot that provides a simple way to collect JVM, system, and application-level metrics. It supports multiple backends like:
- Prometheus
- New Relic
- Datadog
- Graphite
- Elastic
Spring Boot Actuator integrates Micrometer out of the box to expose metrics endpoints.
π¦ What is Prometheus?
Prometheus is a time-series database designed for monitoring. It scrapes metrics exposed by applications and provides a powerful query language (PromQL) and integrates well with Grafana.
π οΈ Setting up Spring Boot with Micrometer and Prometheus
π Maven Dependencies
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> |
βοΈ Application Configuration
application.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
management: endpoints: web: exposure: include: health,info,prometheus endpoint: prometheus: enabled: true metrics: export: prometheus: enabled: true |
π§ͺ Custom Metrics with Micrometer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package com.kscodes.springboot.advanced; import io.micrometer.core.instrument.MeterRegistry; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; @Component public class CustomMetricRegistrar { private final MeterRegistry meterRegistry; public CustomMetricRegistrar(MeterRegistry meterRegistry) { this.meterRegistry = meterRegistry; } @PostConstruct public void initMetrics() { meterRegistry.gauge("custom.orders.active", 10); meterRegistry.counter("custom.orders.completed").increment(); } } |
This code registers a gauge and counter metric that Prometheus can scrape.
π Exposing Metrics Endpoint
Once your application is running, you can access:
1 2 3 4 |
http://localhost:8080/actuator/prometheus |
This endpoint will expose all JVM, system, and custom metrics.
π Prometheus Configuration
Create a file called prometheus.yml
:
1 2 3 4 5 6 7 8 9 10 11 |
global: scrape_interval: 15s scrape_configs: - job_name: 'spring-boot-app' metrics_path: '/actuator/prometheus' static_configs: - targets: ['host.docker.internal:8080'] |
Use host.docker.internal
if Prometheus runs in Docker and your app runs locally.
π³ Running Prometheus with Docker
1 2 3 4 5 6 7 |
docker run -d \ -p 9090:9090 \ -v $(pwd)/prometheus.yml:/etc/prometheus/prometheus.yml \ prom/prometheus |
Visit http://localhost:9090
to access Prometheus UI.
π Querying Metrics in Prometheus
Try some PromQL queries:
custom_orders_active
custom_orders_completed_total
http_server_requests_seconds_count
You can now create alerts or forward these to Grafana for dashboards.
π Adding Grafana for Visualization (Optional)
1 2 3 4 |
docker run -d -p 3000:3000 grafana/grafana |
Login to Grafana (admin/admin
)
Add Prometheus as a data source
Import dashboard ID 4701 for Spring Boot metrics
π§Ό Best Practices
- Use tags (labels) wisely for fine-grained metrics
- Avoid unbounded high-cardinality metrics
- Monitor metrics like:
- JVM memory
- CPU usage
- HTTP request latency
- GC time
- Combine with distributed tracing for full observability
π Conclusion
Micrometer Prometheus Observability in Spring Boot provides a seamless way to monitor the performance, reliability, and health of your applications. By combining Spring Boot Actuator, Micrometer, Prometheus, and optionally Grafana, you get a powerful monitoring stack that’s production ready.
Whether you’re scaling microservices or improving SLAs, metrics play a critical role. Implement this observability pipeline today to build resilient, transparent systems.