Spring boot Custom health indicator is beign target multiple times within seconds in our logs

187 Views Asked by At

I have created a custom health indicator to check availability of an external service. This should be executed by a job every 15 minutes AFAIK. The problem is that I see on the logs that methodToCheckAvailability is being called multiple times within the same second. It is flooding our logs.

Custom Healh Indicator

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.actuate.health.Status;
import org.springframework.stereotype.Component;

import java.util.Optional;
import java.util.function.Consumer;

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

@Slf4j
@Component
@RequiredArgsConstructor
public class BananaHealthIndicator implements HealthIndicator {

    public static final Status PARTIAL_DOWN = new Status("PARTIAL_DOWN");

    @Override
    public Health health() {
        var builder = Health.up();

        var someValues = SomeService.getMap();


        doHealthCheck(builder, value, "ExternalService", ExternalService::methodToCheckAvailability);


        return builder.build();
    }

    private void doHealthCheck(Health.Builder builder, String value, String component, Consumer<String> healthCheck) {
        try {
            healthCheck.accept(value);
        } catch (Exception e) {
            log.atError().setCause(e)
                    .log("Health check failed for {}", component);

            builder.status(PARTIAL_DOWN)
                    .withDetail(value, component + ": " + ExceptionUtils.getMessage(e));
        }
    }
}

properties:

management:
  endpoint:
    health:
      status:
        http-mapping:
          partial_down: 500
      show-details: when_authorized
      probes:
        enabled: true
      group:
        banana:
          include: banana
          show-details: when_authorized
          show-components: when_authorized
          status:
            order: "partial_down,unknown,up"

/actuator/health response

{
    "status": "UP",
    "components": {
        "clientConfigServer": {
            "status": "UP",
            "details": {
                "propertySources": [
                    "configClient"
                ]
            }
        },
        "banana": {
            "status": "UP"
        },
        "livenessState": {
            "status": "UP"
        },
        "ping": {
            "status": "UP"
        },
        "readinessState": {
            "status": "UP"
        },
        "redis": {
            "status": "UP",
            "details": {
                "cluster_size": 2,
                "slots_up": 16384,
                "slots_fail": 0
            }
        },
        "refreshScope": {
            "status": "UP"
        }
    },
    "groups": [
        "banana",
        "liveness",
        "readiness"
    ]
}

When /actuator/health gets called, methodToCheckAvailability gets called within the created BananaHealthIndicator. My guess is maybe Kubernetes is calling health endpoint multiple times to check if the service is running and that is making methodToCheckAvailability get called multiple times.

Any help is appreciated

0

There are 0 best solutions below