Sub topic 9
Exporting metrics to Prometheus
This chapter assumes you have Actuator running, know how to expose endpoints, and have seen the /actuator/metrics endpoint. Here you will turn those metrics into a format Prometheus can collect.
Pull-based monitoring
Prometheus is a monitoring system that pulls data. It does not wait for your application to send metrics. At a fixed interval, Prometheus sends an HTTP request (a scrape) to each target and stores the numbers it receives as time series.
Micrometer, the metrics library behind Actuator, supports this through a registry. A registry is the object that holds meters and knows how to format them. The Prometheus registry formats them as Prometheus text.
Add the registry
Add the Micrometer Prometheus registry next to the Actuator starter. Spring Boot detects it and configures it automatically.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<scope>runtime</scope>
</dependency>The dependency adds a /actuator/prometheus endpoint. Like other endpoints, it is not exposed over HTTP by default, so you must include it.
management:
endpoints:
web:
exposure:
include: health,info,metrics,prometheusRead the output
Request /actuator/prometheus and you get plain text. Each line is a sample, and # lines describe the metric.
# HELP orders_placed_total Number of orders placed
# TYPE orders_placed_total counter
orders_placed_total{channel="web"} 42.0
# HELP jvm_memory_used_bytes The amount of used memory
# TYPE jvm_memory_used_bytes gauge
jvm_memory_used_bytes{area="heap",id="G1 Eden Space"} 3.5651584E7Micrometer renames metrics to follow Prometheus conventions:
| Micrometer name | Prometheus name | Reason |
|---|---|---|
| `orders.placed` (counter) | `orders_placed_total` | Dots become underscores. Counters get a `_total` suffix. |
| `orders.processing` (timer) | `orders_processing_seconds_count`, `_sum`, `_max` | Timers use base unit seconds and expose several series. |
| `jvm.memory.used` (gauge) | `jvm_memory_used_bytes` | The base unit is added to the name. |
Tags become labels, such as channel="web". Each distinct label combination creates a separate time series.
Configure Prometheus to scrape
Tell Prometheus where to find the application. This example assumes the application listens on port 8080.
scrape_configs:
- job_name: "orders-app"
metrics_path: "/actuator/prometheus"
scrape_interval: 15s
static_configs:
- targets: ["localhost:8080"]If you moved Actuator to a separate management port, point targets at that port instead. If you secured the endpoint, add basic_auth credentials to the scrape job.
Common tags and errors
Add tags to every metric so you can tell instances apart in queries.
management:
metrics:
tags:
application: orders-appCommon problems:
- 404 on
/actuator/prometheus: the endpoint is not in the exposure list, or the registry dependency is missing. - Target shows as down: the port, path, or credentials in the scrape job do not match the application.
- Memory growth in Prometheus: a tag with unbounded values, such as a user ID, creates a new series per value. Keep tag values to a small, fixed set.
Exercise
Your counter is registered as payments.failed. What name do you query in Prometheus, and why is a user ID a bad tag for it?
Show answer
Query payments_failed_total, because Micrometer replaces dots with underscores and adds _total to counters. A user ID is a bad tag because every distinct value creates a new time series, which increases memory use in both the application and Prometheus.