{{announcement.body}}
{{announcement.title}}

Application Monitoring With Spring Boot Actuator

DZone 's Guide to

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.

· Java Zone ·
Free Resource

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.

Features

  • 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.

Important Endpoints

Spring Boot Actuator provides the listed HTTP and JMX endpoint. We will discuss in detail later part of this article.

HTTP and JMX endpoint

Building Project

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.


Spring initializer

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.

Groovy
 




x


1
plugins {
2
    id 'org.springframework.boot' version '2.2.4.RELEASE'
3
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
3
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
4
    id 'java'
5
}
6
 
          
7
group = 'com.sanju'
8
version = '0.0.1-SNAPSHOT'
9
sourceCompatibility = '1.8'
10
 
          
11
repositories {
12
    mavenCentral()
13
}
14
 
          
15
dependencies {
16
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
17
    implementation 'org.springframework.boot:spring-boot-starter-web'
18
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
19
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
20
    }
21
}
22
 
          
23
test {
24
    useJUnitPlatform()
25
}
26
 
          



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.

JSON
 




xxxxxxxxxx
1
23


 
1
// 20200222142835
2
// http://localhost:8080/actuator/
3
 
          
4
{
5
  "_links": {
6
    "self": {
7
      "href": "http://localhost:8080/actuator",
8
      "templated": false
9
    },
10
    "health-path": {
11
      "href": "http://localhost:8080/actuator/health/{*path}",
12
      "templated": true
13
    },
14
    "health": {
15
      "href": "http://localhost:8080/actuator/health",
16
      "templated": false
17
    },
18
    "info": {
19
      "href": "http://localhost:8080/actuator/info",
20
      "templated": false
21
    }
22
  }
23
}



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.

Properties files
 




x




1
management.endpoints.web.base-path=/custom-path
2
management.server.port=8070


After deployment with these changes, we will get the same output with http://localhost:8070/custom-path/ URL.

Expose Endpoints

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.

Properties files
 




x


1
management.endpoints.web.base-path=/custom-path
2
management.server.port=8070
3
management.endpoints.jmx.exposure.include=*
4
management.endpoints.jmx.exposure.exclude=health, metrics
5
management.endpoints.web.exposure.include=*
6
management.endpoints.web.exposure.exclude=health, metrics



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.

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. 

Java
 




xxxxxxxxxx
1
49


 
1
package com.sanju.actuatordemo.core;
2
 
          
3
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
4
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
5
import org.springframework.stereotype.Component;
6
 
          
7
import java.net.InetAddress;
8
import java.net.NetworkInterface;
9
import java.time.LocalDate;
10
import java.time.LocalTime;
11
import java.util.ArrayList;
12
import java.util.List;
13
 
          
14
@Component
15
@Endpoint(id = "server-info")
16
public class ServerInfoActuatorEndpoint {
17
 
          
18
    @ReadOperation
19
    public List<String> getServerInfo() {
20
        List<String> serverInfo = new ArrayList<String>();
21
        try {
22
            serverInfo.add("Server IP Address : " + InetAddress.getLocalHost().getHostAddress());
23
            serverInfo.add("Host Name: " + InetAddress.getLocalHost().getHostName());
24
            serverInfo.add("Server OS : " + System.getProperty("os.name").toLowerCase());
25
            NetworkInterface network = NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
26
            byte[] mac = network.getHardwareAddress();
27
            StringBuilder sb = new StringBuilder();
28
            for (int i = 0; i < mac.length; i++) {
29
                sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
30
            }
31
            serverInfo.add("Hardware Address : "+ sb.toString());
32
            serverInfo.add("Java Version : "+getJavaVersion());
33
        } catch (Exception e) {
34
            e.printStackTrace();
35
        }
36
        return serverInfo;
37
    }
38
    private int getJavaVersion() {
39
        String version = System.getProperty("java.version");
40
        if(version.startsWith("1.")) {
41
            version = version.substring(2, 3);
42
        } else {
43
            int dot = version.indexOf(".");
44
            if(dot != -1) { version = version.substring(0, dot); }
45
        } return Integer.parseInt(version);
46
    }
47
 
          
48
}
49
 
          



Spring Boot Actuator provides some annotation by which we just configured our system.  @Endpoint annotation enabled it as an endpoint and annotations  @WriteOperation ,  @ReadOperation ,  @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.

JSON
 




xxxxxxxxxx
1
10


