Actuator and observability

Mid-level Frequently asked 4 min

On this page

The question

What does Spring Boot Actuator give you for health, metrics and readiness?

30-second answer

Production endpoints under /actuator. /health reports UP or DOWN for load balancers. Its liveness and readiness groups tell Kubernetes when to restart a pod and when to send it traffic. /metrics and /prometheus expose Micrometer metrics, such as request timings and JVM (Java Virtual Machine) memory. Only health is exposed over HTTP by default, because the others can leak information.

Understand it

Actuator endpoints and who uses them

Tap the parts

Select a part to see what it does.

Tap an endpoint to see what it does. Each row shows who calls it, and whether HTTP exposes it with no settings.

View as text

Seven rows, one per Actuator endpoint under /actuator. Each has a button with the path, who uses it, and whether it is exposed by default.

/health: used by Load balancer. Exposed by default. Answers UP with 200, or DOWN with 503, combining checks such as the database and disk space.

/health/liveness: used by Kubernetes: restart?. Not exposed by default. Is the process healthy, or should it be restarted? When broken it answers 503, and Kubernetes restarts the pod.

/health/readiness: used by Kubernetes: traffic?. Not exposed by default. Can it take traffic now? It answers 503 while refusing, and Kubernetes stops sending requests but leaves the pod running.

/prometheus: used by Prometheus scrapes. Not exposed by default. Every metric in the text format Prometheus reads.

/metrics: used by Developer, by hand. Not exposed by default. Lists metric names, and /metrics/http.server.requests shows the values and tags of one.

/loggers: used by On-call engineer. Not exposed by default. Reads and changes log levels at run time.

/env: used by Keep this private. Not exposed by default. Shows configuration and which source set each value.

Seven rows, one per Actuator endpoint under /actuator. Each has a button with the path, who uses it, and whether it is exposed by default.

/health: used by Load balancer. Exposed by default. Answers UP with 200, or DOWN with 503, combining checks such as the database and disk space.

/health/liveness: used by Kubernetes: restart?. Not exposed by default. Is the process healthy, or should it be restarted? When broken it answers 503, and Kubernetes restarts the pod.

/health/readiness: used by Kubernetes: traffic?. Not exposed by default. Can it take traffic now? It answers 503 while refusing, and Kubernetes stops sending requests but leaves the pod running.

/prometheus: used by Prometheus scrapes. Not exposed by default. Every metric in the text format Prometheus reads.

/metrics: used by Developer, by hand. Not exposed by default. Lists metric names, and /metrics/http.server.requests shows the values and tags of one.

/loggers: used by On-call engineer. Not exposed by default. Reads and changes log levels at run time.

/env: used by Keep this private. Not exposed by default. Shows configuration and which source set each value.

Takeaway: Each endpoint has a different audience: infrastructure polls health, Prometheus scrapes metrics, and people debug with the rest. Only health is exposed until you choose otherwise.

Liveness and readiness trigger different actionsWhen the liveness probe fails, Kubernetes restarts the pod. When the readiness probe fails, Kubernetes stops sending traffic to it and leaves it running, and traffic returns once it passes again.Liveness failsKubernetes restartsthe podReadiness failsKubernetes stopstraffic,pod keeps runningPasses again: trafficreturns
Liveness and readiness trigger different actionsWhen the liveness probe fails, Kubernetes restarts the pod. When the readiness probe fails, Kubernetes stops sending traffic to it and leaves it running, and traffic returns once it passes again.Liveness failsKubernetes restartsthe podReadiness failsKubernetes stopstraffic,pod keeps runningPasses again: trafficreturns
Liveness and readiness trigger different actions

In code

@Service
class OrderService {
    private final Counter placed;
    private final Timer payment;

    OrderService(MeterRegistry registry) {
        this.placed = Counter.builder("orders.placed").tag("channel", "web").register(registry);
        this.payment = registry.timer("orders.payment");
    }

    void place(Order order) {
        payment.record(() -> charge(order));
        placed.increment();
    }

    private void charge(Order order) {
    }
}
A custom metric with Micrometer
@Component
class PaymentHealth implements HealthIndicator {
    private final PaymentClient client;

    PaymentHealth(PaymentClient client) {
        this.client = client;
    }

    @Override
    public Health health() {
        return client.ping() ? Health.up().build() : Health.down().withDetail("reason", "no ping").build();
    }
}
A custom health check
From a counter in code to a chartApplication code increments a Micrometer counter. The Prometheus registry exposes it at the actuator prometheus endpoint. Prometheus scrapes that endpoint on a schedule, and Grafana draws the chart.placed.increment()Micrometer registry/actuator/prometheusPrometheus scrapesGrafana chart
From a counter in code to a chartApplication code increments a Micrometer counter. The Prometheus registry exposes it at the actuator prometheus endpoint. Prometheus scrapes that endpoint on a schedule, and Grafana draws the chart.placed.increment()Micrometer registry/actuator/prometheusPrometheus scrapesGrafana chart
From a counter in code to a chart
management:
  server:
    port: 8081
  endpoints:
    web:
      exposure:
        include: health, info, prometheus
