Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to aggregate health indicators in Spring boot

I've added Actuator support for my Spring 2.0.4 application using this Baeldung article. In section 4.4 it talks about

A handy feature of health indicators is that we can aggregate them as part of a hierarchy

but it doesn't go into any discussion of how to do this aggregation. Nor have I been able to find any documentation on how to do this.

Question Do any of you know of a tutorial, example or other documentation on creating such an aggregation?

More Info I have a service in my application which relies on several sub-components. The service itself is only considered down if all these sub-components are down. So long as one is up then the service is up. Currently using the normal HealthIndicator mechanism if one of the sub-components is down it marks the server as down.

It seems I would want to use the CompositeHealthIndicator but it's not clear how I create the child HealthIndicators without the system picking them up. The caveat is that each of these sub-components uses the @Scheduled annotation, my understanding is that in order for that to work the class must use the @Component annotation(or some such) which will cause it to be created and sucked up into the application health.

Clarification I have added actuators and the health URL comes up as this:

{"status":"UP","details":{"MyServ1":{"status":"UP","details":{"Latency":...}},"MyServ2":{"status":"UP","details":{"Latency":...}},"diskSpace":{"status":"UP","details":{"total":...,"free":...,"threshold":...}}}}

but if 'MyServ1' or 'MySrv2' are down the overall status is down, but I only want that to happen if 'diskSpace' is down OR 'MyServ1' and 'MyServ2' is down.

It would appear that CompositeHealthIndicator would be the appropriate class for this, it is just unclear how I create the children health indicators for it (just use new)?

Thanks in advance

like image 538
Jim M. Avatar asked Dec 23 '22 04:12

Jim M.


2 Answers

Aggregating the statuses from each health indicator into a single overall status is done by an implementation of org.springframework.boot.actuate.health.HealthAggregator. Spring Boot auto-configures an instance of OrderedHealthAggregator. If you provide your own bean that implements HealthAggregator the auto-configured aggregator will back off in favour of your custom implementation.

The aggregator's aggregate method is called with a Map<String, Status> where the keys are the names of the health indicators and the values are their statuses. Knowing the names of your sub-components' health indicators should allow you to perform custom aggregation for them.

like image 134
Andy Wilkinson Avatar answered Jan 10 '23 06:01

Andy Wilkinson


I came up with a solution, I posted a simple demonstration on GitHub. I have no idea if this is the correct way to do this or not but it seems to be working...

Example Spring Application

like image 30
Jim M. Avatar answered Jan 10 '23 06:01

Jim M.