All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.dropwizard.health.core.HealthCheckConfigValidator Maven / Gradle / Ivy

Go to download

Provides a health check implementation that performs ongoing monitoring of an application's dependencies and includes an endpoint that can be called by a load balancer to determine if the application is healthy and thus able to retrieve traffic.

The newest version!
package io.dropwizard.health.core;

import io.dropwizard.health.conf.HealthCheckConfiguration;

import com.codahale.metrics.health.HealthCheckRegistry;
import io.dropwizard.lifecycle.Managed;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

public class HealthCheckConfigValidator implements Managed {
    private static final Logger log = LoggerFactory.getLogger(HealthCheckConfigValidator.class);
    private final List configs;
    private final HealthCheckRegistry registry;

    public HealthCheckConfigValidator(List configs, HealthCheckRegistry registry) {
        this.configs = configs;
        this.registry = registry;
    }

    @Override
    public void start() throws Exception {
        validateConfiguration(configs, registry.getNames());
    }

    @Override
    public void stop() throws Exception {
        // do nothing
    }

    private void validateConfiguration(final List healthCheckConfigs,
                                       final Set registeredHealthCheckNames) {
        final Set configuredHealthCheckNames = healthCheckConfigs.stream()
                .map(HealthCheckConfiguration::getName)
                .collect(Collectors.toSet());

        // find health checks that are registered but do not have a configured schedule
        final Set notConfiguredHealthCheckNames = registeredHealthCheckNames.stream()
                .filter(healthCheckName -> !configuredHealthCheckNames.contains(healthCheckName))
                .collect(Collectors.toSet());

        if (!notConfiguredHealthCheckNames.isEmpty()) {
            final String healthCheckList = notConfiguredHealthCheckNames.stream()
                    .map(name -> "  * " + name)
                    .collect(Collectors.joining("\n"));
            log.info("The following health check(s) were registered, but are not configured with a schedule:\n{}",
                    healthCheckList);
        }

        // find health checks that are configured with a schedule but are not actually registered
        final Set notRegisteredHealthCheckNames = configuredHealthCheckNames.stream()
                .filter(healthCheckName -> !registeredHealthCheckNames.contains(healthCheckName))
                .collect(Collectors.toSet());

        if (!notRegisteredHealthCheckNames.isEmpty()) {
            final String healthCheckList = notRegisteredHealthCheckNames.stream()
                    .map(name -> "  * " + name)
                    .collect(Collectors.joining("\n"));
            log.error("The following health check(s) are configured with a schedule, but were not registered:\n{}",
                    healthCheckList);
            throw new IllegalStateException("The following configured health checks were not registered: "
                    + notRegisteredHealthCheckNames);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy