Spring Boot Actuator Spring Boot 2 Recipes A Problem-Solution Approach
Spring Boot Actuator Spring Boot 2 Recipes A Problem-Solution Approach
Marten Deinum1
(1) Meppel, Drenthe, The Netherlands
When developing an application, you also want to be able to monitor the
behavior of the application. Spring Boot makes it very easy to enable that
by introducing Spring Boot Actuator. Spring Boot actuator exposes health
and metrics from the application to interested parties. This can be over
JMX, HTTP, or exported to an external system.
The health endpoints tell something about the health of your application
and/or the system it is running on. It will detect if the database is up, re-
port the diskspace, etc. The metrics endpoints expose usage and perfor-
mance statistics like number of request, the longest request, the fastest,
the utilization of your connection pool, etc.
All these metrics can be viewed either through JMX or HTTP when en-
abled, but can also be automatically exported to an external system like
Graphite, InfluxDB, and many others.
Problem
You want to enable health and metrics in your application so that you can
monitor the status of the application.
Solution
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Now when starting the application, Spring Boot will have the actuator
con�igured and it is accessible through JMX (Figure 10-1, and see Recipe 8.4 on
how to use JConsole) and through the web (default under the /actuator
path)(see Figure 10-2).
Figure 10-1 JMX exposed metrics
You will notice that JMX exposes a lot more endpoints then HTTP does. HTTP
only exposes /actuator/health and /actuator/info. JMX exposed a lot
more. This is done with security in mind: the /actuator is exposed publicly and
as such you don’t want everyone to see things. What to expose can be con�igured
through the management.endpoints.web.exposure.include and
management.endpoints.web.exposure.exclude properties. Using a * for
the include will expose all endpoints to the web.
management.endpoints.web.exposure.include=*
Add an X-Application-
management.server.add- Context header to the response
application-context-header containing the application con-
text name
man-
The context path of the manage-
age-
ment server, default none re-
ment.server.servlet.context-
sulting in /
path
management.server.add-application-context-header=true
management.server.port=8090
Property Description
management.endpoint.
<endpoint- Time to cache a response, default is
name>.cache.time-to- 0ms meaning no caching
live
man-
Whether to show details for the health
age-
endpoint, default is never, can be
ment.end-
changed to always or when-
point.health.show-
authorized
details
When Spring Boot detects both Spring Boot Actuator and Spring Security,
it will enable secure access to management endpoints automatically.
When accessing the endpoint, a Basic Login prompt will be shown and
asks for a username and password. Spring Boot will generate a default
user, with the username user and a generated password (see Recipe 6.1)
to use for login.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
This will enable security in your application and for the management
endpoints. Now when accessing the endpoint http://localhost:8090
/actuator a Basic login prompt will be shown. After entering the correct
credentials you should still be able to see the results.
Configure Healthchecks
management.health.diskspace.enabled=false
This will disable the healthcheck for the diskspace, and it won’t be part of
the health checks anymore.
Configure Metrics
One of the features of Spring Boot Actuator is to expose metrics. Those are
exposed under http://localhost:8090/actuator/metrics; this will
produce a list of available metrics for your application (Figure 10-5).
Figure 10-5 List of currently available metrics
management.metrics.enable.system=false
management.metrics.enable.tomcat=false
Problem
Your application needs to expose certain metrics and have a health check
that aren’t available by default.
Solution
The health checks and metrics are pluggable, and beans of type
HealthIndicator and MetricBinder are automatically registered to pro-
vide additional health checks and/or metrics. The task is to create a class
implementing the desired interface and register an instance of that class
as a bean in the context of having it contribute to the health and metrics.
How It Works
First, let’s write the HealthIndicator . You can either directly implement the
HealthIndicator interface or use the convenience
AbstractHealthIndicator as a base class.
package com.apress.springbootrecipes.library.actuator;
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Component;
@Component
class TaskSchedulerHealthIndicator extends AbstractHealthIndicator {
private final ThreadPoolTaskScheduler taskScheduler;
TaskSchedulerHealthIndicator(ThreadPoolTaskScheduler taskScheduler) {
this.taskScheduler = taskScheduler;
}
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
int poolSize = taskScheduler.getPoolSize();
int active = taskScheduler.getActiveCount();
int free = poolSize - active;
builder
.withDetail("active", taskScheduler.getActiveCount())
.withDetail("poolsize", taskScheduler.getPoolSize());
if (poolSize > 0 && free <= 1) {
builder.down();
} else {
builder.up();
}
}
}
Problem
Solution
Use one of the supported systems like Graphite and periodically push the
metrics to that system. Include a micrometer.io registry dependency in
your application (next to the spring-boot-starter-actuator depen-
dency) and metrics will automatically be exported. By default, every
minute the data will be pushed to the server.
How It Works
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-graphite</artifactId>
</dependency>
Property Description
man-
How often to send metrics, default 1
agement.metrics.export.
minute
<registry-name>.step
man-
agement.metrics.export. Base time unit used to report rates,
<registry-name>.rate- default seconds
units
man-
Base time unit used to report dura-
agement.metrics.export.
tions, default milliseconds
<registry-
Property Description
name>.duration-units
To report the metrics every ten seconds instead of each minute, add the
following to the application.properties:
management.metrics.export.graphite.step=10s
Footnotes
1 https://micrometer.io