Search in sources :

Example 6 with HealthResult

use of io.micronaut.management.health.indicator.HealthResult in project micronaut-kafka by micronaut-projects.

the class KafkaHealthIndicator method getResult.

@Override
public Flux<HealthResult> getResult() {
    DescribeClusterResult result = adminClient.describeCluster(new DescribeClusterOptions().timeoutMs((int) defaultConfiguration.getHealthTimeout().toMillis()));
    Mono<String> clusterId = KafkaReactorUtil.fromKafkaFuture(result::clusterId);
    Mono<Collection<Node>> nodes = KafkaReactorUtil.fromKafkaFuture(result::nodes);
    Mono<Node> controller = KafkaReactorUtil.fromKafkaFuture(result::controller);
    return controller.flux().switchMap(node -> {
        String brokerId = node.idString();
        ConfigResource configResource = new ConfigResource(ConfigResource.Type.BROKER, brokerId);
        DescribeConfigsResult configResult = adminClient.describeConfigs(Collections.singletonList(configResource));
        Mono<Map<ConfigResource, Config>> configs = KafkaReactorUtil.fromKafkaFuture(configResult::all);
        return configs.flux().switchMap(resources -> {
            Config config = resources.get(configResource);
            int replicationFactor = getClusterReplicationFactor(config);
            return nodes.flux().switchMap(nodeList -> clusterId.map(clusterIdString -> {
                int nodeCount = nodeList.size();
                HealthResult.Builder builder;
                if (nodeCount >= replicationFactor) {
                    builder = HealthResult.builder(ID, HealthStatus.UP);
                } else {
                    builder = HealthResult.builder(ID, HealthStatus.DOWN);
                }
                return builder.details(CollectionUtils.mapOf("brokerId", brokerId, "clusterId", clusterIdString, "nodes", nodeCount)).build();
            }));
        });
    }).onErrorResume(throwable -> Mono.just(HealthResult.builder(ID, HealthStatus.DOWN).exception(throwable).build()));
}
Also used : Config(org.apache.kafka.clients.admin.Config) DescribeConfigsResult(org.apache.kafka.clients.admin.DescribeConfigsResult) Collection(java.util.Collection) AbstractKafkaConfiguration(io.micronaut.configuration.kafka.config.AbstractKafkaConfiguration) KafkaDefaultConfiguration(io.micronaut.configuration.kafka.config.KafkaDefaultConfiguration) Singleton(jakarta.inject.Singleton) HealthIndicator(io.micronaut.management.health.indicator.HealthIndicator) Mono(reactor.core.publisher.Mono) ConfigEntry(org.apache.kafka.clients.admin.ConfigEntry) HealthStatus(io.micronaut.health.HealthStatus) AdminClient(org.apache.kafka.clients.admin.AdminClient) ConfigResource(org.apache.kafka.common.config.ConfigResource) Flux(reactor.core.publisher.Flux) KafkaReactorUtil(io.micronaut.configuration.kafka.reactor.KafkaReactorUtil) CollectionUtils(io.micronaut.core.util.CollectionUtils) Map(java.util.Map) Requires(io.micronaut.context.annotation.Requires) DescribeClusterOptions(org.apache.kafka.clients.admin.DescribeClusterOptions) Optional(java.util.Optional) Node(org.apache.kafka.common.Node) DescribeClusterResult(org.apache.kafka.clients.admin.DescribeClusterResult) HealthResult(io.micronaut.management.health.indicator.HealthResult) Collections(java.util.Collections) DescribeClusterOptions(org.apache.kafka.clients.admin.DescribeClusterOptions) Config(org.apache.kafka.clients.admin.Config) Node(org.apache.kafka.common.Node) Mono(reactor.core.publisher.Mono) DescribeConfigsResult(org.apache.kafka.clients.admin.DescribeConfigsResult) ConfigResource(org.apache.kafka.common.config.ConfigResource) DescribeClusterResult(org.apache.kafka.clients.admin.DescribeClusterResult) Collection(java.util.Collection) HealthResult(io.micronaut.management.health.indicator.HealthResult)

Example 7 with HealthResult

use of io.micronaut.management.health.indicator.HealthResult in project micronaut-core by micronaut-projects.

the class HealthMonitorTask method monitor.

/**
 * Start the continuous health monitor.
 */
@Scheduled(fixedDelay = "${micronaut.health.monitor.interval:1m}", initialDelay = "${micronaut.health.monitor.initial-delay:1m}")
void monitor() {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Starting health monitor check");
    }
    List<Publisher<HealthResult>> healthResults = healthIndicators.stream().map(HealthIndicator::getResult).collect(Collectors.toList());
    Flux<HealthResult> reactiveSequence = Flux.merge(healthResults).filter(healthResult -> {
        HealthStatus status = healthResult.getStatus();
        return status.equals(HealthStatus.DOWN) || !status.getOperational().orElse(true);
    });
    reactiveSequence.next().subscribe(new Subscriber<HealthResult>() {

        @Override
        public void onSubscribe(Subscription s) {
        }

        @Override
        public void onNext(HealthResult healthResult) {
            HealthStatus status = healthResult.getStatus();
            if (LOG.isDebugEnabled()) {
                LOG.debug("Health monitor check failed with status {}", status);
            }
            currentHealthStatus.update(status);
        }

        @Override
        public void onError(Throwable e) {
            if (LOG.isErrorEnabled()) {
                LOG.error("Health monitor check failed with exception: " + e.getMessage(), e);
            }
            currentHealthStatus.update(HealthStatus.DOWN.describe("Error occurred running health check: " + e.getMessage()));
        }

        @Override
        public void onComplete() {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Health monitor check passed.");
            }
            currentHealthStatus.update(HealthStatus.UP);
        }
    });
}
Also used : HealthStatus(io.micronaut.health.HealthStatus) CurrentHealthStatus(io.micronaut.health.CurrentHealthStatus) Publisher(org.reactivestreams.Publisher) Subscription(org.reactivestreams.Subscription) HealthResult(io.micronaut.management.health.indicator.HealthResult) Scheduled(io.micronaut.scheduling.annotation.Scheduled)

