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()));
}
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);
}
});
}
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();
}
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();
}
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();
}
}));
}
Aggregations