Sub topic 1
What Actuator provides and how to add it
Spring Boot Actuator adds production-ready operational features to a Spring Boot application. It exposes information about a running application, such as its health, metrics, configuration, loggers, and HTTP mappings. This chapter explains what Actuator provides, how to add it to a build, and what you see once it runs.
Prerequisites
This chapter assumes you have:
- A Spring Boot application that starts and serves requests, and some familiarity with auto-configuration.
- Experience with Maven or Gradle dependency management.
- Basic knowledge of
application.propertiesorapplication.yml. - Basic familiarity with HTTP and JSON.
Spring Security knowledge is helpful for the later chapter on securing endpoints, but you don't need it here.
What Actuator provides
An application in production raises questions that its business code doesn't answer. Is it healthy? How many requests per second does it handle? Which log level is active? Which configuration properties were applied? Actuator answers these through endpoints. An endpoint is a named operation that returns data about the application, and in some cases changes its behavior.
Actuator groups its features into three areas:
- Health. Reports whether the application and its dependencies, such as a database or message broker, can do their work.
- Metrics. Collects numeric measurements, such as JVM memory use, HTTP request timings, and connection pool sizes. Actuator uses Micrometer, a metrics facade, to record and export them.
- Diagnostics. Shows the configuration, beans, request mappings, loggers, and threads of a running instance.
Common built-in endpoints:
| Endpoint ID | Purpose |
|---|---|
| `health` | Application health status, optionally with component details |
| `info` | Application, build, and git information |
| `metrics` | Names and values of the metrics Micrometer has recorded |
| `prometheus` | Metrics in Prometheus text format (needs an extra dependency) |
| `loggers` | Shows and changes logger levels at runtime |
| `env` | Properties from the Spring `Environment` |
| `beans` | All Spring beans in the context |
| `mappings` | All request mappings |
| `threaddump` | A thread dump of the JVM |
Each endpoint has an ID. The ID is the name you use in configuration and, for web access, the last part of the URL: /actuator/health, /actuator/metrics, and so on.
Adding Actuator to a project
Actuator is a separate starter. Add spring-boot-starter-actuator to your build. This is the only dependency needed for the core endpoints.
If you plan to expose metrics for Prometheus, add the Micrometer Prometheus registry as well. This registry adds the /actuator/prometheus endpoint, which a later chapter covers in detail.
<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>With Gradle, the equivalent dependencies are:
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
runtimeOnly 'io.micrometer:micrometer-registry-prometheus'
}You don't need a version number when your project uses the Spring Boot parent POM or the Spring Boot dependency management plugin. They manage the versions of both dependencies.
What happens at startup
Actuator works through Spring Boot auto-configuration. When the starter is on the classpath, auto-configuration classes register the endpoint beans, a MeterRegistry, health indicators for the technologies it detects, and the infrastructure that publishes endpoints over HTTP.
Health indicators are conditional. If a DataSource bean exists, a database indicator is registered. If Redis is configured, a Redis indicator appears. You get useful health checks without writing code.
The diagram shows how a request reaches an endpoint over HTTP.
Checking that it works
Start the application and request the health endpoint. The default port is 8080 unless you changed it.
curl http://localhost:8080/actuator/healthThe default response is:
{"status":"UP"}UP means the application is healthy. Other statuses are DOWN, OUT_OF_SERVICE, and UNKNOWN.
The root path /actuator returns a discovery page. It lists the links to every endpoint that is currently exposed over HTTP:
curl http://localhost:8080/actuatorTreat this page as a check of your exposure settings. Without any configuration, it lists only self and health.
Enabled versus exposed
Newcomers often get confused here. An endpoint has two independent states:
- Enabled. The endpoint bean exists in the application context. Most endpoints are enabled by default. The
shutdownendpoint is the exception, and it stays disabled until you turn it on. - Exposed. The endpoint is reachable through a technology, such as HTTP or JMX.
An endpoint is usable only when it is both enabled and exposed. Spring Boot's defaults are cautious. Over HTTP, only health is exposed. Over JMX, a wider set is exposed. If you request /actuator/metrics without changing anything, you get a 404, not a 403. The endpoint exists, but the web layer hasn't published it.
To expose more endpoints over HTTP, list them in management.endpoints.web.exposure.include:
management:
endpoints:
web:
exposure:
include: health,info,metrics,prometheus,loggersThe next chapter covers exposure rules in detail, including wildcards and exclude. For now, remember that each exposed endpoint adds to your application's attack surface. Expose only the endpoints you need.
Common errors
These are the problems people hit most often when first adding Actuator.
- A 404 on an endpoint you expect. The endpoint isn't exposed over HTTP. Check
management.endpoints.web.exposure.includeand look at the/actuatordiscovery page. - A 404 on
/actuatoritself. Either the starter isn't on the classpath, or the application serves Actuator on a different port or base path (management.server.portormanagement.endpoints.web.base-path). A later chapter covers both settings. /actuator/prometheusis missing. The endpoint comes from themicrometer-registry-prometheusdependency. Without it, the endpoint doesn't exist, even if you listprometheusin the exposure setting.- A 401 or 403 after you add Spring Security. Spring Security protects all HTTP endpoints by default, including Actuator ones, unless you configure otherwise. See the chapter on securing endpoints.
- Exposing everything with
*and leaving it. Usinginclude: "*"is convenient on a laptop. In production, it can leak environment properties, bean definitions, and heap dumps. Quote the asterisk in YAML, because an unquoted*is a YAML alias character and is invalid.
How the chapters fit together
This chapter covers the foundation. The next chapters build on it in this order:
- Exposure and enabling endpoints, then health and its details.
- Custom health indicators and Kubernetes probes.
- Micrometer metrics, custom metrics, and Prometheus export.
- Security and a separate management port.
- Diagnostic endpoints and custom endpoints.
- Tracing, dashboards, and alerting.
Nearly every later topic depends on the idea from this chapter: an endpoint must be both enabled and exposed, and you decide the exposure.
Exercise
You add spring-boot-starter-actuator to a project and start the application with no extra configuration. A request to /actuator/metrics returns 404, while /actuator/health returns {"status":"UP"}. What explains this, and what is the smallest configuration change that makes /actuator/metrics respond?
Show answer
The metrics endpoint is enabled but not exposed over HTTP. By default, only health is exposed over the web. Add management.endpoints.web.exposure.include=health,metrics (or the YAML equivalent). Keeping health in the list matters, because setting include replaces the default list.
Exercise
What are the two states an endpoint must be in before a client can use it, and which Spring Boot dependency do you add to get a /actuator/prometheus endpoint?
Show answer
The endpoint must be enabled (its bean exists) and exposed (published over a technology such as HTTP or JMX). The Prometheus endpoint comes from the io.micrometer:micrometer-registry-prometheus dependency, added alongside spring-boot-starter-actuator. Also include prometheus in the exposure list.