|
|
@ -16,11 +16,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
package org.springframework.boot.actuate.elasticsearch;
|
|
|
|
package org.springframework.boot.actuate.elasticsearch;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
import com.google.gson.JsonElement;
|
|
|
|
|
|
|
|
import com.google.gson.JsonParser;
|
|
|
|
|
|
|
|
import org.apache.http.HttpStatus;
|
|
|
|
import org.apache.http.HttpStatus;
|
|
|
|
import org.elasticsearch.client.Request;
|
|
|
|
import org.elasticsearch.client.Request;
|
|
|
|
import org.elasticsearch.client.Response;
|
|
|
|
import org.elasticsearch.client.Response;
|
|
|
@ -29,22 +28,29 @@ import org.elasticsearch.client.RestClient;
|
|
|
|
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
|
|
|
|
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
|
|
|
|
import org.springframework.boot.actuate.health.Health;
|
|
|
|
import org.springframework.boot.actuate.health.Health;
|
|
|
|
import org.springframework.boot.actuate.health.HealthIndicator;
|
|
|
|
import org.springframework.boot.actuate.health.HealthIndicator;
|
|
|
|
|
|
|
|
import org.springframework.boot.json.JsonParser;
|
|
|
|
|
|
|
|
import org.springframework.boot.json.JsonParserFactory;
|
|
|
|
|
|
|
|
import org.springframework.util.StreamUtils;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* {@link HealthIndicator} for an Elasticsearch cluster by REST.
|
|
|
|
* {@link HealthIndicator} for an Elasticsearch cluster using a {@link RestClient}.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @author Artsiom Yudovin
|
|
|
|
* @author Artsiom Yudovin
|
|
|
|
* @since 2.1.0
|
|
|
|
* @author Brian Clozel
|
|
|
|
|
|
|
|
* @since 2.1.1
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public class ElasticsearchRestHealthIndicator extends AbstractHealthIndicator {
|
|
|
|
public class ElasticsearchRestHealthIndicator extends AbstractHealthIndicator {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final String RED_STATUS = "red";
|
|
|
|
|
|
|
|
|
|
|
|
private final RestClient client;
|
|
|
|
private final RestClient client;
|
|
|
|
|
|
|
|
|
|
|
|
private final JsonParser jsonParser = new JsonParser();
|
|
|
|
private final JsonParser jsonParser;
|
|
|
|
|
|
|
|
|
|
|
|
public ElasticsearchRestHealthIndicator(RestClient client) {
|
|
|
|
public ElasticsearchRestHealthIndicator(RestClient client) {
|
|
|
|
super("Elasticsearch health check failed");
|
|
|
|
super("Elasticsearch health check failed");
|
|
|
|
this.client = client;
|
|
|
|
this.client = client;
|
|
|
|
|
|
|
|
this.jsonParser = JsonParserFactory.getJsonParser();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
@ -56,12 +62,11 @@ public class ElasticsearchRestHealthIndicator extends AbstractHealthIndicator {
|
|
|
|
builder.down();
|
|
|
|
builder.down();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
else {
|
|
|
|
try (InputStreamReader reader = new InputStreamReader(
|
|
|
|
try (InputStream is = response.getEntity().getContent()) {
|
|
|
|
response.getEntity().getContent(), StandardCharsets.UTF_8)) {
|
|
|
|
Map<String, Object> root = this.jsonParser
|
|
|
|
JsonElement root = this.jsonParser.parse(reader);
|
|
|
|
.parseMap(StreamUtils.copyToString(is, StandardCharsets.UTF_8));
|
|
|
|
JsonElement status = root.getAsJsonObject().get("status");
|
|
|
|
String status = (String) root.get("status");
|
|
|
|
if (status.getAsString()
|
|
|
|
if (status.equals(RED_STATUS)) {
|
|
|
|
.equals(io.searchbox.cluster.Health.Status.RED.getKey())) {
|
|
|
|
|
|
|
|
builder.outOfService();
|
|
|
|
builder.outOfService();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
else {
|
|
|
|