Prometheus has certainly become the most used solution to monitor applications running in a distributed system such as Kubernetes.
It quite simple to expose metrics in a Spring Boot application that can be scraped using Prometheus.
Expose Metrics in Spring Boot
First we need to add the required dependencies to our build.
// build.gradle
compile 'io.micrometer:micrometer-registry-prometheus'
compile 'io.github.mweirauch:micrometer-jvm-extras:0.1.4'
Using configuration we activate the Prometheus actuator, add an application name, and a common application tag.
# application.properties
spring.application.name=Metrics Example
management.endpoints.web.exposure.include=health, prometheus
management.metrics.tags.application=${spring.application.name}
The library micrometer-jvm-extras adds, as it’s name suggests, some more metrics to the registry. To enable it, we have to add two additional beans into the application context.
@Bean
public MeterBinder processMemoryMetrics() {
return new ProcessMemoryMetrics();
}
@Bean
public MeterBinder processThreadMetrics() {
return new ProcessThreadMetrics();
}
You should now be able to access the metrics in Prometheus format using a simple HTTP call: curl -s localhost:8080/actuator/prometheus
Prometheus Configuration
Prometheus is a time-series database as well as a data collection agent. It can either use service discovery (such as Kubernetes, Consul, EC2, and others) or a static target configuration. The configuration sample below statically scrapes the hosts grafana, prometheus, and application every 15 seconds.
# prometheus.yaml
global:
scrape_interval: 15s
scrape_configs:
- job_name: spring-boot
metrics_path: /actuator/prometheus
static_configs:
- targets:
- application:8080
- job_name: grafana
static_configs:
- targets:
- grafana:3000
- job_name: prometheus
static_configs:
- targets:
- localhost:9090
Prometheus runs by default on port 9090 and when you have an active port mapping, you can access it on localhost:9090.

Grafana Configuration
Grafana supports Prometheus as well as many other data sources.
You can now query data using the Prometheus Query Language (PromQL).

Working Example
I’ve published the complete example on GitHub: gysel/spring-boot-metrics-prometheus. Prometheus is configured to scrape all the Spring Boot application as well as Grafana and itself. A Grafana dashboard is loaded automatically.