Spring Boot Actuator
Spring Actuator is a set of additional product-ready features that help to monitor, manage and interact with the application. These features are effortless to enable and valuable to use. There is an HTTP endpoint as well as a JMX version available and is used primarily to expose operational information about the application — health, metrics, info, dump, env, etc.
Endpoints
To have access to the application’s operational information for monitoring and management purposes, Spring Boot includes a number of built-in endpoints and lets you add your own. For example, the health
endpoint provides basic application health information. You can enable or disable each endpoint and expose them (make them remotely accessible) over HTTP or JMX. The following table shows the default exposure for the build-in endpoints.
Getting Start
The easiest way to use these features is to add the spring-boot-starter-actuator to your project pom.xml file.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Enabling Endpoints
It is highly recommended to disable all endpoints and enable the ones you really need to use and later secure them. By default, only the health endpoint is enabled, and to include or exclude any other endpoints you can use the following pattern:
management.endpoints.jmx.exposure.exclude
: exclude the JMX endpointmanagement.endpoints.jmx.exposure.include
: include JMX endpointmanagement.endpoints.web.exposure.exclude
: exclude HTTP endpointmanagement.endpoints.web.exposure.include
: include HTTP endpoint
In the following example, we are enabling all the endpoints except /env
and /beans
over HTTP.
management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=env,beansmanagement.endpoints.jmx.exposure.include=*
Change the Path
Using the following properties you can change the path to the actuator endpoints. by default the value is /actuator
.
management.endpoints.web.base-path=/maintenance/actuator
Securing Endpoints
If the application exposes to the public you will need to secure these endpoints since they contain sensitive information about your application. Keep in mind that it is highly recommended to not enable any endpoint that you are not using. To secure the endpoints you can start by adding the spring-boot-starter-security
dependency.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
The auto-configuration features will take care of securing the endpoint at a basic level. To add a username and password you can begin with setting the following properties in your application.properties
file.
spring.security.user.name=admin
spring.security.user.password=admin
In case you need a more flexible security configuration you need to consider extending WebSecurityConfigurerAdapter
or providing SecurityFilterChain
bean for relevant endpoints.
Commonly used Endpoints
The above list is some of the most used Spring Boot Actuator endpoints:
- /health — This endpoint will show details of the health of the application. By default, the only available value is the overall status of the application. but using some extra configuration you can enable more details about the application and the host. you just need to add the following property to your
application.properties
file.
management.endpoint.health.show-details=always
having health.show-details
enabled using always property you will get information about the host storage and also related auto-configured HealthIndicators when Spring Boot provides them like dB, JMS, Redis, and…(complete list).
- /metrics — Another useful endpoint is the metrics endpoint which provides a lot of useful information about the application. By calling this endpoint you will get a list of keys that you can use to get more details in the same URL path.
/actuator/metrics/{key}
For example to get /actuator/metrics/application.started.time
you will get the application start time.
- /heapdump — Using this endpoint you will be able to download a heap dump snapshot of your running application JVM. This file can be later opened and analyzed using Java VisualVM, IBM HeapAnalyzer, or Eclipse Memory Analyze.
- /threaddump —This endpoint will dump the thread information of the underlying JVM. This information will be returned in JSON format and will include the
threadName
,threadId
,threadStatus
, and … in any given time.
Each of these endpoints can be helpful in monitoring and managing your application in the production environment. I will be covering the most useful actuator in separate articles in near future.
Hope this article helped you and please support me with your applauding 👏 for the story. if you don’t know how it is just like this:
Or buy me a coffee here!