Sub topic 2
Health Groups, Metrics, and Prometheus
Assume you know basic Spring Boot configuration, Actuator dependencies, and HTTP endpoints. Actuator health groups let you expose different views of application health. This matters when Kubernetes needs separate startup decisions from dependency checks.
Health groups and Kubernetes probes
Enable probe groups and define which indicators each group includes:
management:
endpoint:
health:
probes:
enabled: true
group:
liveness:
include: livenessState
readiness:
include: readinessState,dbThe liveness endpoint answers whether the application process should be restarted. It should contain only checks about the application itself. Do not include a database or remote API in liveness. A temporary dependency failure could otherwise cause restart loops.
The readiness endpoint answers whether the application should receive traffic. It can include dependencies such as a database or payment service.
Publishing application and build information
The info endpoint publishes selected metadata. Build plugins can generate build-info.properties, and a Git plugin can generate git.properties. Expose it with:
management:
endpoints:
web:
exposure:
include: health,infoTreat build and Git data as operational metadata. Avoid putting secrets, tokens, or private configuration values into the response.
Micrometer and custom metrics
Micrometer provides a vendor-neutral API for metrics. Actuator exposes registered meters through /actuator/metrics. Add the Actuator starter and a registry such as Prometheus:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<scope>runtime</scope>
</dependency>Use counters for totals, timers for duration, and gauges for a value that can rise or fall:
@Service
public class OrderMetrics {
private final Counter placed;
private final Timer processing;
private final AtomicInteger inFlight = new AtomicInteger();
public OrderMetrics(MeterRegistry registry) {
placed = registry.counter("orders.placed");
processing = registry.timer("orders.processing");
Gauge.builder("orders.in.flight", inFlight, AtomicInteger::get)
.register(registry);
}
public void record(Runnable work) {
inFlight.incrementAndGet();
try {
processing.record(work);
placed.increment();
} finally {
inFlight.decrementAndGet();
}
}
}Exporting metrics to Prometheus
Expose the scrape endpoint:
management:
endpoints:
web:
exposure:
include: health,info,metrics,prometheusPrometheus periodically sends an HTTP request to /actuator/prometheus and stores the returned time series. Dashboards visualize those series, while alerts detect sustained conditions such as high latency or repeated readiness failures.
Exercise
Which probe should include a database check: liveness or readiness?
Show answer
Readiness. A database outage should stop traffic, not automatically restart a healthy application process.