Actuators, Metrics and Health
Estimated time to read: 2 minutes
Actuators¶
Spring Boot Actuator provides the following via various exposed HTTP or JMX endpoints:
- Health Checks
- Auditing
- Metrics Gathering
- HTTP tracing
Dependencies¶
To include Spring Boot Actuator in a project, the following dependencies are required:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Endpoints¶
spring-boot-starter-actuator provides a list of endpoints that assist with monitoring and managing the application. These endpoints can be found at localhost:8080/actuator.
/actuator/healthreturns information about the applications health/actuator/inforeturns general information about the application obtained from build files/actuator/loggersreturns application logs and the ability to update the log level at runtime/actuator/metricsreturns metric information such as system information, JVM information, handlers, etc/actuator/mappingsreturns mapping information
Paths can also be followed to specific items which give more details around the required item. For example:
/actuator/metrics/system.cpu.usage
Note
By default a number of endpoints are disabled as they may contain sensitive information.
Note
/actuator/health reports the overall health of the application and any reliant services. If a custom metric is in use and the service it tests is DOWN, then the health of the entire application will also respond with DOWN.
Configuration¶
Endpoints can be configured within the application.properties file.
// ...
management.endpoints.web.exposure.include=info,health,metrics,loggers,beans,mappings
management.endpoint.health.show-details=always
// ...
Custom Endpoints¶
Spring Boot provides a selection of ready to use health indicators; however, custom indicators can be added as required.
For example, assuming that our API is reliant on another API on another system a custom indicator can be used and a custom endpoint generated.
To create a custom metric, a class can be created implementing the HealthIndicator interface and override the Health method.
// ...
@Component
public class ExternalAPIIndicator implements HealthIndicator {
private final String message_key = "ExternalService";
@Override
public Health health() {
if(!isRunningServiceExternalAPIService()) {
return Health.down().withDetail(message_key, "Not Available").build();
}
return Health.up().withDetail(message_key, "Available").build();
}
private Boolean isRunningServiceExternalAPIService() {
Boolean isRunning = false;
// Add real logic here to test if the ExternalAPI Service is running
return isRunning;
}
}
//...