Spring Boot Actuator Example
Spring Boot Actuator Tutorial :
Spring Boot Actuator module assists you in monitoring and managing your Spring Boot application through offering production-ready features such as health check-up, auditing, metrics gathering, HTTP tracing, and so on. All of these features are available via JMX or HTTP endpoints.
Actuator also combines with third-party application monitoring systems such as Prometheus, Graphite, DataDog, Influx, Wavefront, and New Relic, among others. These systems offer better dashboards, graphs, analytics, and alarms to assist you in monitoring and managing your application from a single unified interface.
- This first section of the article will demonstrate how to configure Actuator in a Spring Boot application and access its features through the use of HTTP endpoints.
- The second section will demonstrate how to integrate Actuator with an external application monitoring system.
Take a look at our suggested posts:
Let's create a Spring Boot Application and follow below steps to integrate Actuator.
Create Spring Boot Project from Spring Initializr.Project Structure
Maven Dependency
Add spring-boot-actuator
dependency in pom.xml.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.techgeeknext</groupId>
<artifactId>spring-boot-actuator-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-boot-actuator-example</name>
<description>Spring Boot Actuator Example Project</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Properties
Define below properties in application.properties file, will cover each property in detail.
# INFO ENDPOINT CONFIGURATION
#http://localhost:8080/actuator/info
info.app.name=@project.name@
info.app.description=@project.description@
info.app.version=@project.version@
info.app.encoding=@project.build.sourceEncoding@
info.app.java.version=@java.version@
# Logging
#http://localhost:8080/actuator/logfile
logging.file=app.log
# SHUTDOWN ENDPOINT (ShutdownEndpoint)
management.endpoint.shutdown.enabled=true
# HEALTH ENDPOINT
# enable a detailed view
#http://localhost:8080/actuator/health
management.endpoint.health.show-details=always
# ENDPOINTS WEB CONFIGURATION
# Use "*" to expose all endpoints
management.endpoints.web.exposure.include=*
#comma-separated list to expose selected endpoints
# management.endpoints.web.exposure.include=health,info
Monitoring application via Actuator Endpoints
By default, only the /health
and /info
endpoints are accessible via HTTP. As a result, the /actuator
page only lists the health and information endpoints.
Let's start the application using below command or from IDE.
mvn spring-boot:run
Now, you can perform below steps to understand and test the actuator endpoints.
- Once the application has started, the URL http://localhost:8080/actuator will display a list of all the actuator endpoints exposed over HTTP.
- Let's look into the health endpoint by visiting http://localhost:8080/actuator/health. The following information should be displayed at the endpoint:
-
It should be observed that each actuator endpoint can be explicitly enabled and disabled.
You could enable or disable an actuator endpoint by adding below setting in application.properties file.
Exposing Actuator endpoints via HTTP
# Use "*" to expose all endpoints, or a comma-separated list to expose selected ones management.endpoints.web.exposure.include=health,info management.endpoints.web.exposure.exclude= *
Exposing Actuator endpoints via JMX
# Use "*" to expose all endpoints, or a comma-separated list to expose selected ones management.endpoints.jmx.exposure.include=* management.endpoints.jmx.exposure.exclude=*
Allow all actuator endpoints to be exposed by setting the property
management.endpoints.web.exposure.include
to*
and inspecting the output of thehttp://localhost:8080/actuator
page. You'll observe that the actuator page now displays a list of all the available endpoints.common actuator endpoints
/health endpoint
The health endpoint examines your application's health by integrating several health indicators. Spring Boot Actuator includes a number of predefined health indicators, such as DataSourceHealthIndicator, DiskSpaceHealthIndicator, MongoHealthIndicator, RedisHealthIndicator, CassandraHealthIndicator, and others. Such health indicators are used as part of the health check-up process.
By default it'll be enable, you can disable a particular health indicator from application properties.management.health.mongo.enabled=false ## detailed health information management.endpoint.health.show-details=always
The health endpoint now provides information about the DiskSpaceHealthIndicator, which would be execute as part of the health checkup process.
If your application does have a database (for example, MySQL), the health endpoint would also give a result of that database.
As shown in the highlighted data above, it was generated by adding the Custom Health Indicator code to the health endpoint.
package com.techgeeknext.actuator.health;
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.stereotype.Component;
@Component
public class CustomHealthIndicator extends AbstractHealthIndicator {
/*
Method to add custom health checks/code
http://localhost:8080/actuator/health
*/
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
// You can use the builder to display status details
builder.up()
.withDetail("Service", "Running")
.withDetail("Error", "No Error- Healthy status");
//If you add below line, it will show on the health page - "error":"java.lang.RuntimeException:
//.withException(new RuntimeException());
}
}
As long as the application is in good health, the status will be UP. It will display DOWN if the application becomes unhealthy due to a problem such as a loss of database connectivity or a lack of disc space, among other things.
Here's a list of some extremely useful actuator endpoints. The complete list is available in the official documentation.
Endpoint ID |
Description |
auditevents |
Exposes audit events (e.g. auth_success, order_failed) for your application |
info |
Displays information about your application. |
health |
Displays your application's health status. |
metrics |
Shows various metrics information of your application. |
loggers |
Displays and modifies the configured loggers. |
logfile |
Returns the contents of the log file (if |
httptrace |
Displays HTTP trace info for the last 100 HTTP request/response. |
env |
Displays current environment properties. |
flyway |
Shows details of Flyway database migrations. |
liquidbase |
Shows details of Liquibase database migrations. |
shutdown |
Lets you shut down the application gracefully. |
mappings |
Displays a list of all @RequestMapping paths. |
scheduledtasks |
Displays the scheduled tasks in your application. |
threaddump |
Performs a thread dump. |
heapdump |
Returns a GZip compressed JVM heap dump. |
Enabling and Disabling Actuator Endpoints
Download Source Code
The full source code for this article can be found on below.Download it here - Spring Boot Actuator Example