Polish support for reactive Elasticsearch healthcheck
Fixes gh-21042
This commit is contained in:
@@ -16,24 +16,31 @@
|
||||
|
||||
package org.springframework.boot.actuate.elasticsearch;
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.Map;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.boot.actuate.health.AbstractReactiveHealthIndicator;
|
||||
import org.springframework.boot.actuate.health.Health;
|
||||
import org.springframework.boot.actuate.health.HealthIndicator;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient;
|
||||
|
||||
/**
|
||||
* {@link HealthIndicator} for an Elasticsearch cluster using a
|
||||
* {@link ReactiveElasticsearchClient}.
|
||||
*
|
||||
* @author Brian Clozel
|
||||
* @author Aleksander Lech
|
||||
* @since 2.3
|
||||
* @since 2.3.2
|
||||
*/
|
||||
public class ElasticsearchReactiveHealthIndicator extends AbstractReactiveHealthIndicator {
|
||||
|
||||
private static final ParameterizedTypeReference<Map<String, Object>> STRING_OBJECT_MAP = new ParameterizedTypeReference<Map<String, Object>>() {
|
||||
};
|
||||
|
||||
private static final String RED_STATUS = "red";
|
||||
|
||||
private final ReactiveElasticsearchClient client;
|
||||
|
||||
public ElasticsearchReactiveHealthIndicator(ReactiveElasticsearchClient client) {
|
||||
@@ -43,19 +50,28 @@ public class ElasticsearchReactiveHealthIndicator extends AbstractReactiveHealth
|
||||
|
||||
@Override
|
||||
protected Mono<Health> doHealthCheck(Health.Builder builder) {
|
||||
return this.client.status().map((status) -> {
|
||||
if (status.isOk()) {
|
||||
builder.up();
|
||||
}
|
||||
else {
|
||||
builder.down();
|
||||
}
|
||||
|
||||
builder.withDetails(status.hosts().stream().collect(Collectors
|
||||
.toMap((host) -> host.getEndpoint().getHostString(), (host) -> host.getState().toString())));
|
||||
|
||||
return builder.build();
|
||||
});
|
||||
return this.client.execute((callback) -> callback.get().uri("/_cluster/health/").exchange())
|
||||
.flatMap((response) -> {
|
||||
if (response.statusCode().is2xxSuccessful()) {
|
||||
return response.bodyToMono(STRING_OBJECT_MAP).map((body) -> {
|
||||
String status = (String) body.get("status");
|
||||
if (RED_STATUS.equals(status)) {
|
||||
builder.outOfService();
|
||||
}
|
||||
else {
|
||||
builder.up();
|
||||
}
|
||||
builder.withDetails(body);
|
||||
return builder.build();
|
||||
});
|
||||
}
|
||||
else {
|
||||
builder.down();
|
||||
builder.withDetail("statusCode", response.rawStatusCode());
|
||||
builder.withDetail("reasonPhrase", response.statusCode().getReasonPhrase());
|
||||
return response.releaseBody().thenReturn(builder.build());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user