Sub topic 7
Micrometer and the metrics endpoint
This chapter assumes you have Actuator on the classpath and know how to expose endpoints over HTTP, as covered in the earlier chapters. You should also be comfortable reading JSON.
What Micrometer is
Micrometer is a metrics facade. Your code and the Spring Boot libraries record measurements through the Micrometer API. A backend-specific registry then stores those measurements and publishes them in the format its monitoring system expects. Actuator auto-configures a MeterRegistry bean and binds many built-in meters to it.
A meter is one named measurement. The common types are:
- Counter: a value that only increases, such as requests served.
- Timer: counts events and records how long they took.
- Gauge: a value that goes up and down, such as queue size.
- DistributionSummary: like a timer, but for sizes such as payload bytes.
Exposing the endpoint
The metrics endpoint is enabled by default but not exposed over HTTP. Add it to the exposure list:
management:
endpoints:
web:
exposure:
include: health,metrics
metrics:
tags:
application: orders-serviceThe management.metrics.tags property adds a common tag to every meter. Common tags let you tell instances and services apart in a shared monitoring system.
Browsing meters
Request /actuator/metrics to get the list of meter names Boot has registered:
curl -s http://localhost:8080/actuator/metricsThe response looks like this, shortened:
{
"names": [
"http.server.requests",
"jvm.memory.used",
"jvm.gc.pause",
"system.cpu.usage",
"process.uptime"
]
}To read one meter, append its name:
curl -s http://localhost:8080/actuator/metrics/jvm.memory.used{
"name": "jvm.memory.used",
"baseUnit": "bytes",
"measurements": [{ "statistic": "VALUE", "value": 1.2E8 }],
"availableTags": [
{ "tag": "area", "values": ["heap", "nonheap"] },
{ "tag": "id", "values": ["G1 Eden Space", "Metaspace"] }
]
}measurements holds the values. availableTags lists the dimensions you can filter on. Repeat the tag query parameter to narrow the result:
curl -s "http://localhost:8080/actuator/metrics/jvm.memory.used?tag=area:heap"A timer such as http.server.requests returns three statistics: COUNT, TOTAL_TIME, and MAX. Divide total time by count to get the mean latency. You can filter by tags such as uri, method, and status.
Built-in meters
Boot registers meters for the JVM (memory, garbage collection, threads, class loading), the system (CPU, uptime), Tomcat, the HikariCP connection pool, and every HTTP request handled by Spring MVC or WebFlux. Several of these appear only after the relevant component has been used. A meter such as http.server.requests is missing from the list until the first request arrives.
Common errors
- 404 on
/actuator/metrics. The endpoint is not inmanagement.endpoints.web.exposure.include. - 404 on
/actuator/metrics/some.name. The meter is not registered yet, or the name is misspelled. Check the names list first. - Empty result with a tag filter. The tag value does not match exactly. Compare against
availableTags. - Metric names differ in Prometheus. Prometheus uses underscores, so
jvm.memory.usedbecomesjvm_memory_used_bytes. The Prometheus chapter covers this. - Tags with unbounded values. Do not use user IDs or raw URLs as tag values. Each distinct value creates a new time series, which increases memory use and monitoring costs.
Exercise
You call /actuator/metrics/http.server.requests and see availableTags containing "status" with values 200 and 500. How do you read only the statistics for server errors, and how do you compute the average latency for them?
Show answer
Request /actuator/metrics/http.server.requests?tag=status:500. In the response, divide the TOTAL_TIME measurement by the COUNT measurement to get the mean duration of those requests.