Spring Boot Actuator: health, metrics and production monitoring
Actuator run on Spring Boot 4.0.5: which endpoints are open by default, how to expose and protect them, a custom health indicator and liveness/readiness groups for Kubernetes, Micrometer metrics (timers, percentiles, cardinality), changing log levels at runtime, and which numbers to alert on.
On this page
Actuator lets you look inside a running Spring Boot app without SSH: is it alive, can it take traffic, where is it slow. This post configures Actuator safely, writes your own health indicators and metrics, and wires them into Kubernetes probes. Tracing is covered in distributed tracing with OpenTelemetry.
1. The problem
The DB slows down for 30 seconds. The liveness probe of 20 pods checks the DB, all fail, and Kubernetes restarts all 20. On startup, 20 pods open their pools at once, load the DB further, fail again, restart again.
2. Key idea
Actuator is the app's dashboard. It belongs in the cockpit: an internal port that only Prometheus and the kubelet can reach.
| Liveness | Readiness | |
|---|---|---|
| Question | Does the container need a restart? | Should requests go to this pod? |
| On failure Kubernetes | kills and restarts the pod | removes the pod from the Service, no restart |
| Contains | livenessState |
readinessState + required dependencies (DB) |
| Doesn't contain | DB, Redis, external APIs | optional dependencies |
2.1 Which endpoints to open?
| Endpoint | Use when | Don't use when |
|---|---|---|
health, info |
always: probes, knowing which build runs | |
prometheus, metrics |
a system scrapes metrics | on a public port |
loggers |
changing one instance's log level while debugging | without authentication |
env, heapdump, threaddump |
incident debugging, behind a dedicated role | exposure.include=*: the heap holds passwords and tokens |
2.2 Which metrics, which tags?
Counter: a count that only grows.Gauge: a current value (queue length).Timer: count and time distribution.DistributionSummary: distribution of other values (payload size).- Unbounded tags (
userId, raw URL, error message) explode the series count: 10,000 requests become 10,000 series.channel(3 values) ×outcome(2) makes just 6. - 2% of requests at 900 ms push p99 to 905 ms while the average is 38 ms. An average-based alert never fires.
- With many pods, enable
percentiles-histogramand let Prometheus runhistogram_quantileover the summed buckets.
3. In Java
@Service
class OrderMetrics {
private final MeterRegistry registry;
OrderMetrics(MeterRegistry registry) { this.registry = registry; }
void placed(String channel, boolean rejected) {
Counter.builder("orders.placed")
.tag("channel", channel) // web, app, pos
.tag("outcome", rejected ? "rejected" : "ok")
.register(registry).increment();
}
}
// application.properties
// management.server.port=8081
// management.endpoints.web.exposure.include=health,info,prometheus
// management.endpoint.health.group.readiness.include=readinessState,db
// -> /actuator/health/liveness 200, /actuator/health/readiness 503 when the DB is downOther languages: Go and Node.js use the Prometheus or OpenTelemetry client, with the same tag rules.
4. Working with AI
Give the AI: Actuator config, probes, a SecurityFilterChain for EndpointRequest, a RED dashboard (Rate, Errors, Duration).
Check in AI-written code:
- No
exposure.include=*; a test assertsGET /actuator/heapdumpon the public port returns 404. - The liveness group holds only
livenessState. - No tag comes from
userId, ids, raw URLs orex.getMessage(). - Meters have the right type: no
gaugefor "amount per payment". - Alerts use 5xx rate and p99, not averages.
5. Exercises
Exercise 1: Probes for an ordering service
6 pods, depending on MySQL (required), a Redis cache (on failure, read the DB directly) and a payment gateway (needed only for /checkout). What goes into the liveness and readiness groups, and why?
Solution to Exercise 1
management.endpoint.health.group.liveness.include=livenessState
management.endpoint.health.group.readiness.include=readinessState,db
management.health.redis.enabled=false- Liveness has only
livenessState: a restart can't fix MySQL. - Readiness has
db: without MySQL the pod can serve nothing. - Redis is out of health: it's optional, and including it reports DOWN while the app still works.
- The payment gateway stays out of readiness: otherwise a gateway outage removes all 6 pods and users lose order viewing too. Use a circuit breaker for
/checkout. - Add a
startupProbeso liveness doesn't kill a slow-starting pod.
Exercise 2: Review a metric
registry.counter("payment", "user", userId, "url", request.getRequestURI(),
"error", ex == null ? "" : ex.getMessage()).increment();
registry.gauge("payment.amount", amount);The AI says this is "enough to monitor payments". Point out the problems.
Solution to Exercise 2
user, rawurlandex.getMessage()are unbounded tags: every user and every id is a new series.- The name
paymentis vague; it doesn't say what it measures. - A
gaugeholds the current value of an object, not "the amount of each payment"; use aDistributionSummary.
Fix: a Timer named payment.requests with tags method (charge, refund), outcome, and error = the exception class name (a finite set). userId and the full message go into logs with a traceId.
Summary
- Boot exposes only
health; neverinclude=*in production. - Actuator sits on an internal port; authenticate everything except
healthandinfo. - Liveness is internal state only; the DB goes into readiness.
- Tags need a small value set;
userIdbelongs in logs. - Alert on p99 and error rate.
Read more: Observability for LLM calls.
Self-compiled notes from official documentation and hands-on practice.
Practice interview questions on this topic
- What is Spring Boot Actuator, which endpoints are commonly used, and how do you secure it in production?
- How do Counter, Gauge and Timer differ in Micrometer? How would you set metrics and alerts for a payment API?
- How do readiness, liveness and startup probes differ? How do you configure them for Spring Boot?
Comments
Community rules