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:
Brian Clozel 2018-12-11 11:48:07 +01:00
parent 1ec2bbf54f
commit 036aace2cc
3 changed files with 49 additions and 32 deletions

View File

@ -16,14 +16,16 @@
package org.springframework.boot.actuate.elasticsearch; package org.springframework.boot.actuate.elasticsearch;
import com.google.gson.JsonElement; import java.util.Map;
import com.google.gson.JsonParser;
import io.searchbox.client.JestClient; import io.searchbox.client.JestClient;
import io.searchbox.client.JestResult; import io.searchbox.client.JestResult;
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;
/** /**
* {@link HealthIndicator} for Elasticsearch using a {@link JestClient}. * {@link HealthIndicator} for Elasticsearch using a {@link JestClient}.
@ -37,7 +39,7 @@ public class ElasticsearchJestHealthIndicator extends AbstractHealthIndicator {
private final JestClient jestClient; private final JestClient jestClient;
private final JsonParser jsonParser = new JsonParser(); private final JsonParser jsonParser = JsonParserFactory.getJsonParser();
public ElasticsearchJestHealthIndicator(JestClient jestClient) { public ElasticsearchJestHealthIndicator(JestClient jestClient) {
super("Elasticsearch health check failed"); super("Elasticsearch health check failed");
@ -50,17 +52,19 @@ public class ElasticsearchJestHealthIndicator extends AbstractHealthIndicator {
.execute(new io.searchbox.cluster.Health.Builder().build()); .execute(new io.searchbox.cluster.Health.Builder().build());
if (healthResult.getResponseCode() != 200 || !healthResult.isSucceeded()) { if (healthResult.getResponseCode() != 200 || !healthResult.isSucceeded()) {
builder.down(); builder.down();
builder.withDetail("statusCode", healthResult.getResponseCode());
} }
else { else {
JsonElement root = this.jsonParser.parse(healthResult.getJsonString()); Map<String, Object> response = this.jsonParser
JsonElement status = root.getAsJsonObject().get("status"); .parseMap(healthResult.getJsonString());
if (status.getAsString() String status = (String) response.get("status");
.equals(io.searchbox.cluster.Health.Status.RED.getKey())) { if (status.equals(io.searchbox.cluster.Health.Status.RED.getKey())) {
builder.outOfService(); builder.outOfService();
} }
else { else {
builder.up(); builder.up();
} }
builder.withDetails(response);
} }
} }

View File

@ -17,6 +17,7 @@
package org.springframework.boot.actuate.elasticsearch; package org.springframework.boot.actuate.elasticsearch;
import java.io.IOException; import java.io.IOException;
import java.util.Map;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
@ -31,6 +32,7 @@ import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.Status; import org.springframework.boot.actuate.health.Status;
import static org.assertj.core.api.Assertions.assertThat; 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.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@ -56,6 +58,16 @@ public class ElasticsearchJestHealthIndicatorTests {
.willReturn(createJestResult(200, true, "green")); .willReturn(createJestResult(200, true, "green"));
Health health = this.healthIndicator.health(); Health health = this.healthIndicator.health();
assertThat(health.getStatus()).isEqualTo(Status.UP); 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") @SuppressWarnings("unchecked")
@ -83,6 +95,7 @@ public class ElasticsearchJestHealthIndicatorTests {
.willReturn(createJestResult(500, false, "")); .willReturn(createJestResult(500, false, ""));
Health health = this.healthIndicator.health(); Health health = this.healthIndicator.health();
assertThat(health.getStatus()).isEqualTo(Status.DOWN); assertThat(health.getStatus()).isEqualTo(Status.DOWN);
assertThat(health.getDetails()).contains(entry("statusCode", 500));
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -92,6 +105,21 @@ public class ElasticsearchJestHealthIndicatorTests {
.willReturn(createJestResult(200, true, "red")); .willReturn(createJestResult(200, true, "red"));
Health health = this.healthIndicator.health(); Health health = this.healthIndicator.health();
assertThat(health.getStatus()).isEqualTo(Status.OUT_OF_SERVICE); 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, private static JestResult createJestResult(int responseCode, boolean succeeded,

View File

@ -18,6 +18,7 @@ package org.springframework.boot.actuate.elasticsearch;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.util.Map;
import org.apache.http.StatusLine; import org.apache.http.StatusLine;
import org.apache.http.entity.BasicHttpEntity; import org.apache.http.entity.BasicHttpEntity;
@ -64,17 +65,7 @@ public class ElasticsearchRestHealthIndicatorTest {
Health health = this.elasticsearchRestHealthIndicator.health(); Health health = this.elasticsearchRestHealthIndicator.health();
assertThat(health.getStatus()).isEqualTo(Status.UP); assertThat(health.getStatus()).isEqualTo(Status.UP);
assertHealthDetailsWithStatus(health.getDetails(), "green");
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));
} }
@Test @Test
@ -93,17 +84,7 @@ public class ElasticsearchRestHealthIndicatorTest {
Health health = this.elasticsearchRestHealthIndicator.health(); Health health = this.elasticsearchRestHealthIndicator.health();
assertThat(health.getStatus()).isEqualTo(Status.UP); assertThat(health.getStatus()).isEqualTo(Status.UP);
assertHealthDetailsWithStatus(health.getDetails(), "yellow");
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));
} }
@Test @Test
@ -119,7 +100,6 @@ public class ElasticsearchRestHealthIndicatorTest {
@Test @Test
public void elasticsearchIsDownByResponseCode() throws IOException { public void elasticsearchIsDownByResponseCode() throws IOException {
Response response = mock(Response.class); Response response = mock(Response.class);
StatusLine statusLine = mock(StatusLine.class); StatusLine statusLine = mock(StatusLine.class);
@ -150,8 +130,13 @@ public class ElasticsearchRestHealthIndicatorTest {
Health health = this.elasticsearchRestHealthIndicator.health(); Health health = this.elasticsearchRestHealthIndicator.health();
assertThat(health.getStatus()).isEqualTo(Status.OUT_OF_SERVICE); assertThat(health.getStatus()).isEqualTo(Status.OUT_OF_SERVICE);
assertThat(health.getDetails()).contains(entry("cluster_name", "elasticsearch"), assertHealthDetailsWithStatus(health.getDetails(), "red");
entry("status", "red"), entry("timed_out", false), }
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("number_of_nodes", 1), entry("number_of_data_nodes", 1),
entry("active_primary_shards", 0), entry("active_shards", 0), entry("active_primary_shards", 0), entry("active_shards", 0),
entry("relocating_shards", 0), entry("initializing_shards", 0), entry("relocating_shards", 0), entry("initializing_shards", 0),