Search in sources :

Example 1 with HealthStatus

use of io.micronaut.health.HealthStatus in project micronaut-core by micronaut-projects.

the class HealthResultFilter method doFilterOnce.

@Override
protected Publisher<MutableHttpResponse<?>> doFilterOnce(HttpRequest<?> request, ServerFilterChain chain) {
    return Publishers.map(chain.proceed(request), response -> {
        Object body = response.body();
        if (body instanceof HealthResult) {
            HealthResult healthResult = (HealthResult) body;
            HealthStatus status = healthResult.getStatus();
            HttpStatus httpStatus = healthEndpoint.getStatusConfiguration().getHttpMapping().get(status.getName());
            if (httpStatus != null) {
                response.status(httpStatus);
            } else {
                boolean operational = status.getOperational().orElse(true);
                if (!operational) {
                    response.status(HttpStatus.SERVICE_UNAVAILABLE);
                }
            }
        }
        return response;
    });
}
Also used : HttpStatus(io.micronaut.http.HttpStatus) HealthStatus(io.micronaut.health.HealthStatus) HealthResult(io.micronaut.management.health.indicator.HealthResult)

Example 2 with HealthStatus

use of io.micronaut.health.HealthStatus in project micronaut-grpc by micronaut-projects.

the class GrpcServerHealthIndicator method getHealthResult.

/**
 * Checks if grpc is running and return status UP otherwise return status DOWN.
 *
 * @return Result with server address in the details and status UP or DOWN.
 */
private HealthResult getHealthResult() {
    final HealthStatus healthStatus = server.isRunning() ? HealthStatus.UP : HealthStatus.DOWN;
    /**
     * BUGFIX: it avoids to call the server.getPort() method when the gRPC-Server is DOWN because
     * it throws an unexpected exception that breaks the /health endpoint
     */
    final String serverHost = server.getHost();
    // don't call the server.getPort() here!
    final int serverPort = server.getServerConfiguration().getServerPort();
    final Map details = mapOf("host", serverHost, "port", serverPort);
    return HealthResult.builder(ID, healthStatus).details(details).build();
}
Also used : HealthStatus(io.micronaut.health.HealthStatus) Map(java.util.Map) HealthEndpoint(io.micronaut.management.endpoint.health.HealthEndpoint)

Example 3 with HealthStatus

use of io.micronaut.health.HealthStatus in project micronaut-elasticsearch by micronaut-projects.

the class ElasticsearchClientHealthIndicator 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 -> {
        final HealthResult.Builder resultBuilder = HealthResult.builder(NAME);
        try {
            client.cluster().health().handle((health, exception) -> {
                if (exception != null) {
                    subscriber.onNext(resultBuilder.status(DOWN).exception(exception).build());
                    subscriber.onComplete();
                } else {
                    HealthStatus status = health.status() == co.elastic.clients.elasticsearch._types.HealthStatus.Red ? DOWN : UP;
                    subscriber.onNext(resultBuilder.status(status).details(health).build());
                    subscriber.onComplete();
                }
                return health;
            });
        } catch (IOException e) {
            subscriber.onNext(resultBuilder.status(DOWN).exception(e).build());
            subscriber.onComplete();
        }
    });
}
Also used : HealthStatus(io.micronaut.health.HealthStatus) IOException(java.io.IOException) HealthResult(io.micronaut.management.health.indicator.HealthResult)

Example 4 with HealthStatus

use of io.micronaut.health.HealthStatus 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 5 with HealthStatus

use of io.micronaut.health.HealthStatus 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)

Aggregations

HealthStatus (io.micronaut.health.HealthStatus)6 HealthResult (io.micronaut.management.health.indicator.HealthResult)5 CurrentHealthStatus (io.micronaut.health.CurrentHealthStatus)1 HttpStatus (io.micronaut.http.HttpStatus)1 HealthEndpoint (io.micronaut.management.endpoint.health.HealthEndpoint)1 Scheduled (io.micronaut.scheduling.annotation.Scheduled)1 IOException (java.io.IOException)1 Map (java.util.Map)1 Publisher (org.reactivestreams.Publisher)1 Subscription (org.reactivestreams.Subscription)1