How to exclude redis check in spring actuator health
Asked Answered
P

3

12

Service health check api response 503 when redis is down.

{
    "status": "DOWN",
    "details": {
        "diskSpace": {
            "status": "UP",
            "details": {
                "total": 250790436864,
                "free": 95412813824,
                "threshold": 10485760
            }
        },
        "db": {
            "status": "UP",
            "details": {
                "database": "PostgreSQL",
                "hello": 1
            }
        },
        "refreshScope": {
            "status": "UP"
        },
        "redis": {
            "status": "DOWN",
            "details": {
                "error": "org.springframework.data.redis.connection.PoolException: Could not get a resource from the pool; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to localhost:6379"
            }
        }
    }
}

But actually service is available when redis is down.

Could service status not be effected by redis status ? and I also need see the redis detail in health check api.

Puma answered 10/4, 2020 at 7:49 Comment(0)
F
23

management.health.redis.enabled=false

Add this code to disable the Redis health check in your application.yaml/properties

Fernand answered 21/8, 2020 at 16:29 Comment(1)
How can I upvote this 100 times ?!Lathan
L
4

I usually check https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html this to find something like what you are doing

and there I found management.health.redis.enabled (default true) so my guess would be if you explicitly set this to false it would disappear from your healthcheck

Lounging answered 10/4, 2020 at 8:30 Comment(2)
Many Thanks. and after config management.health.redis.enabled to false, redis health check has disapeared. But is there anyway i could check redis status in actuator?Puma
i think (haven't done it before) you need to make a custom health indicator for redis then (tip extend the default one) and make it always return UP and in the details add its actual status otherwise your main health check will be DOWN when redis is downLounging
R
1

We solved this issue by overriding getAggregateStatus of SimpleStatusAggregator so that the redis status is shown in the details, but no influence in the aggregated status.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.*;
import org.springframework.boot.actuate.redis.RedisHealthIndicator;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import static java.util.Collections.singletonList;
import static java.util.function.Predicate.not;
import static java.util.stream.Collectors.toList;


@Component
public class CustomStatusAggregator extends SimpleStatusAggregator {

    @Autowired
    private List<HealthContributor> healthContributorList;

       
    @Override
    public Status getAggregateStatus(Set<Status> statuses) {
        return super.getAggregateStatus(getStatuses());
    }

    private Set<Status> getStatuses() {
        Stream<Status> statuses = healthContributorList //
                .stream() //
                .map(CustomStatusAggregator::getHealthIndicators) //
                .flatMap(List<HealthIndicator>::stream) //
                .filter(not(RedisHealthIndicator.class::isInstance)) //
                .map(HealthIndicator::health) //
                .map(Health::getStatus);

        return statuses.collect(Collectors.toSet());
    }

    private static List<HealthIndicator> getHealthIndicators(HealthContributor healthContributor) {
        if (healthContributor instanceof HealthIndicator) {
            return singletonList(((HealthIndicator) healthContributor));
        }

        return stream((CompositeHealthContributor) healthContributor) //
                .map(NamedContributor::getContributor) //
                .map(CustomStatusAggregator::getHealthIndicators) //
                .flatMap(List<HealthIndicator>::stream) //
                .collect(toList());
    }

    private static <T> Stream<T> stream(Iterable<T> iterable) {
        return StreamSupport.stream(iterable.spliterator(), false);
    }

}

If you have a combination of ReactiveHealthContributor and HealthContributor, you can combine the status

@Autowired
private List<ReactiveHealthContributor> reactiveHealthContributorList;

return Stream.concat(statuses, reactiveStatuses).collect(Collectors.toSet());
Rhapsody answered 19/10, 2022 at 8:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.