In this post, we will look at how we can use the spring boot actuator to add some metrics related endpoints in Spring Boot based microservice or application. Spring boot helps to monitor your production-ready application by offering a number of metrics related endpoints.
What do you need:
- Java 8
- Spring Boot
- IntelliJ
- Gradle
What are Spring boot actuator endpoints?
I will not be showing how to create a Spring boot REST in this article. You can refer my post to create a REST API based on Spring boot here.
As Spring documentation says – “Spring boot actuator let you monitor and interact with your application.” The endpoints that we will be discussing, are built in Spring Boot. Of course, the first question that comes to mind, is how do we activate these endpoints and what different endpoints are available there for what purposes.
How to activate actuator endpoints?
To activate these endpoints in a gradle-based project, we have to add the following dependency
dependencies { compile("org.springframework.boot:spring-boot-starter-actuator") }
If you are using maven, this will be like below:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies>
All the actuator endpoints are enabled by default.
auditevents
– Audit events of your applicationinfo
– Displays information about your application – you can customize to show version information.health
– application’s health statusmetrics
– various metrics information of applicationloggers
– Displays and modifies configured loggerslogfile
– Shows the contents of the log filehttptrace
– Displays HTTP trace info for the last 100 HTTP request/responseenv
– Displays current environment informationflyway
– Shows Flyway database migrations detailsliquidbase
– Shows details of liquibase database migrationsshutdown
– Allows to shut down the application gracefullymappings
– Displays a list of all@RequestMapping
pathsscheduledtasks
– Displays the scheduled tasks in the applicationthreaddump
– Performs a thread dumpheaddump
– Returns JVM head dump
Another way these endpoints can be enabled or disabled, is to add properties in application.properties
as shown below:
management.endpoint.health.enabled=true
Running the spring boot application with actuator
Once we have added the required spring-boot-starter-actuator
dependency, we can build our application. There is still one more change we need to add in our gradle build file to get application version information.
springBoot{ buildInfo() }
If you are using maven, this can be added as below:
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>build-info</goal> </goals> </execution> </executions> </plugin>
If you want to add some additional properties, add following properties in application.properties
as below:
info.app.name = Reusable-Rest-Module info.app.version = 0.1.0-SNAPSHOT management.security.enabled=false
Now once you build the application and run it, this will give us following results:
Conclusion
In this post, we showed how we can use Spring Boot Actuator to get different metrics data for your spring boot based service.