Example 8 with HealthResult

use of io.micronaut.management.health.indicator.HealthResult in project micronaut-core by micronaut-projects.

the class DefaultHealthAggregator method aggregate.

@Override
public Publisher<HealthResult> aggregate(String name, Publisher<HealthResult> results) {
    Mono<HealthResult> result = Flux.from(results).collectList().map(list -> {
        HealthStatus overallStatus = calculateOverallStatus(list);
        Object details = aggregateDetails(list);
        return HealthResult.builder(name, overallStatus).details(details).build();
    });
    return result.flux();
}
Also used : HealthStatus(io.micronaut.health.HealthStatus) HealthResult(io.micronaut.management.health.indicator.HealthResult)

Example 9 with HealthResult

use of io.micronaut.management.health.indicator.HealthResult in project micronaut-core by micronaut-projects.

the class DefaultHealthAggregator method aggregate.

@Override
public Publisher<HealthResult> aggregate(HealthIndicator[] indicators, HealthLevelOfDetail healthLevelOfDetail) {
    Flux<HealthResult> results = aggregateResults(indicators);
    Mono<HealthResult> result = results.collectList().map(list -> {
        HealthStatus overallStatus = calculateOverallStatus(list);
        return buildResult(overallStatus, aggregateDetails(list), healthLevelOfDetail);
    });
    return result.flux();
}
Also used : HealthStatus(io.micronaut.health.HealthStatus) HealthResult(io.micronaut.management.health.indicator.HealthResult)

Example 10 with HealthResult

use of io.micronaut.management.health.indicator.HealthResult in project micronaut-elasticsearch by micronaut-projects.

the class ElasticsearchHealthIndicator method getResult.

/**
 * Tries to call the cluster info API on Elasticsearch to obtain information about the cluster. If the call succeeds, the Elasticsearch cluster
 * health status (GREEN / YELLOW / RED) will be included in the health indicator details.
 *
 * @return A positive health result UP if the cluster can be communicated with and is in either GREEN or YELLOW status. A negative health result
 * DOWN if the cluster cannot be communicated with or is in RED status.
 */
@Override
public Publisher<HealthResult> getResult() {
    return (subscriber -> esClient.cluster().healthAsync(new ClusterHealthRequest(), RequestOptions.DEFAULT, new ActionListener<ClusterHealthResponse>() {

        private final HealthResult.Builder resultBuilder = HealthResult.builder(NAME);

        @Override
        public void onResponse(ClusterHealthResponse response) {
            HealthResult result;
            try {
                result = resultBuilder.status(healthResultStatus(response)).details(healthResultDetails(response)).build();
            } catch (IOException e) {
                result = resultBuilder.status(DOWN).exception(e).build();
            }
            subscriber.onNext(result);
            subscriber.onComplete();
        }

        @Override
        public void onFailure(Exception e) {
            subscriber.onNext(resultBuilder.status(DOWN).exception(e).build());
            subscriber.onComplete();
        }
    }));
}
Also used : ActionListener(org.elasticsearch.action.ActionListener) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) ClusterHealthRequest(org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest) XContentBuilder(org.elasticsearch.xcontent.XContentBuilder) IOException(java.io.IOException) IOException(java.io.IOException) HealthResult(io.micronaut.management.health.indicator.HealthResult)

Aggregations

HealthResult (io.micronaut.management.health.indicator.HealthResult)12 HealthStatus (io.micronaut.health.HealthStatus)8 Requires (io.micronaut.context.annotation.Requires)3 HealthIndicator (io.micronaut.management.health.indicator.HealthIndicator)3 Singleton (jakarta.inject.Singleton)3 Publisher (org.reactivestreams.Publisher)3 Row (io.vertx.reactivex.sqlclient.Row)2 IOException (java.io.IOException)2 Collections (java.util.Collections)2 Flux (reactor.core.publisher.Flux)2 BasicDBObject (com.mongodb.BasicDBObject)1 MongoClient (com.mongodb.reactivestreams.client.MongoClient)1 AbstractKafkaConfiguration (io.micronaut.configuration.kafka.config.AbstractKafkaConfiguration)1 KafkaDefaultConfiguration (io.micronaut.configuration.kafka.config.KafkaDefaultConfiguration)1 KafkaReactorUtil (io.micronaut.configuration.kafka.reactor.KafkaReactorUtil)1 BeanContext (io.micronaut.context.BeanContext)1 BeanRegistration (io.micronaut.context.BeanRegistration)1 AsyncSingleResultPublisher (io.micronaut.core.async.publisher.AsyncSingleResultPublisher)1 CollectionUtils (io.micronaut.core.util.CollectionUtils)1 CurrentHealthStatus (io.micronaut.health.CurrentHealthStatus)1