Performing MicroProfile health checks

Configure the mpHealth-1.0 feature and implement the HealthCheck application programming interface (API) to provide health checks in your microservice.

Open Liberty Documentation for the MicroProfile Health feature versions 2.1 and later is available on the Open Liberty website.

About this task

A health check is a MicroProfile Health API implementation that is provided by a microservice. Use health checks to assess the health of a service. These checks are primarily intended as machine to machine mechanisms in containerized environments, such as liveness checks in Kubernetes. The mpHealth-1.0 feature provides a /health endpoint that represents the binary status, either UP or DOWN, of the microservices that are installed. A microservice can supply zero or more health checks, and the overall health of the server is the aggregate of the status from each health check. The overall result is positive only if all health checks report a positive result.

Procedure

  1. Configure the feature. Update the server.xml file to add the feature:
    <featureManager>
       <feature>mpHealth-1.0</feature>
    </featureManager>
  2. Use the API to provide the health checks that you want to perform:
    @FunctionalInterface
    public interface HealthCheck {
    
        HealthCheckResponse call();
    }

    The following example displays how you can use the API for health checks:

    @Health
    @ApplicationScoped
    public class MyCheck implements HealthCheck {
    
       @Override
       public HealthCheckResponse call() {
            return HealthCheckResponse.named("MyCheck")
                    .withData("key1", "val1")
                    .withData("key2", "val2")
                    .up()
                    .build();
    
       }
    }
    The HTTP request to the /health endpoint returns a response with one of the following status codes:
    • Code 200 indicates a health check with a positive outcome.
    • Code 503 indicates that the overall outcome is negative.
    • Code 500 indicates that the system was unable to process the health check request. This code might indicate an error in the procedure.

    The response also contains a JavaScript Object Notation (JSON) payload with the details of the health checks. The following code displays an example of a positive result:
    {
     "outcome": "UP",
     "checks": [
        {
          "name": "MyCheck",
          "state": "UP",
          "data: {
            "key": "value",
            "key1": "val1",
            "key2": "val2"
          }
        }
      ]
    }
    The following code displays an example of a negative result:
    {
     "outcome": "DOWN",
     "checks": [
       {
        "name": "check1",
        "state": "UP"
       },
       {
        "name": "check2",
        "state": "DOWN"
       }
      ]
    }
    If you configure the feature but do not provide any health checks, the system responds with code 200. The payload contains a positive outcome of UP and an empty set of checks.