user2921180
user2921180

Reputation: 11

Customize Overall Health of Spring Boot Actuator Health Check

I'm setting up the health check for my spring application and as part of it I created a custom health indicator to check a downstream call's own health check. If it's down, the downstream call is marked as DOWN. Right now, the health check looks like the following

{
    "status" : "DOWN",
    "downstreamCall" : {
        "status" : "DOWN",
        "Error Code" : 1
     },
     .
     .
     .
     "diskSpace" : {
         "status" : "UP",
         "free" : 209047318528,
         "threshold" : 10485760
     }
}

So the downstreamCall is being marked as down, when it's down, as it should. However, when the downstreamCall is marked as down, I want the overall status to be marked as DEGRADED, as shown below.

{
    "status" : "DEGRADED",
    "downstreamCall" : {
        "status" : "DOWN",
        "Error Code" : 1
     },
     .
     .
     .
     "diskSpace" : {
         "status" : "UP",
         "free" : 209047318528,
         "threshold" : 10485760
     }
}

This is specifically for the downstreamCall. Any other part of the health check that is DOWN should still result in the overall status being DOWN. I've been looking through the Spring documentation as well as questions here but haven't found a way to achieve this.

Upvotes: 0

Views: 1435

Answers (1)

User51
User51

Reputation: 1109

Unclear when it was added, but since at least SpringBoot 2 (and beyond) custom types are supported...

See also: https://docs.spring.io/spring-boot/docs/2.2.x/reference/html/production-ready-features.html

Add something such as the following to your application.yml:

management.endpoint.health.status:
  order: fatal,down,out-of-service,jones,unknown,up
  http-mapping:
    up: 200
    jones: 410
    down: 200
    fatal: 500
    out-of-service: 503

Then, in a custom health component, you can do something like:

Health.Builder jonser = Health.status("jones");
// Add more detail here if you'ld like.
return jonser.build();

This results in a health status body of something to the order of:

{
    "status": "jones",
    "components": {
    // etc...
    }
}

NOTE: In my tests, if you skip the order part, the status will come out as UNKNOWN.

Upvotes: 0

Related Questions