Muatik
Muatik

Reputation: 4171

get notified when spring boot healtcheck status changes

Is there a way to get notified when the status of the registered health check indicators changes? For example, when the healthcheck indicator of database becomes down, I would like to take some actions.

Actually, my final goal is to export healthcheck status to Prometheus' metrics. So, when there is status change, I want to update health metrics.

Upvotes: 1

Views: 2790

Answers (1)

mweirauch
mweirauch

Reputation: 2248

I assume your question refers to Micrometer issue 416 and Micrometer-Docs issue 39.

As per documentation, you can register the custom HealthMetricsConfiguration. The value of the gauge is determined by the status the ComposeHealthIndicator returns and is actually changing depending on the state of the single HealthIndicators.

I am using afformentioned HealthMetricsConfiguration (just with different status value mappings as discussed in issue 416).

Wen't ahead and implemented a custom alternating health indicator:

@Component
public class AlternatingHealthIndicator extends AbstractHealthIndicator {

    @Override
    protected void doHealthCheck(Builder builder) throws Exception {
        int minute = LocalDateTime.now().getMinute();
        boolean minuteIsEven = minute % 2 == 0;
        builder.status(minuteIsEven ? Status.UP : Status.DOWN);
        builder.withDetail("description", "UP when current minute is even; DOWN when current minute is odd");
        builder.withDetail("currentMinute", minute);
        builder.withDetail("minuteIsEven", minuteIsEven);
    }

}

The health gauge exported on the Prometheus endpoint is minutely changing from 1=UP to -2=DOWN. Here's a visualization:

alternating health status visualized in Grafana, queried from Prometheus

Regarding alerting, you can use Grafana alerting or look into Prometheus' Alertmanager.

Upvotes: 3

Related Questions