io.micronaut.health.HealthStatus Java Examples

The following examples show how to use io.micronaut.health.HealthStatus. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KafkaStreamsHealth.java    From micronaut-kafka with Apache License 2.0 6 votes vote down vote up
/**
 * Get the health result of the streams.  Will attempt to interrogate
 * details of each stream as well.  The application.id will be used
 * for each configured stream as the primary health check name.
 *
 * @return Health Result Aggregate
 */

@Override
public Publisher<HealthResult> getResult() {
    Flowable<HealthResult> kafkaStreamHealth = Flowable.fromIterable(this.kafkaStreamsFactory.getStreams().keySet())
            .map(kafkaStreams -> Pair.of(getApplicationId(kafkaStreams), kafkaStreams))
            .flatMap(pair -> Flowable.just(pair)
                    .filter(p -> p.getValue().state().isRunningOrRebalancing())
                    .map(p -> HealthResult.builder(p.getKey(), HealthStatus.UP)
                            .details(buildDetails(p.getValue())))
                    .defaultIfEmpty(HealthResult.builder(pair.getKey(), HealthStatus.DOWN)
                            .details(buildDownDetails(pair.getValue().state())))
                    .onErrorReturn(e -> HealthResult.builder(pair.getKey(), HealthStatus.DOWN)
                            .details(buildDownDetails(e.getMessage(), pair.getValue().state()))))
            .map(HealthResult.Builder::build);
    return healthAggregator.aggregate(NAME, kafkaStreamHealth);
}
 
Example #2
Source File: PgHealthIndicator.java    From micronaut-sql with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<HealthResult> getResult() {
    return client.query(QUERY).rxExecute().map(rows -> {
        HealthResult.Builder status = HealthResult.builder(NAME, HealthStatus.UP);
        Row row = rows.iterator().next();
        status.details(Collections.singletonMap("version", row.getString(0)));
        return status.build();
    }).onErrorReturn(this::buildErrorResult).toFlowable();
}
 
Example #3
Source File: JasyncHealthIndicator.java    From micronaut-sql with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<HealthResult> getResult() {
    return Flowable.fromFuture(client.sendQuery(QUERY))
            .map(queryResult -> {
                Map<String, String> error = new HashMap<>(1);
                error.put("version", String.valueOf(queryResult.getRows().get(0).get(0)));
                return HealthResult.builder(NAME, HealthStatus.UP).details(error).build();
            })
            .onErrorReturn(error -> HealthResult.builder(NAME, HealthStatus.DOWN).exception(error).build());
}
 
Example #4
Source File: MySQLHealthIndicator.java    From micronaut-sql with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<HealthResult> getResult() {
    return client.query(QUERY).rxExecute().map(rows -> {
        HealthResult.Builder status = HealthResult.builder(NAME, HealthStatus.UP);
        Row row = rows.iterator().next();
        status.details(Collections.singletonMap("version", row.getString(0)));
        return status.build();
    }).onErrorReturn(this::buildErrorResult).toFlowable();
}
 
Example #5
Source File: Route53AutoNamingRegistrationClient.java    From micronaut-aws with Apache License 2.0 4 votes vote down vote up
/**
 * If custom health check is enabled, this sends a heartbeat to it.
 * In most cases aws monitoring works off polling an application's endpoint
 * @param instance The instance of the service
 * @param status   The {@link HealthStatus}
 */
@Override
protected void pulsate(ServiceInstance instance, HealthStatus status) {
    // this only work if you create a health status check when you register it
    // we can't really pulsate anywhere because amazon health checks work inverse from this UNLESS you have a custom health check
    Optional<String> opt = instance.getInstanceId();
    if (!opt.isPresent()) {
        // try the metadata
        if (instance.getMetadata().contains("instanceId")) {
            opt = Optional.of(instance.getMetadata().asMap().get("instanceId"));
        } else {
            if (LOG.isErrorEnabled()) {
                LOG.error("Cannot determine the instance ID. Are you sure you are running on AWS EC2?");
            }
        }
    }

    opt.ifPresent(instanceId -> {
        if (discoveryService != null && discoveryService.getHealthCheckCustomConfig() != null) {
            CustomHealthStatus customHealthStatus = CustomHealthStatus.UNHEALTHY;

            if (status.getOperational().isPresent()) {
                customHealthStatus = CustomHealthStatus.HEALTHY;
            }

            UpdateInstanceCustomHealthStatusRequest updateInstanceCustomHealthStatusRequest = new UpdateInstanceCustomHealthStatusRequest()
                                                                                                        .withInstanceId(instanceId)
                                                                                                        .withServiceId(route53AutoRegistrationConfiguration.getAwsServiceId())
                                                                                                        .withStatus(customHealthStatus);
            getDiscoveryClient().updateInstanceCustomHealthStatus(
                    updateInstanceCustomHealthStatusRequest);
        }

        if (status.getOperational().isPresent() && !status.getOperational().get()) {
            getDiscoveryClient().deregisterInstance(new DeregisterInstanceRequest().withInstanceId(instanceId).withServiceId(route53AutoRegistrationConfiguration.getAwsServiceId()));
            LOG.info("Health status is non operational, instance id " + instanceId + " was de-registered from the discovery service.");
        }

    });

}
 
Example #6
Source File: EC2ServiceInstance.java    From micronaut-aws with Apache License 2.0 4 votes vote down vote up
/**
 * Gets the current instance health status.
 * @return status
 */
@Override
public HealthStatus getHealthStatus() {
    return healthStatus;
}
 
Example #7
Source File: PgHealthIndicator.java    From micronaut-sql with Apache License 2.0 4 votes vote down vote up
private HealthResult buildErrorResult(Throwable throwable) {
    return HealthResult.builder(NAME, HealthStatus.DOWN).exception(throwable).build();
}
 
Example #8
Source File: MySQLHealthIndicator.java    From micronaut-sql with Apache License 2.0 4 votes vote down vote up
private HealthResult buildErrorResult(Throwable throwable) {
    return HealthResult.builder(NAME, HealthStatus.DOWN).exception(throwable).build();
}
 
Example #9
Source File: KafkaHealthIndicator.java    From micronaut-kafka with Apache License 2.0 4 votes vote down vote up
@Override
public Flowable<HealthResult> getResult() {
    DescribeClusterResult result = adminClient.describeCluster(
            new DescribeClusterOptions().timeoutMs(
                    (int) defaultConfiguration.getHealthTimeout().toMillis()
            )
    );

    Flowable<String> clusterId = Flowable.fromFuture(result.clusterId());
    Flowable<Collection<Node>> nodes = Flowable.fromFuture(result.nodes());
    Flowable<Node> controller = Flowable.fromFuture(result.controller());

    return controller.switchMap(node -> {
        String brokerId = node.idString();
        ConfigResource configResource = new ConfigResource(ConfigResource.Type.BROKER, brokerId);
        DescribeConfigsResult configResult = adminClient.describeConfigs(Collections.singletonList(configResource));
        Flowable<Map<ConfigResource, Config>> configs = Flowable.fromFuture(configResult.all());
        return configs.switchMap(resources -> {
            Config config = resources.get(configResource);
            ConfigEntry ce = config.get(REPLICATION_PROPERTY);
            int replicationFactor = Integer.parseInt(ce.value());
            return nodes.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();
            }));
        });
    }).onErrorReturn(throwable ->
            HealthResult.builder(ID, HealthStatus.DOWN)
                    .exception(throwable).build()
    );
}