Actuator provides monitoring and metrics features that are ready for production use in Spring Boot applications. These features are accessible through various endpoints, which can be accessed both via HTTP and JMX MBeans.
Enabling Actuator in a Spring Boot application allows us to gain visibility into the application: Among other things, statistics about the application, such as the number of times a particular endpoint has been requested, the amount of memory the application is consuming, the logging levels of different packages within the application, and the available configuration properties in the application environment. Additionally, Actuator provides information about the application’s health and any external services it is coordinating with.
Enabling Actuator in Spring Boot projects
To enable Actuator in a Spring Boot project, all you need to do is add the following dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Actuator adds multiple endpoints to your application, and you can obtain a list of these endpoints by accessing the following URL:
http://localhost:8080/actuator
Note: By default, only one endpoint, the health endpoint, is accessible for security reasons. This endpoint checks whether the service is up or not. To enable access to the other endpoints, you will need to set the management.endpoints.web.exposure.include parameter to ‘*’. In sensitive environments, it is essential to secure all the endpoints as they may expose sensitive data.
The link mentioned above provides HATEOAS links for all the endpoints:
{"_links":{"self":{"href":"http://localhost:8080/actuator","templated":false},"beans":{"href":"http://localhost:8080/actuator/beans","templated":false},"caches-cache":{"href":"http://localhost:8080/actuator/caches/{cache}","templated":true},"caches":{"href":"http://localhost:8080/actuator/caches","templated":false},"health":{"href":"http://localhost:8080/actuator/health","templated":false},"health-path":{"href":"http://localhost:8080/actuator/health/{*path}","templated":true},"info":{"href":"http://localhost:8080/actuator/info","templated":false},"conditions":{"href":"http://localhost:8080/actuator/conditions","templated":false},"configprops-prefix":{"href":"http://localhost:8080/actuator/configprops/{prefix}","templated":true},"configprops":{"href":"http://localhost:8080/actuator/configprops","templated":false},"env":{"href":"http://localhost:8080/actuator/env","templated":false},"env-toMatch":{"href":"http://localhost:8080/actuator/env/{toMatch}","templated":true},"loggers":{"href":"http://localhost:8080/actuator/loggers","templated":false},"loggers-name":{"href":"http://localhost:8080/actuator/loggers/{name}","templated":true},"heapdump":{"href":"http://localhost:8080/actuator/heapdump","templated":false},"threaddump":{"href":"http://localhost:8080/actuator/threaddump","templated":false},"metrics":{"href":"http://localhost:8080/actuator/metrics","templated":false},"metrics-requiredMetricName":{"href":"http://localhost:8080/actuator/metrics/{requiredMetricName}","templated":true},"scheduledtasks":{"href":"http://localhost:8080/actuator/scheduledtasks","templated":false},"mappings":{"href":"http://localhost:8080/actuator/mappings","templated":false}}}
Now, let’s take a look at the info endpoint. You have complete control over what is returned by this REST endpoint. The information presented by this endpoint is contributed by different info contributors:
| build | BuildInfoContributor | Exposes build information. |
| env | EnvironmentInfoContributor | Exposes any property from the Environment whose name starts with info. |
| git | GitInfoContributor | Exposes Git related information. |
| java | JavaInfoContributor | Exposes Java runtime information. |
Enabling the env contributor by setting the management.info.env.enabled parameter to true will display all properties with the prefix „info“
If your application.yml file looks like the following:
management:
endpoints:
web:
exposure:
include: '*'
info:
env:
enabled: true
info:
creator: tramotech
email: info@email.com
then the info endpoint will provide the following information:
{"creator":"tramotech","email":"info@email.com"}
Build information is critical. The contributor that provides this information is enabled by default. If a valid META-INF/build-info.properties file is present, Spring Boot Actuator will display the build details. So all you need to do is to populate the build-info.properties file. Fortunately, the Spring Boot Maven plugin includes a build-info goal that can be used to create the build-info.properties file. Here’s an example with custom properties:
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>build-info</goal> </goals> <configuration> <additionalProperties> <custom.otherInfo>foo</custom.otherInfo> <custom.otherInfo2>bar</custom.otherInfo2> </additionalProperties> </configuration> </execution> </executions> </plugin>
and run this maven command:
mvn generate-resources
And this is the output of the info endpoint:
{"creator":"tramotech","email":"info@email.com","build":{"custom":{"otherInfo":"foo","otherInfo2":"bar"},"version":"0.0.1-SNAPSHOT","artifact":"demo-actuator","name":"demo-actuator","time":"2023-04-03T08:43:24.891Z","group":"de.tramotech"}}
Another crucial piece of information is Git information, which is provided by the GitContributor and enabled by default. The contents of git.properties file will be exposed if it is present. To generate this file, you will need to add the git-commit-id-plugin plugin:
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<version>4.0.0</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>revision</goal>
</goals>
</execution>
</executions>
<configuration>
<generateGitPropertiesFile>true</generateGitPropertiesFile>
<dotGitDirectory>${project.basedir}/../.git</dotGitDirectory>
</configuration>
</plugin>
When you build your project the git.properties file will be generated in the classes folder. Here is a snippet of this file:
#Generated by Git-Commit-Id-Plugin #Mon Apr 03 11:30:06 CEST 2023 git.branch=main git.build.host=Ahmeds-MacBook-Pro.fritz.box git.build.time=2023-04-03T11\:30\:06+0200 git.build.user.email=info@email.com git.build.user.name=Ahmed Fikri git.build.version=0.0.1-SNAPSHOT git.closest.tag.commit.count= git.closest.tag.name= git.commit.id=557bc0a5c3f128d0b2d42177be7e452acca1d108 git.commit.id.abbrev=557bc0a
Reading and updating configuration properties:
The env endpoint is another crucial endpoint that provides an overview of all configuration properties in a transparent manner. This endpoint not only allows you to read properties but also enables the modification of properties in the running application.
For example with this command:
curl localhost:8080/actuator/env -d{"name”:”my-property,”value”:”new-value”}
The variable my-property will be set to new-value
To delete a property:
curl localhost:8080/actuator/env -X DELETE {“my-varialbe”:”new-value”}
Actuator also includes an endpoint that enables the shutdown of the application. For instance:
curl -X POST http://localhost:8080/actuator/shutdown
As you can see, Actuator can alter the behavior of your application, which is why it is crucial to secure the exposed endpoints using Spring Boot Security.