Helidon: Microservice health check

Health check of our Microservice is part of Microprofile specification. So we don’t need to write anything special with Helidon. All the health API will be available out of the box in our implementation

Health Check APIs

There are 3 endpoints are available as part of the specification to check the health of our microservices. These APIs will be used by the 3rd party application/integration tools to check the health. Those APIs are as follows,

/health
/health/live
/health/ready

/health

This is the top-level API to check the health of our microservice. Apart from the liveness and readiness of our application, we will also get more internal information about services like the following,

  • deadlock
  • diskSpace
  • heapMemory

/health/live

We can check the liveness of our application using this end-point. Any micro profile compliant framework should offer this implementation out of the box. Helidon will also offer such end-point and return meaningful information. In some cases, we may need to customize the liveness end-points. In such use-cases, we need to provide an implementation of HealthCheck interface with @Liveness annotation. See the code below,

//Custom Liveness implementation
@Liveness
@ApplicationScoped
public class DailyApiLiveness implements HealthCheck {
    @Override
    public HealthCheckResponse call() {
        return HealthCheckResponse.named("DailyAPI Liveness")
                .up()
                .withData("time", System.currentTimeMillis())
                .build();
    }
}

/health/ready

This end-point is similar to the above live end-point. The only difference is the @Readiness annotation instead of @Liveness. We can add our custom logic to calculate the readiness to expose the real value to our partners or integration solutions. Please see the sample code below,

//We can add our custom logic to calculate the real readiness and return the response //accordingly.
@Readiness
@ApplicationScoped
public class DailyApiReadiness implements HealthCheck {
    @Override
    public HealthCheckResponse call() {
        return HealthCheckResponse.named("DailyAPI Readiness")
                .up()
                .build();
    }
}

Once everything is set properly, we will get the following response when we access the /health API

{
  "outcome": "UP",
  "status": "UP",
  "checks": [
    {
      "name": "DailyAPI Liveness",
      "state": "UP",
      "status": "UP",
      "data": {
        "time": 1571677530451
      }
    },
    {
      "name": "DailyAPI Readiness",
      "state": "UP",
      "status": "UP",
      "data": {
        "time": 1571677530460
      }
    },
    {
      "name": "deadlock",
      "state": "UP",
      "status": "UP"
    },
    {
      "name": "diskSpace",
      "state": "UP",
      "status": "UP",
      "data": {
        "free": "96.31 GB",
        "freeBytes": 103412359168,
        "percentFree": "41.23%",
        "total": "233.57 GB",
        "totalBytes": 250790436864
      }
    },
    {
      "name": "heapMemory",
      "state": "UP",
      "status": "UP",
      "data": {
        "free": "194.89 MB",
        "freeBytes": 204357016,
        "max": "1.78 GB",
        "maxBytes": 1908932608,
        "percentFree": "98.02%",
        "total": "231.00 MB",
        "totalBytes": 242221056
      }
    }
  ]
}

I hope you have enjoyed the article. Let me know if you need any improvement or suggestion on this topic.

{ Happy coding }

API Evangelist | Chief Hustler @workrituals.com | Habit Coach | Madras Java User Group Leader
81FollowersFollow
0SubscribersSubscribe

Latest articles

Jakarta Tech Talk: Build Microservice using Microprofile + Helidon

Recently I gave a presentation on Jakarta Tech Talk series on 'Building a Microservice using Microprofile + Helidon. Enjoy watching the presentation...

Serverless REST APIs made simple with Cloud functions

Introduction Serverless is one of the top keyword in the integration space right now. Everyone is talking about the...

How to get started with Google cloud functions?

The 'Cloud Functions' is a Google technology bundled as part of the Firebase platform. It is a serverless backend to support our...

Why Firebase is a great technology for ‘Indie Makers’...

Firebase is a technology developed by Google. In this article, I am going to explore why Firebase is a great technology for...

Helidon: 2x productivity with Microprofile REST Client

We have already many articles before on how to write Microservice using the Helidon framework. We are going to further enhance our...

Similar articles