Health checks

One of the extremely critical aspects of ensuring the high performance of the application is to monitor the health of the application. A production-grade application is always under observation of a specialized monitoring and alerting software. Threshold values are configured for every parameter, whether it be average response time, disk utilization, or CPU utilization. Once the parameter value exceeds the specified threshold value, the monitoring software signals an alert via email or notifications. Development and operations teams take the necessary actions to ensure the application is back to its normal state.

For the Spring Boot application, we can collect health information by navigating to the /actuator/health URL. The health endpoint is enabled by default. For the application deployed in the production environment, health information gathered using the health endpoint can be sent to a monitoring software for alerting purposes.

The information presented by the health endpoint depends on the management.endpoint.health.show-details property. The following is the list of supported values for the property:

  • always: It indicates that all the information should be shown to all users.
  • never: It indicates that the details should never be shown.
  • when-authorized: This indicates that the details are shown to users with authorized roles only. The authorized roles can be configured using the management.endpoint.health.roles property.

The default value for the show-details property is never. Also, the user can be considered authorized when it has one or more of the endpoint's authorized roles. By default, none of the roles are configured as authorized. So, all authenticated users are considered authorized users.

HealthIndicator is one of the important interfaces that provides an indication of application health on different aspects, such as disk space, data source, or JMS. The health endpoint collects health information from all the HealthIndicator implementation beans defined in the application's context. Spring Boot comes with an auto-configured set of health indicators. The framework is flexible enough to support custom health indicator implementations. The final health status of the application is derived by HealthAggregator. The health aggregator sorts statuses from all the health indicators as per the order of statuses that have been defined.

Here is a list of auto-configured HealthIndicators by Spring Boot:

  • CassandraHealthIndicator: Checks whether the Cassandra database is up
  • DiskSpaceHealthIndicator: Checks whether enough disk space is available
  • DataSourceHealthIndicator: Checks whether the connection with the data source can be obtained or not
  • ElasticSearchHealthIndicator: Checks whether the elasticsearch cluster is up
  • InfluxDbHealthIndicator: Checks whether the Influx server is up and running
  • JmsHealthIndicator: Checks whether the JMS broker is up and running
  • MailHealthIndicator: Checks whether the mail server is up and running
  • MongoHealthIndicator: Checks whether the Mongo database is up and running
  • Neo4jHealthIndicator: Checks whether the Neo4j server is up and running
  • RabbitHealthIndicator: Checks whether the Rabbit server is up and running
  • RedisHealthIndicator: Checks whether the Redis server is up and running
  • SolrHealthIndicator: Checks whether the Solr server is up and running

These health indicators are auto-configured based on the appropriate Spring Boot starter configuration.

The following is the example disk space health check output when we navigate to the http://localhost:8080/actuator/health URL:

{
"status": "UP",
"details": {
"diskSpace": {
"status": "UP",
"details": {
"total": 407250137088,
"free": 392089661440,
"threshold": 10485760
}
}
}
}

We can add additional customized health indicators to include the information we want to see. The customized health indicator will be displayed in the result of the health endpoint. It is super easy to create and register a custom health indicator.

The following is an example of a custom health indicator:

package com.packt.springhighperformance.ch09.healthindicators;

import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.stereotype.Component;

@Component
public class ExampleHealthCheck extends AbstractHealthIndicator {
@Override
protected void doHealthCheck(Health.Builder builder)
throws Exception
{
// TODO implement some check
boolean running = true;
if (running) {
builder.up();
} else {
builder.down();
}
}
}

We have to create a Java class that extends from AbstractHealthIndicator. In the custom health indicator class, we have to implement the doHealthCheck() method. The method expects a Health.Builder object to be passed. If we find that the health parameters OK, then the builder.up() method should be called, otherwise the builder.down() method should be called. 

The following is the output rendered on the page when the /actuator/health URL is hit:

{
"status": "UP",
"details": {
"exampleHealthCheck": {
"status": "UP"
},
"diskSpace": {
"status": "UP",
"details": {
"total": 407250137088,
"free": 392071581696,
"threshold": 10485760
}
},
"db": {
"status": "UP",
"details": {
"database": "MySQL",
"hello": 1
}
}
}
}

The custom health indicator is not required to be registered. The @Component annotation is scanned and the bean is registered with the ApplicationContext.

So far, we have learned, in detail, about Spring Boot with examples. The following sections will focus on the use of Spring Boot with microservices.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset