Spring Boot Actuator Metrics
Spring Boot Actuator exposes application and system measurements through its metrics endpoint. In a typical Spring Boot application, these measurements are collected through Micrometer and can be used for monitoring, dashboards, and alerting.
Spring Boot Actuator provides operational endpoints for observing a running application. The `metrics` endpoint exposes measurements such as JVM memory, CPU usage, HTTP request counts, and other metrics supplied by the application's instrumentation.
Metrics are organized by name. For example, `jvm.memory.used` represents JVM memory usage, while HTTP server metrics can describe request activity. The endpoint can also provide details for a particular metric when its name is included in the request.
Spring Boot uses Micrometer as its metrics facade. Micrometer records measurements and can connect them to monitoring systems such as Prometheus, while Actuator provides an HTTP interface for inspecting metrics directly.
The endpoint is commonly exposed at `/actuator/metrics`, assuming Actuator is included and the endpoint is exposed through the application's management configuration. Production applications should treat actuator endpoints as operational interfaces and configure exposure and access appropriately.
Key points
- `/actuator/metrics` lists available metric names.
- `/actuator/metrics/{name}` retrieves details for a specific metric.
- Spring Boot integrates Actuator with Micrometer for metrics instrumentation.
- Metrics can include JVM, system, HTTP, and application measurements.
- Actuator endpoint exposure and access should be configured deliberately in production.
Interview questions
What is the purpose of Spring Boot's Actuator metrics endpoint?
It provides access to measurements collected from a running Spring Boot application. The endpoint can list available metric names and can return details for an individual metric. These measurements can cover areas such as JVM memory, CPU, HTTP requests, and application instrumentation. This makes the endpoint useful for inspecting application behavior and operational state.
How would you inspect JVM memory usage with Actuator?
First, I would inspect `/actuator/metrics` to find the available metric names. A JVM memory metric such as `jvm.memory.used` can then be requested through the corresponding metric endpoint. The response contains the measurement along with metadata and, where applicable, dimensions such as memory area. The exact available metrics depend on the application's instrumentation and environment.
What is Micrometer's role in Spring Boot metrics?
Micrometer provides a vendor-neutral metrics API and instrumentation model. Spring Boot integrates with Micrometer so applications and supported components can produce measurements without being tightly coupled to one monitoring backend. Actuator can expose those measurements through management endpoints. Micrometer can also publish metrics to monitoring systems through supported registries.
Why might a metric you expect not appear in the Actuator metrics endpoint?
The relevant instrumentation may not be present because the required dependency or component is missing. The metric may also only be created after the relevant code path or component has been initialized. Configuration, conditional instrumentation, and application version can also affect what is available. I would first inspect the metric list and then verify the relevant dependency and instrumentation configuration.
How should Actuator endpoints be handled in production?
Actuator endpoints should be exposed deliberately rather than treating every management endpoint as public application functionality. Access should be controlled using appropriate security and network configuration, and only required endpoints should normally be exposed. Sensitive operational information should not be unnecessarily available to untrusted clients. The management interface can also be separated from the main application interface when the deployment architecture supports it.