How to Visualise Spring Boot Actuator Health Indicators in Spring Boot Admin

The Spring Boot Admin project provides a user-friendly way to manage and monitor a Spring Boot application with very little effort.

The Spring Boot Admin project provides a user-friendly way to manage and monitor a Spring Boot application with very little effort.

Spring Boot Admin is a web application that helps you to monitor and manage a Spring Boot application. It can gather auditing, health and metric data. In this blogpost, we will first shed light on what a health indicator is and how to create a custom one. Next, we’re going to visualize our custom health indicator by means of Spring Boot Admin.

Health indicator

A Spring Boot Actuator Health Indicator provides health information of a Spring Boot Application. This health information is accessible at the /health endpoint and can be consumed by monitoring software. By default, multiple Health Indicators are auto-configured.

Custom health indicator

A custom health indicator can be defined by implementing the HealthIndicator interface. As an example, I created a BlogEnpointStatusHealthIndicator bean. This bean returns 2 values: “Health status” and “Up since”. The “Health status” property indicates whether our endpoint is up or not and the “Up Since” property indicates since when our endpoint has been up. These properties will automatically be added to the JSON of the /health endpoint:

@Component

public class BlogEndpointStatusHealthIndicator implements HealthIndicator {

private BlogService blogService;


@Override

public Health health() {

    try {

        Health.Builder healthBuilder;

        healthBuilder = blogService.isBlogEndpointUp() ? Health.up() : Health.down();


        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_DATE_TIME;

        LocalDateTime availableSince = blogService.getAvailableSince();


        return healthBuilder.withDetail("up since", uptime.format(dateTimeFormatter))

                .build();

    } catch (Exception e) {

        return Health.down(e).build();

    }

}


@Autowired

public void setBlogService(BlogService blogService) {

    this.blogService = blogService;

}

}

Resulting JSON:

{  

  "status":"UP",

  "blogEndpointStatus":{  

     "status":"UP",

     "up since":"2017-06-26T15:56:28.574"

  },

  "diskSpace":{  

     "status":"UP",

     "total":497687719936,

     "free":201710972928,

     "threshold":10485760

  }

}
Spring Boot Admin

The Spring Boot Admin project simplifies the administration of Spring Boot applications by providing a simple user interface. In order to incorporate Spring Boot Admin into your application, you first need to set up a separate Spring Boot Admin server. This server will collect data from one or more Spring Boot Admin clients.

Setup Spring Boot Admin Server

To set up a Spring Boot Admin server, create a separate Spring Boot project. After your Spring Boot project has been created, add the following info to your pom.xml:

<dependency>

<groupId>de.codecentric</groupId>

<artifactId>spring-boot-admin-server</artifactId>

   <version>1.5.2</version>

</dependency>

<dependency>

<groupId>de.codecentric</groupId>

<artifactId>spring-boot-admin-server-ui</artifactId>

<version>1.5.2</version>

 

</dependency>

Next, add the @EnableAdminServer annotation to your configuration. By defining this annotation, the application will automatically pull in the Spring Boot Admin Server configuration.

@Configuration

@EnableAutoConfiguration

@EnableAdminServer

public class SpringBootAdminApplication {

public static void main(String[] args) {

    SpringApplication.run(SpringBootAdminApplication.class, args);

}

 

}

Finally, add the property management.security.enabled to your properties file. This property will disable the security on your Spring Boot Admin server endpoints (don’t use this in a production environment!). To secure your endpoints, look at how to Secure Spring Boot Admin: management.security.enabled=false

Setup Spring Boot Admin Client

To register an application to a Spring Boot Admin Server, you have to include the Spring Boot Admin Client project in your pom.xml:

<dependency>

   <groupId>de.codecentric</groupId>

   <artifactId>spring-boot-admin-starter-client</artifactId>

   <version>1.5.2</version>

 

</dependency>

Next, add the property spring.boot.admin.url to your properties file. This property should point to the URL of your Spring Boot Admin server:

spring.boot.admin.url=http://localhost:8080

By defining the spring.boot.admin.url property, the Spring Boot Admin Client will periodically try to register itself at the Spring Boot Admin server.

Spring Boot Admin server overview

Now, start up your Spring Boot Admin and Spring Boot Client server and navigate to http://localhost:8080. If all went well, you should now be able to see your client application and server registered in the overview.

Our client indicates status “up”, so this means that our application is running. When we click on the “details”-button, you can see our custom health indicator, BlogEndpointStatus, indicating an “up”-status and an “Up Since” date.

Apart from some health indicators, you can also find info about memory, JVM, garbage collection and Servlet containers on this tab. Next to the “Details” tab, you also have the following tabs:

  • Environment: Show all configuration properties that are used by the client;

  • Logging: View or alter the logging configuration for a class;

  • JMX: Interact with JMX-beans;

  • Threads: View all threads and their status;

  • Audit: View all audit events;

  • Trace: View all requests that were issued at the application;

  • Heapdump: Download a heapdump.

An in-depth analysis of these tabs, however, is outside the scope of this blogpost. If you want to get more info and/or code samples, you can always consult the Spring Boot Admin Reference Guide.

Note: If you’re considering using Spring Boot Admin in a production environment, then definitely look at the Security Section in the Spring Boot Admin Reference Guide. Here, you can find all the necessary info to secure your Spring Boot Admin interface.

Conclusion

The Spring Boot Admin project provides a user-friendly way to manage and monitor a Spring Boot application with very little effort. We demonstrated this by defining a custom Health Indicator and visualising it with Spring Boot Admin. Happy coding!

Dit blog is geschreven door de specialisten van Foreach.

Inmiddels is Foreach onderdeel van iO. Meer weten? Neem gerust contact op!

logo iO