The Spring Boot Actuator module provides advanced production-ready features such as monitoring, metrics, health checks, etc. The Spring Boot Actuator enables you to monitor the application using HTTP endpoints and JMX. In this tutorial we will learn how to get started with it.
Configuring the Actuator in an application
In order to kick-start the Spring Boot Actuator it is sufficient to include the spring-boot-starter-actuator to autoconfigure Actuator. You can then take advantage of Actuator’s features in order to monitor a Spring Boot application:
$ spring init -dweb,actuator actuator-demo
Here is the list of dependencies from the pom.xml:
<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>
Now if you run the entry point class, you can check the metrics available:
$ mvn spring-boot:run
You should see from the Console logs the following entry:
2022-01-04 12:59:42.798 INFO 18424 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 1 endpoint(s) beneath base path '/actuator'
Default Endpoints available
Unlike in Spring Boot 1.x, Spring Boot 2 Actuator comes with most endpoints disabled, except for the health endpoint:
$ curl http://localhost:8080/actuator/health {"status":"UP"}
To check the list of available actuator endpoints, just point to the /actuator Root Context:
curl -s http://localhost:8080/actuator | jq { "_links": { "self": { "href": "http://localhost:8080/actuator", "templated": false }, "health": { "href": "http://localhost:8080/actuator/health", "templated": false }, "health-path": { "href": "http://localhost:8080/actuator/health/{*path}", "templated": true } } }
How to enable additional Actuator endpoints
In order to enable all the available Actuator endpoints you have to set the following property into the application.properties file:
management.endpoints.web.exposure.include=*
As you can see from the Application Logs, now 15 endpoints are available:
INFO 11550 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 15 endpoint(s) beneath base path '/actuator'
You can also explicitly enable/disable a specific endpoint (for example /beans), as follows:
management.endpoint.beans.enabled=true
To expose all enabled endpoints except one (for example /env), we use:
management.endpoints.web.exposure.include=* management.endpoints.web.exposure.exclude=env
Configurable Endpoints
The following Actuator endpoints are autoconfigured by the Actuator starter with the default settings:
-
- info: Displays arbitrary application info.
- health: Shows application basic health info for unauthenticated users and full details for authenticated users.
- beans: Shows a list of all Spring beans configured in the application.
- autoconfig: Displays an autoconfiguration report showing all autoconfiguration candidates and the reason they were/were not applied.
- mappings: Displays a collated list of all @RequestMapping paths.
- configprops: Displays a collated list of all @ConfigurationProperties.
- metrics: Shows metrics information for the current application.
- env: Exposes properties from Spring’s ConfigurableEnvironment.
- trace: Displays trace information (by default, the last 100 HTTP requests).
-
- dump: Performs a thread dump.
- loggers: Shows and modifies the configuration of loggers in the application.
- auditevents: Exposes audit events information for the current application.
- flyway: Shows any Flyway database migrations that have been applied.
- liquibase: Shows any Liquibase database migrations that have been applied.
- actuator: Provides a hypermedia-based “discovery page” for the other endpoints.
- shutdown: Allows the application to be gracefully shut down (not enabled by default).
How to collect application metrics
You can collect metrics from the /metrics endpoint. When you request this endpoint, you will see the list of default metrics which you can query:
curl -s http://localhost:8080/actuator/metrics | jq { "names": [ "application.ready.time", "application.started.time", "disk.free", "disk.total", "executor.active", "executor.completed", "executor.pool.core", "executor.pool.max", "executor.pool.size", "executor.queue.remaining", "executor.queued", "http.server.requests", "jvm.buffer.count", "jvm.buffer.memory.used", "jvm.buffer.total.capacity", . . . . . . . .
Then, you can query about detail for a specific metric. For example, if you want to check application metrics for http.server.request, then just add it at the end of the path:
http://localhost:8080/actuator/metrics/http.server.requests
Here are the metrics for the HTTP Endpoints:
From the output, you can see some basic statistics such as:
- COUNT of HTTP requests
- TOTAL_TIME spent in requests
- MAX longest HTTP Request
How to customize the output of the actuator
You can also customize the data shown by the metrics. For example, if we want to customize the “/info”, we can modify the “info.app” properties in application.properties:
info.app.name=Demo Spring Sample Application info.app.description=This is an example of Spring Boot Actuator info.app.version=1.0.0
This results in:
curl http://localhost:8080/actuator/info {"build":{"name":"Demo Spring Sample Application","description":"This is an example of Spring Boot Actuator","version":"1.0.0"}}Found the article helpful? if so please follow us on Socials