1
// 20200222160924
2
// http://localhost:8070/custom-path/server-info
3
 
          
4
[
5
  "Server IP Address : 192.168.0.177",
6
  "Host Name: Sanjoys-MacBook-Pro.local",
7
  "Server OS : mac os x",
8
  "Hardware Address : A4-5E-60-F2-07-51",
9
  "Java Version : 8"
10
]



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.

Groovy
 




x


1
compile 'org.springframework.boot:spring-boot-starter-security'



To define username and password we need to add the following lines to the application.properties file.

Groovy
 




xxxxxxxxxx
1


 
1
spring.security.user.name=admin
2
spring.security.user.password=admin


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..

Sign in

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.

Java
 




x



1
package com.sanju.actuatordemo.core;
2
 
          
3
import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;
4
import org.springframework.context.annotation.Bean;
5
import org.springframework.context.annotation.Configuration;
6
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
7
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
8
import org.springframework.security.core.userdetails.User;
9
import org.springframework.security.core.userdetails.UserDetails;
10
import org.springframework.security.core.userdetails.UserDetailsService;
11
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
12
import org.springframework.security.crypto.password.PasswordEncoder;
13
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
14
 
          
15
 
          
16
@Configuration
17
public class SecurityConfig extends WebSecurityConfigurerAdapter {
18
    @Override
19
    protected void configure(HttpSecurity http) throws Exception {
20
        http.requestMatcher(EndpointRequest.toAnyEndpoint()).authorizeRequests()
21
                .antMatchers("/custom-path/**").hasRole("ADMIN")
22
                .and()
23
                .httpBasic();
24
    }
25
 
          
26
    @Bean
27
    @Override
28
    public UserDetailsService userDetailsService() {
29
        UserDetails user =
30
                User.withUsername("admin")
31
                        .password(passwordEncoder().encode("admin"))
32
                        .roles("ADMIN")
33
                        .build();
34
        return new InMemoryUserDetailsManager(user);
35
    }
36
 
          
37
    @Bean
38
    public PasswordEncoder passwordEncoder() {
39
        return new BCryptPasswordEncoder();
40
    }
41
}
42
 
          



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.

Credentials pop up

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

With the  /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.

Properties files
 




xxxxxxxxxx
1


1
info.app.name=Spring Boot actuator Test Application
2
info.app.description=Sample application for article
3
info.app.version=1.0.0


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 

Groovy
 




xxxxxxxxxx
1


1
springBoot {
2
    buildInfo()
3
}


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.

Groovy
 




xxxxxxxxxx
1


1
plugins {
2
    id 'org.springframework.boot' version '2.2.4.RELEASE'
3
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
4
    // added plugin to access git information  
5
    id "com.gorylenko.gradle-git-properties" version "1.5.1"
6
    id 'java'
7
}


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.

Java
 




xxxxxxxxxx
1
15


1
package com.sanju.actuatordemo.core;
2
 
          
3
import org.springframework.boot.actuate.info.Info;
4
import org.springframework.boot.actuate.info.InfoContributor;
5
import org.springframework.stereotype.Component;
6
 
          
7
@Component
8
public class CustomInfoContributor  implements InfoContributor {
9
    @Override
10
    public void contribute(Info.Builder builder) {
11
        builder.withDetail("customInfo","This is custom info indicator. You can add your application data. " +
12
                "You can share application persistent data from here");
13
    }
14
}
15
 
          


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.

Properties files
 




xxxxxxxxxx
1


1
management.endpoint.health.show-details=always


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  HealthIndicator  interface.

Java
 




xxxxxxxxxx
1
17


 
1
package com.sanju.actuatordemo.core;
2
import org.springframework.beans.factory.annotation.Autowired;
3
import org.springframework.boot.actuate.health.Health;
4
import org.springframework.boot.actuate.health.HealthEndpoint;
5
import org.springframework.boot.actuate.health.HealthIndicator;
6
import org.springframework.stereotype.Component;
7
 
          
8
@Component
9
public class CustomHealthIndicator  implements HealthIndicator {
10
    @Override
11
    public Health health() {
12
        Health health = Health.up().withDetail("Details","Server is up").build();
13
        return health;
14
    }
15
}
16
 
          


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.

Happy Coding!!!

You can read my original article from here

Topics:
actuator endpoint ,actuator endpoints ,application monitoring ,java ,monitoring app ,performance ,spring ,spring boot ,spring boot 2

Published at DZone with permission of Sanjoy Kumer Deb . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}