Expose a few endpoints, on a port the internet cannot reach

Solve it

Kubernetes restarts every pod during a database outage

The database is down for five minutes. Instead of waiting, Kubernetes restarts all pods again and again, and the service takes ten more minutes to recover after the database is back. The liveness probe calls /actuator/health.

  1. Read the events. kubectl describe pod shows “Liveness probe failed” and restarts. /actuator/health turned DOWN because the db indicator is part of it.
  2. Separate the two questions. Liveness asks “is this process broken beyond repair?” Readiness asks “should it get traffic now?” A restart does not fix a database.
  3. Point each probe at its group. Use /actuator/health/liveness for liveness, which reports only the app’s own state, and /actuator/health/readiness for readiness.
  4. Put dependencies in readiness only. Add db to the readiness group, so an outage removes pods from the load balancer without restarting them.
  5. Check. Stop the database in staging: pods turn not ready, nothing restarts, and they serve again once it returns.
management:
  endpoint:
    health:
      probes:
        enabled: true
      group:
        readiness:
          include: readinessState, db
application.yml: the database counts for readiness, not liveness
livenessProbe:
  httpGet: { path: /actuator/health/liveness, port: 8081 }
  periodSeconds: 10
readinessProbe:
  httpGet: { path: /actuator/health/readiness, port: 8081 }
  periodSeconds: 5
The Kubernetes probes that use the two groups

How to answer

  • Add more with management.endpoints.web.exposure.include.
  • Failing health returns 503, which is what infrastructure reacts to.
  • The starter is spring-boot-starter-actuator.
  • Put management endpoints on a separate port to keep them private.

Common mistakes

  • Exposing every endpoint publicly with include=* and no security.
  • Putting a database check in the liveness probe.

Follow-up questions

What is Micrometer, and how do you add a custom metric?

Micrometer is a facade for metrics, as SLF4J (Simple Logging Facade for Java) is for logs. You record through a MeterRegistry, and the dependency you add decides the backend: Prometheus, Datadog and others. Boot auto-configures the registry and records HTTP timings as http.server.requests. Inject MeterRegistry to create a Counter for how many, a Timer for how long, or a Gauge for a current value.

  • Tags add dimensions, such as status or uri.
  • Avoid tags with unbounded values, such as a user id, because each value makes a new time series.
  • Prometheus names use underscores and a suffix, such as orders_placed_total.
Counter placed = Counter.builder("orders.placed")
        .tag("channel", "web")
        .register(registry);
placed.increment();

See it in the infographic: Actuator endpoints and who uses them

What is the difference between the liveness and readiness probes?

Liveness asks whether the process is healthy; on failure, Kubernetes restarts the pod. Readiness asks whether it can take traffic now; on failure, Kubernetes stops routing to it, with no restart. An application can refuse traffic while warming up or during shutdown, while staying alive: readiness answers 503 while liveness stays 200.

  • Publish AvailabilityChangeEvent to change the state from code.
  • Boot 4.1 exposes both by default; Boot 3.5 needs Kubernetes or management.endpoint.health.probes.enabled.
  • A startup probe gives a slow application time before liveness starts counting.

See it in the infographic: Actuator endpoints and who uses them

How do you expose Actuator safely in production?

Expose only what is needed, with an explicit include list. Put management on a separate port with management.server.port, so it is not reachable through the public ingress, and require an admin role for anything beyond health. /env, /heapdump and /loggers are sensitive: a heap dump can contain passwords and tokens from memory, and /loggers changes behavior.

  • Values in /env are masked by default, since Boot 3.
  • Never use include=* on a public port.
  • Network rules and authentication add up; rely on both.

See it in the infographic: Actuator endpoints and who uses them

Why should the liveness probe not include the database?

A failed liveness probe restarts the pod. If the database goes down, every pod fails together, and Kubernetes restarts them all, which cannot fix the database and adds load when it returns. Boot keeps liveness free of external checks by default. Readiness may include them, since it only stops traffic.

How do you see which checks make up /health?

Set management.endpoint.health.show-details to when-authorized or always. The response then lists components, such as db and diskSpace, each with its status. Any HealthIndicator bean adds a component. The overall status is the worst of them, so one optional dependency can take the application down, unless you group the checks.

Check yourself

1. You add spring-boot-starter-actuator and change no settings. Which endpoint answers over HTTP?
Show answers
  • /actuator/health: Correct. Only health is exposed by default. /info has not been exposed since Boot 2.5.
  • /actuator/metrics: Incorrect. It exists, but returns 404 until you add it to the exposure list.
  • /actuator/env: Incorrect. It is sensitive, so it stays hidden until you expose it.
2. The readiness probe of a pod starts failing. What does Kubernetes do?
Show answers
  • It stops sending traffic to the pod and leaves it running.: Correct. Readiness controls traffic only. The pod can recover and receive traffic again.
  • It restarts the pod.: Incorrect. That is the reaction to a failing liveness probe.
  • Nothing, until a person acts.: Incorrect. Reacting to probes is automatic. That is why they exist.