Health Check Endpoints - waterdog-oss/ktor-template GitHub Wiki

Ktor Template Health Check

Setup

Checks must be defined as extension functions of Health.Configuration inside HealthCheck.kt file. We already have 2 default probes - liveness and readiness - and they are activated in Application.kt:

install(Health) {
    liveness()
    readiness()
}

Default probes

By default we have 2 probes endpoints:

  • /liveness
    • Will return 200OK if ktor is running correctly
  • readiness
    • Will return 200OK if ktor was able to access database

If any check fails in these endpoints, 500 will be returned.

Liveness response

{
    "alive": true
}

Readiness response

{
    "database": true
}

Custom probes

Any additional prob can be added with customCheck method. This method receives 3 arguments:

  • url
  • name
    • this will be the content of the json response. For instance, liveness prob could be defined by customCheck("/liveness", "alive") { true }
  • check
    • the function containing the code that will actually verify if this check is ok or not
    • must return a boolean

Multiple checks in the same prob

We can set as many checks as we want for each probe endpoint. Say we have an algorithm running in a micro service and we want to check his readiness along with the database. Our setup would look something like this:

customCheck("/readiness", "database") { // our database ping }
customCheck("/readiness", "algorithm") { // our algorithm ping }

In fact, because readyCheck is a facilitator method that internally calls customCheck("/readiness", ... ), it could be simplified to:

readyCheck("database") { // our database ping }
readyCheck("algorithm") { // our algorithm ping }

The response will be

{
    "database": true,
    "algorithm": true
}