Add details in Jest ElasticSearch HealthIndicator
This commit polishes the previous one and aligns the Jest HealthIndicator with the changes made for the REST variant. Closes gh-15366
This commit is contained in:
parent
1ec2bbf54f
commit
036aace2cc
|
|
@ -16,14 +16,16 @@
|
|||
|
||||
package org.springframework.boot.actuate.elasticsearch;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParser;
|
||||
import java.util.Map;
|
||||
|
||||
import io.searchbox.client.JestClient;
|
||||
import io.searchbox.client.JestResult;
|
||||
|
||||
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
|
||||
import org.springframework.boot.actuate.health.Health;
|
||||
import org.springframework.boot.actuate.health.HealthIndicator;
|
||||
import org.springframework.boot.json.JsonParser;
|
||||
import org.springframework.boot.json.JsonParserFactory;
|
||||
|
||||
/**
|
||||
* {@link HealthIndicator} for Elasticsearch using a {@link JestClient}.
|
||||
|
|
@ -37,7 +39,7 @@ public class ElasticsearchJestHealthIndicator extends AbstractHealthIndicator {
|
|||
|
||||
private final JestClient jestClient;
|
||||
|
||||
private final JsonParser jsonParser = new JsonParser();
|
||||
private final JsonParser jsonParser = JsonParserFactory.getJsonParser();
|
||||
|
||||
public ElasticsearchJestHealthIndicator(JestClient jestClient) {
|
||||
super("Elasticsearch health check failed");
|
||||
|
|
@ -50,17 +52,19 @@ public class ElasticsearchJestHealthIndicator extends AbstractHealthIndicator {
|
|||
.execute(new io.searchbox.cluster.Health.Builder().build());
|
||||
if (healthResult.getResponseCode() != 200 || !healthResult.isSucceeded()) {
|
||||
builder.down();
|
||||
builder.withDetail("statusCode", healthResult.getResponseCode());
|
||||
}
|
||||
else {
|
||||
JsonElement root = this.jsonParser.parse(healthResult.getJsonString());
|
||||
JsonElement status = root.getAsJsonObject().get("status");
|
||||
if (status.getAsString()
|
||||
.equals(io.searchbox.cluster.Health.Status.RED.getKey())) {
|
||||
Map<String, Object> response = this.jsonParser
|
||||
.parseMap(healthResult.getJsonString());
|
||||
String status = (String) response.get("status");
|
||||
if (status.equals(io.searchbox.cluster.Health.Status.RED.getKey())) {
|
||||
builder.outOfService();
|
||||
}
|
||||
else {
|
||||
builder.up();
|
||||
}
|
||||
builder.withDetails(response);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@
|
|||
package org.springframework.boot.actuate.elasticsearch;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonParser;
|
||||
|
|
@ -31,6 +32,7 @@ import org.springframework.boot.actuate.health.Health;
|
|||
import org.springframework.boot.actuate.health.Status;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.entry;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
|
@ -56,6 +58,16 @@ public class ElasticsearchJestHealthIndicatorTests {
|
|||
.willReturn(createJestResult(200, true, "green"));
|
||||
Health health = this.healthIndicator.health();
|
||||
assertThat(health.getStatus()).isEqualTo(Status.UP);
|
||||
assertHealthDetailsWithStatus(health.getDetails(), "green");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void elasticsearchWithYellowStatusIsUp() throws IOException {
|
||||
given(this.jestClient.execute(any(Action.class)))
|
||||
.willReturn(createJestResult(200, true, "yellow"));
|
||||
Health health = this.healthIndicator.health();
|
||||
assertThat(health.getStatus()).isEqualTo(Status.UP);
|
||||
assertHealthDetailsWithStatus(health.getDetails(), "yellow");
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
|
@ -83,6 +95,7 @@ public class ElasticsearchJestHealthIndicatorTests {
|
|||
.willReturn(createJestResult(500, false, ""));
|
||||
Health health = this.healthIndicator.health();
|
||||
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
|
||||
assertThat(health.getDetails()).contains(entry("statusCode", 500));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
|
@ -92,6 +105,21 @@ public class ElasticsearchJestHealthIndicatorTests {
|
|||
.willReturn(createJestResult(200, true, "red"));
|
||||
Health health = this.healthIndicator.health();
|
||||
assertThat(health.getStatus()).isEqualTo(Status.OUT_OF_SERVICE);
|
||||
assertHealthDetailsWithStatus(health.getDetails(), "red");
|
||||
}
|
||||
|
||||
private void assertHealthDetailsWithStatus(Map<String, Object> details,
|
||||
String status) {
|
||||
assertThat(details).contains(entry("cluster_name", "elasticsearch"),
|
||||
entry("status", status), entry("timed_out", false),
|
||||
entry("number_of_nodes", 1), entry("number_of_data_nodes", 1),
|
||||
entry("active_primary_shards", 0), entry("active_shards", 0),
|
||||
entry("relocating_shards", 0), entry("initializing_shards", 0),
|
||||
entry("unassigned_shards", 0), entry("delayed_unassigned_shards", 0),
|
||||
entry("number_of_pending_tasks", 0),
|
||||
entry("number_of_in_flight_fetch", 0),
|
||||
entry("task_max_waiting_in_queue_millis", 0),
|
||||
entry("active_shards_percent_as_number", 100.0));
|
||||
}
|
||||
|
||||
private static JestResult createJestResult(int responseCode, boolean succeeded,
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ package org.springframework.boot.actuate.elasticsearch;
|
|||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.http.StatusLine;
|
||||
import org.apache.http.entity.BasicHttpEntity;
|
||||
|
|
@ -64,17 +65,7 @@ public class ElasticsearchRestHealthIndicatorTest {
|
|||
|
||||
Health health = this.elasticsearchRestHealthIndicator.health();
|
||||
assertThat(health.getStatus()).isEqualTo(Status.UP);
|
||||
|
||||
assertThat(health.getDetails()).contains(entry("cluster_name", "elasticsearch"),
|
||||
entry("status", "green"), entry("timed_out", false),
|
||||
entry("number_of_nodes", 1), entry("number_of_data_nodes", 1),
|
||||
entry("active_primary_shards", 0), entry("active_shards", 0),
|
||||
entry("relocating_shards", 0), entry("initializing_shards", 0),
|
||||
entry("unassigned_shards", 0), entry("delayed_unassigned_shards", 0),
|
||||
entry("number_of_pending_tasks", 0),
|
||||
entry("number_of_in_flight_fetch", 0),
|
||||
entry("task_max_waiting_in_queue_millis", 0),
|
||||
entry("active_shards_percent_as_number", 100.0));
|
||||
assertHealthDetailsWithStatus(health.getDetails(), "green");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -93,17 +84,7 @@ public class ElasticsearchRestHealthIndicatorTest {
|
|||
|
||||
Health health = this.elasticsearchRestHealthIndicator.health();
|
||||
assertThat(health.getStatus()).isEqualTo(Status.UP);
|
||||
|
||||
assertThat(health.getDetails()).contains(entry("cluster_name", "elasticsearch"),
|
||||
entry("status", "yellow"), entry("timed_out", false),
|
||||
entry("number_of_nodes", 1), entry("number_of_data_nodes", 1),
|
||||
entry("active_primary_shards", 0), entry("active_shards", 0),
|
||||
entry("relocating_shards", 0), entry("initializing_shards", 0),
|
||||
entry("unassigned_shards", 0), entry("delayed_unassigned_shards", 0),
|
||||
entry("number_of_pending_tasks", 0),
|
||||
entry("number_of_in_flight_fetch", 0),
|
||||
entry("task_max_waiting_in_queue_millis", 0),
|
||||
entry("active_shards_percent_as_number", 100.0));
|
||||
assertHealthDetailsWithStatus(health.getDetails(), "yellow");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -119,7 +100,6 @@ public class ElasticsearchRestHealthIndicatorTest {
|
|||
|
||||
@Test
|
||||
public void elasticsearchIsDownByResponseCode() throws IOException {
|
||||
|
||||
Response response = mock(Response.class);
|
||||
StatusLine statusLine = mock(StatusLine.class);
|
||||
|
||||
|
|
@ -150,8 +130,13 @@ public class ElasticsearchRestHealthIndicatorTest {
|
|||
|
||||
Health health = this.elasticsearchRestHealthIndicator.health();
|
||||
assertThat(health.getStatus()).isEqualTo(Status.OUT_OF_SERVICE);
|
||||
assertThat(health.getDetails()).contains(entry("cluster_name", "elasticsearch"),
|
||||
entry("status", "red"), entry("timed_out", false),
|
||||
assertHealthDetailsWithStatus(health.getDetails(), "red");
|
||||
}
|
||||
|
||||
private void assertHealthDetailsWithStatus(Map<String, Object> details,
|
||||
String status) {
|
||||
assertThat(details).contains(entry("cluster_name", "elasticsearch"),
|
||||
entry("status", status), entry("timed_out", false),
|
||||
entry("number_of_nodes", 1), entry("number_of_data_nodes", 1),
|
||||
entry("active_primary_shards", 0), entry("active_shards", 0),
|
||||
entry("relocating_shards", 0), entry("initializing_shards", 0),
|
||||
|
|
|
|||
Loading…
Reference in New Issue