Observability: Actuator & Micrometer
Expose health checks and metrics.
By Invicly Tech, , in Backend Java Development. About 9 minutes to read.
In short
Spring Boot Actuator adds endpoints for health, info and metrics. Micrometer records metrics in a vendor-neutral way, and Prometheus scrapes them so Grafana can graph and alert.
Explained simply
Once your app runs in production, you need to know if it is healthy and how it behaves. Is it up? Can it reach the database? How many requests per second does it serve, and how slow are they? Observability is the practice of answering those questions from the outside.
Spring Boot Actuator adds ready-made web endpoints for this, under /actuator. The health endpoint says UP or DOWN, checking things like the database and disk space. The info endpoint shows the app version. The metrics endpoints show numbers.
Micrometer is the library that records those numbers: request counts, response times, memory use, and any counters you add yourself. It is like a universal plug. You write metrics once, and Micrometer can send them to Prometheus, Datadog, CloudWatch and many others.
Prometheus is a popular open-source monitoring server. Every few seconds it visits your /actuator/prometheus page and stores the numbers over time. Grafana then draws graphs from them, and alert rules can wake someone up when errors spike.
Press Play on a diagram to walk through it one step at a time.
- 1Micrometer records
Request timers, JVM memory, custom counters
- 2Actuator exposes
/actuator/prometheus in text format
- 3Prometheus scrapes
Pulls metrics every 15 seconds or so
- 4Grafana graphs
Dashboards for latency, errors, traffic
- 5Alerts fire
Notify when error rate or latency spikes
- Is the process broken?
- Failing it restarts the container
- Keep it simple and internal
- Never depend on the database
- Can it take traffic now?
- Failing it removes the pod from load balancing
- DOWN during startup and shutdown
- May include key dependencies
In depth
Health: what UP really checks
The /actuator/health endpoint combines many health indicators into one status. Spring Boot adds indicators automatically for what is on the classpath: the database (it runs a validation query), disk space, Redis, Kafka and more. If any required one is DOWN, the whole status is DOWN and the endpoint returns HTTP 503.
By default the response shows only the overall status. Set management.endpoint.health.show-details to when-authorized or always to see each component, but do not show details publicly; they reveal your infrastructure.
For Kubernetes there are two groups. Liveness (/actuator/health/liveness) answers "is the process stuck and should it be restarted?" Readiness (/actuator/health/readiness) answers "should it receive traffic now?" Keep external checks like the database out of liveness, or a database outage will restart every pod and make things worse. The probes are enabled automatically on Kubernetes, or with management.endpoint.health.probes.enabled=true.
- liveness group
- livenessState
- No external systems
- readiness group
- readinessState
- Add db here if you want
- Auto indicators
- db
- diskSpace
- redis
- ping
- Your own
- A HealthIndicator bean
- e.g. payment provider reachable
Exposing endpoints safely
Actuator has many endpoints, but over HTTP only health is exposed by default. You opt in with management.endpoints.web.exposure.include, listing exactly what you need, for example health,info,prometheus. Avoid include: "*" on anything public: env shows configuration, heapdump downloads the whole memory, which can contain passwords and tokens.
A common production setup moves Actuator to a separate port with management.server.port=8081. The main port stays open to the internet, while 8081 is reachable only inside the cluster where Prometheus and the load balancer live. You can also protect endpoints with Spring Security, allowing health without login and requiring a role for the rest.
The info endpoint is empty until you feed it. The build-info goal of the Spring Boot Maven plugin adds the version and build time, and the git-commit-id plugin adds the commit, which helps confirm what is actually deployed.
Micrometer metric types and naming
Micrometer has a few core meter types. A counter only goes up, like orders created. A gauge reports a current value that goes up and down, like queue size. A timer records how long something took and how often it happened. A distribution summary records sizes, like payload bytes.
Names use dots, for example orders.created, and tags add dimensions such as status or region. Each backend translates names: in Prometheus, http.server.requests becomes http_server_requests_seconds with _count, _sum and _bucket series.
Every unique combination of tag values is a separate time series. Tags with few values (method, status, outcome) are cheap. Tags with unbounded values, like user ids or full URLs, create millions of series and can take down your monitoring. Spring already tags HTTP metrics with the URI template, such as /orders/{id}, for this reason.
- Counter: things that happened
- Gauge: current level
- Timer: duration and count
- Add management.metrics.tags.application to tell apps apart
Scraping with Prometheus and asking useful questions
Add micrometer-registry-prometheus and expose the prometheus endpoint. Prometheus then pulls /actuator/prometheus on each scrape (the global default interval is 1 minute; 15 seconds is common). Each scrape stores the current value of every series with a timestamp.
You query with PromQL. Counters only grow, so you almost always wrap them in rate(), which gives a per-second speed over a time window. Request rate: sum(rate(http_server_requests_seconds_count[5m])). Error ratio: the same with status=~"5.." divided by the total. Latency percentiles need histograms: set management.metrics.distribution.percentiles-histogram.http.server.requests=true, then use histogram_quantile(0.95, ...) over the _bucket series.
A good first dashboard follows the RED method: rate, errors and duration for each endpoint, plus JVM memory and GC pause time.
- Prometheus to Spring app: GET /actuator/prometheus1GET /actuator/prometheus
- Spring app to Prometheus: Text format: name, labels, value2Text format: name, labels, value
- Prometheus to Alertmanager: Rule true for 10m: alert fires3Rule true for 10m: alert fires
- Alertmanager to On-call: Grouped notice to chat or pager4Grouped notice to chat or pager
Learn it in 3 short steps
About 1h each. Track them with a timer in your plan.
- 1Enable Actuator health and info endpoints~1h
- 2Expose metrics with Micrometer~1h
- 3Scrape metrics with Prometheus~1h
Key points
- Only health is exposed over HTTP by default; opt in to other endpoints.
- Liveness and readiness probes tell Kubernetes when to restart or route traffic.
- Micrometer works like SLF4J for metrics: one API, many backends.
- Never expose sensitive endpoints like env or heapdump publicly.
Key terms
Tap a card to flip it. 0/5 known
Examples
# application.yml (needs micrometer-registry-prometheus)
management:
endpoints:
web:
exposure:
include: health,info,prometheus
endpoint:
health:
probes:
enabled: true
# Custom metric in Java:
# meterRegistry.counter("orders.created").increment();Count orders and time the payment call
Inject MeterRegistry, build a counter with a tag for the channel and a timer around the payment call. After a few requests, open /actuator/prometheus and look for orders_created_total and payment_call_seconds_count. The total suffix is added by Prometheus naming for counters.
@Service
class OrderService {
private final MeterRegistry registry;
private final Timer paymentTimer;
OrderService(MeterRegistry registry) {
this.registry = registry;
this.paymentTimer = Timer.builder("payment.call").register(registry);
}
void place(Order o) {
paymentTimer.record(() -> payments.charge(o));
registry.counter("orders.created", "channel", o.channel()).increment();
}
}Prometheus scraping the app on a management port
The app serves traffic on 8080 and Actuator on 8081. Prometheus runs next to it in Compose and scrapes the management port every 15 seconds. Open Prometheus on localhost:9090, check Status then Targets shows the job as UP, and try the query rate(http_server_requests_seconds_count[1m]).
# application.yml
management:
server.port: 8081
endpoints.web.exposure.include: health,info,prometheus
metrics.tags.application: booking-api
---
# prometheus.yml
global: { scrape_interval: 15s }
scrape_configs:
- job_name: booking-api
metrics_path: /actuator/prometheus
static_configs:
- targets: ["api:8081"]Quick reference
- Only /actuator/health is exposed over HTTP by default
- management.endpoints.web.exposure.include=health,info,prometheus opts in to more
- Probes: /actuator/health/liveness and /actuator/health/readiness
- management.server.port=8081 keeps Actuator off the public port
- Prometheus endpoint needs the micrometer-registry-prometheus dependency
- Wrap counters in rate() in PromQL: rate(http_server_requests_seconds_count[5m])
- Never tag metrics with user ids, emails or raw URLs
- show-details: when-authorized hides health internals from anonymous users
Common mistakes
- Exposing all endpoints with include: "*" on a public port.
- Using unbounded values like user ids as metric tags.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Expose health, info and prometheus, then call each endpoint with curl and describe what you see.
- Exercise 2medium
Write a custom HealthIndicator that reports DOWN when a payment provider URL cannot be reached within 2 seconds, and add it to the readiness group only.
- Exercise 3hard
Write a Prometheus alert rule that fires when more than 5% of requests for booking-api return 5xx for 10 minutes.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Is Actuator safe to use in production?
- Yes, if you expose only what you need, hide health details from anonymous users, and keep sensitive endpoints off the public network, for example on a separate management port.
- What is the difference between metrics, logs and traces?
- Metrics are numbers over time, cheap and good for alerts. Logs are individual events with detail. Traces follow one request across services; Spring Boot uses Micrometer Tracing for them.
- Do I need Prometheus to use Micrometer?
- No. Micrometer supports many backends through separate registry dependencies, such as Datadog, CloudWatch or OTLP for OpenTelemetry.
- Why do my Prometheus numbers look like they never go down?
- Counters only increase. Use rate() or increase() in your queries to see how fast they grow over a time window.