Application Monitoring With Spring Boot Actuator
Application Monitoring With Spring Boot Actuator
The Spring Boot Actuator is a module built into Spring Boot that has a number of features that make it easy to manage and monitor applications.
Join the DZone community and get the full member experience.Join For Free
Monitoring production is an important part of a software service provider. Many companies providing monitoring systems for maintaining the production environment. Spring Boot comes with different awesome modules that developers can easily configure and maintain development and production environments with. The actuator module provides production-ready features by which we can easily maintain the production environment. The actuator exposes JMX and HTTP endpoints.
- Endpoints: Spring Boot Actuator provides some default endpoints by which we can access application information. We can also monitor the production environment with those endpoints. Endpoints can also be accessed by third-party monitoring tools.
- Metrics: We can access OS and JVM related information using spring boot actuator endpoints. This is very useful for runtime environment. Spring boot actuator provides this feature by integrating with micrometer application monitoring.
- Audit: Spring Boot Actuator publishes events to
AuditEventRepository. Spring security by default publishes “authentication success,” “failure,” and “access denied” exceptions. This is a very useful feature for reporting and authentication failures. Auditing can be enabled by AuditEventRepository. By default, spring-boot provides InMemoryAuditEventRepository for auditing which has limited capabilities.
- HTTP Tracing: Spring boot actuator also provides an HTTP tracing facility. If you want to use it you have to include web endpoint. Http tracing provides the information about request-response exchange.
Spring Boot Actuator provides the listed HTTP and JMX endpoint. We will discuss in detail later part of this article.
As we know Spring Boot provides some starter dependency for different spring modules we can create applications from Spring Initializr with web and actuator modules. You can follow the image instruction. We will use Gradle as a build tool.
Click on the Generate button to generate the initial project. After that copy this project in a location on your PC and unzip it. Go to the project's root location and open terminal. Your initial build file looks like the below code.
So now we will deploy the project. Run
gradle bootrun from terminal. With the default configuration, the application will run at 8080 port with a management path /actuator. After deployment completion, we will browse the URL http://localhost:8080/actuator/ from a browser. With the default configuration, the actuator module exposes four endpoints that we can see as the response.
The Spring Boot Actuator module provides the facility to change the management port and path by adding some property in application.properties file. So, we will add the following lines of code to application.properties file.
After deployment with these changes, we will get the same output with http://localhost:8070/custom-path/ URL.
With the default configuration, we can access only four endpoints, but the Spring Actuator has more endpoints like metrics, HTTP trace, audit events, etc. If we want to access such endpoints we need to configure them. Spring boot actuator provides some configuration to include and exclude endpoints from the user. Here is an example of the application.properties file.
After deployment with this properties file, we will check different endpoints. We can see that without health and metrics endpoint maximum endpoints will work. To make all endpoints make workable we need to add more configuration related to specific endpoints. If we hit http://localhost:8070/custom-path/ , we can see that the endpoints exposed by actuator.
Here we can see that health and metrics endpoint is missing because we excluded then from our configuration.
Add Custom Endpoints
Spring Boot Actuator provides the facility to write custom endpoints from where we can see our own customized application data. For example, I am using a custom endpoint to check my server address, server deployed in which OS, MAC address of the server and which java version installed in the server machine.
Spring Boot Actuator provides some annotation by which we just configured our system.
@Endpoint annotation enabled it as an endpoint and annotations
@DeleteOperation to perform like POST, GET, DELETE operations in HTTP verbs. Here we just used the
@ReadOperation annotation. So if we now redeploy the application and hit with URL http://localhost:8070/custom-path/server-info, we will get the following output.
Spring Security with Actuator
Spring Boot Actuator exposes some endpoints which are really sensitive. Those endpoints contains many system and core application-related information like beans, metrics and configuration related information. So we have to make access restricted for endpoints. For this purpose, we can use Spring security. Spring Boot Actuator provides auto-configuration of spring security. To make endpoints secured we have to add the following dependency to build.properties file.
To define username and password we need to add the following lines to the application.properties file.
Spring Boot Actuator security auto-configuration is done. We need to restart the server again to enable Spring security. After redeploying the application, we have to hit http://localhost:8070/custom-path/server-info. URL will be redirect to http://localhost:8070/login..
As we have configured admin as username and password, we need to enter username and password and click on Sign in button. If we provide the wrong username or password it will give the "Bad credentials" error. Otherwise, redirect to our desire page. By default
/info and /health endpoints are not restricted. We can access those without credentials.
Since Spring Boot 2, we can configure endpoint security config with Spring security by extending the
WebSecurityConfigurerAdapter class. I have created a class
SecurityConfig which extends
WebSecurityConfigurerAdapter and overrides configure and
userDetailsService method. I have also included bcrypt encoder as a password encoder.
With this configuration, we can restrict specific endpoints as we want. So now we hit URL http://localhost:8070/custom-path/info from browser it gives a pop-up for credentials.
So we have to enter our credentials to access the specific URL otherwise we will get 403 Unauthorized response. May we can get further details from spring security.
More About /info Endpoint
/info endpoint we want to get basic information about the application. For example, we can define static properties about the application by defining them to application.properties file.
After this configuration added, we can browse http://localhost:8070/custom-path/info URL. We should get the following output
We can add other information like build info, git info of application. So, we have to add the following lines of code to build.properties file. For build info, we have to add
And for git information we have to initialize git repository by executing git init command and then add files and commit to git by executing git add -A, git commit -m "Initial commit" respectively. And then add the following plugin to build.properties.
We have just completed the configuration for different application information.
We can see the custom information of the application by using the
InfoContributor interface. We will create
CustomInfoContributor.java by implementing InfoIndicator.
So we will hit http://localhost:8070/custom-path/info URL from the browser and will get the following response.
So, here we have got the all information with custom information.
More About /health Endpoint
With the default configuration, health endpoint returns the only information that the server is up or down. But we can check details also by adding the following line of code to application.properties file. Here value can be when-authorized, always or never against key management.endpoint.health.show-details.
We can also add details of custom health information with details response. That's why we have to add a custom class that will implement the
After re-deploy the server we can browse http://localhost:8070/custom-path/health URL from the browser. We will get the below response.
Spring Boot Actuator contains many useful endpoints, more than than I explained above. The actuator provides many useful features in production environment. There are a lot of things to discuss but this article already goes too long. I will discuss the details of some other endpoints and features in my next article. Thanks.
You can read my original article from here
Published at DZone with permission of Sanjoy Kumer Deb . See the original article here.
Opinions expressed by DZone contributors are their own.