Skip to content

Commit

Permalink
Fix Elastic health indicator without RestHighLevelClient
Browse files Browse the repository at this point in the history
Closes gh-28496
  • Loading branch information
wilkinsona committed Apr 13, 2022
1 parent f96efa7 commit 27a936e
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 80 deletions.
Expand Up @@ -22,7 +22,7 @@

import org.springframework.boot.actuate.autoconfigure.health.CompositeHealthContributorConfiguration;
import org.springframework.boot.actuate.autoconfigure.health.ConditionalOnEnabledHealthIndicator;
import org.springframework.boot.actuate.elasticsearch.ElasticsearchRestHealthIndicator;
import org.springframework.boot.actuate.elasticsearch.ElasticsearchRestClientHealthIndicator;
import org.springframework.boot.actuate.health.HealthContributor;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
Expand All @@ -34,7 +34,7 @@

/**
* {@link EnableAutoConfiguration Auto-configuration} for
* {@link ElasticsearchRestHealthIndicator} using the {@link RestClient}.
* {@link ElasticsearchRestClientHealthIndicator}.
*
* @author Artsiom Yudovin
* @since 2.1.1
Expand All @@ -44,7 +44,7 @@
@ConditionalOnBean(RestClient.class)
@ConditionalOnEnabledHealthIndicator("elasticsearch")
public class ElasticSearchRestHealthContributorAutoConfiguration
extends CompositeHealthContributorConfiguration<ElasticsearchRestHealthIndicator, RestClient> {
extends CompositeHealthContributorConfiguration<ElasticsearchRestClientHealthIndicator, RestClient> {

@Bean
@ConditionalOnMissingBean(name = { "elasticsearchHealthIndicator", "elasticsearchHealthContributor" })
Expand Down
Expand Up @@ -21,7 +21,7 @@
import org.junit.jupiter.api.Test;

import org.springframework.boot.actuate.autoconfigure.health.HealthContributorAutoConfiguration;
import org.springframework.boot.actuate.elasticsearch.ElasticsearchRestHealthIndicator;
import org.springframework.boot.actuate.elasticsearch.ElasticsearchRestClientHealthIndicator;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientAutoConfiguration;
import org.springframework.boot.test.context.FilteredClassLoader;
Expand All @@ -46,8 +46,8 @@ class ElasticSearchRestHealthContributorAutoConfigurationTests {

@Test
void runShouldCreateIndicator() {
this.contextRunner.run((context) -> assertThat(context).hasSingleBean(ElasticsearchRestHealthIndicator.class)
.hasBean("elasticsearchHealthContributor"));
this.contextRunner.run((context) -> assertThat(context)
.hasSingleBean(ElasticsearchRestClientHealthIndicator.class).hasBean("elasticsearchHealthContributor"));
}

@Test
Expand All @@ -56,29 +56,28 @@ void runWithoutRestHighLevelClientAndWithoutRestClientShouldNotCreateIndicator()
this.contextRunner
.withClassLoader(
new FilteredClassLoader(org.elasticsearch.client.RestHighLevelClient.class, RestClient.class))
.run((context) -> assertThat(context).doesNotHaveBean(ElasticsearchRestHealthIndicator.class)
.run((context) -> assertThat(context).doesNotHaveBean(ElasticsearchRestClientHealthIndicator.class)
.doesNotHaveBean("elasticsearchHealthContributor"));
}

@Test
void runWithoutRestHighLevelClientAndWithRestClientShouldCreateIndicator() {
this.contextRunner.withUserConfiguration(CustomRestClientConfiguration.class)
.run((context) -> assertThat(context).hasSingleBean(ElasticsearchRestHealthIndicator.class)
.hasSingleBean(ElasticsearchRestHealthIndicator.class)
.run((context) -> assertThat(context).hasSingleBean(ElasticsearchRestClientHealthIndicator.class)
.hasBean("elasticsearchHealthContributor"));
}

@Test
void runWithRestHighLevelClientAndWithRestClientShouldCreateIndicator() {
this.contextRunner.withUserConfiguration(CustomRestHighClientConfiguration.class)
.run((context) -> assertThat(context).hasSingleBean(ElasticsearchRestHealthIndicator.class)
.run((context) -> assertThat(context).hasSingleBean(ElasticsearchRestClientHealthIndicator.class)
.hasBean("elasticsearchHealthContributor"));
}

@Test
void runWhenDisabledShouldNotCreateIndicator() {
this.contextRunner.withPropertyValues("management.health.elasticsearch.enabled:false")
.run((context) -> assertThat(context).doesNotHaveBean(ElasticsearchRestHealthIndicator.class)
.run((context) -> assertThat(context).doesNotHaveBean(ElasticsearchRestClientHealthIndicator.class)
.doesNotHaveBean("elasticsearchHealthContributor"));
}

Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,7 +20,7 @@

import org.springframework.boot.actuate.autoconfigure.health.HealthContributorAutoConfiguration;
import org.springframework.boot.actuate.elasticsearch.ElasticsearchReactiveHealthIndicator;
import org.springframework.boot.actuate.elasticsearch.ElasticsearchRestHealthIndicator;
import org.springframework.boot.actuate.elasticsearch.ElasticsearchRestClientHealthIndicator;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration;
import org.springframework.boot.autoconfigure.data.elasticsearch.ReactiveElasticsearchRestClientAutoConfiguration;
Expand Down Expand Up @@ -55,7 +55,7 @@ void runWithRegularIndicatorShouldOnlyCreateReactiveIndicator() {
.withConfiguration(AutoConfigurations.of(ElasticSearchRestHealthContributorAutoConfiguration.class))
.run((context) -> assertThat(context).hasSingleBean(ElasticsearchReactiveHealthIndicator.class)
.hasBean("elasticsearchHealthContributor")
.doesNotHaveBean(ElasticsearchRestHealthIndicator.class));
.doesNotHaveBean(ElasticsearchRestClientHealthIndicator.class));
}

@Test
Expand Down
@@ -0,0 +1,85 @@
/*
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.actuate.elasticsearch;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Map;

import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;

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;
import org.springframework.util.StreamUtils;

/**
* {@link HealthIndicator} for an Elasticsearch cluster using a {@link RestClient}.
*
* @author Artsiom Yudovin
* @author Brian Clozel
* @author Filip Hrisafov
* @since 2.7.0
*/
public class ElasticsearchRestClientHealthIndicator extends AbstractHealthIndicator {

private static final String RED_STATUS = "red";

private final RestClient client;

private final JsonParser jsonParser;

public ElasticsearchRestClientHealthIndicator(RestClient client) {
super("Elasticsearch health check failed");
this.client = client;
this.jsonParser = JsonParserFactory.getJsonParser();
}

@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
Response response = this.client.performRequest(new Request("GET", "/_cluster/health/"));
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() != HttpStatus.SC_OK) {
builder.down();
builder.withDetail("statusCode", statusLine.getStatusCode());
builder.withDetail("reasonPhrase", statusLine.getReasonPhrase());
return;
}
try (InputStream inputStream = response.getEntity().getContent()) {
doHealthCheck(builder, StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8));
}
}

private void doHealthCheck(Health.Builder builder, String json) {
Map<String, Object> response = this.jsonParser.parseMap(json);
String status = (String) response.get("status");
if (RED_STATUS.equals(status)) {
builder.outOfService();
}
else {
builder.up();
}
builder.withDetails(response);
}

}
Expand Up @@ -16,22 +16,9 @@

package org.springframework.boot.actuate.elasticsearch;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Map;

import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;

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;
import org.springframework.util.StreamUtils;

/**
* {@link HealthIndicator} for an Elasticsearch cluster using a {@link RestClient}.
Expand All @@ -40,58 +27,18 @@
* @author Brian Clozel
* @author Filip Hrisafov
* @since 2.1.1
* @deprecated since 2.7.0 for removal in 2.9.0 in favor of
* {@link ElasticsearchRestClientHealthIndicator}
*/
public class ElasticsearchRestHealthIndicator extends AbstractHealthIndicator {

private static final String RED_STATUS = "red";

private final RestClient client;

private final JsonParser jsonParser;
@Deprecated
public class ElasticsearchRestHealthIndicator extends ElasticsearchRestClientHealthIndicator {

/**
* Create a new {@code ElasticsearchRestHealthIndicator} using the {@link RestClient}
* obtained from the given high-level client.
* @param client the high-level client
* @deprecated since 2.7.0 for removal in 2.9.0 in favor of
* {@link #ElasticsearchRestHealthIndicator(RestClient)}
*/
@Deprecated
public ElasticsearchRestHealthIndicator(org.elasticsearch.client.RestHighLevelClient client) {
this(client.getLowLevelClient());
}

public ElasticsearchRestHealthIndicator(RestClient client) {
super("Elasticsearch health check failed");
this.client = client;
this.jsonParser = JsonParserFactory.getJsonParser();
}

@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
Response response = this.client.performRequest(new Request("GET", "/_cluster/health/"));
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() != HttpStatus.SC_OK) {
builder.down();
builder.withDetail("statusCode", statusLine.getStatusCode());
builder.withDetail("reasonPhrase", statusLine.getReasonPhrase());
return;
}
try (InputStream inputStream = response.getEntity().getContent()) {
doHealthCheck(builder, StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8));
}
}

private void doHealthCheck(Health.Builder builder, String json) {
Map<String, Object> response = this.jsonParser.parseMap(json);
String status = (String) response.get("status");
if (RED_STATUS.equals(status)) {
builder.outOfService();
}
else {
builder.up();
}
builder.withDetails(response);
super(client);
}

}
Expand Up @@ -37,16 +37,16 @@
import static org.mockito.Mockito.mock;

/**
* Tests for {@link ElasticsearchRestHealthIndicator}.
* Tests for {@link ElasticsearchRestClientHealthIndicator}.
*
* @author Artsiom Yudovin
* @author Filip Hrisafov
*/
class ElasticsearchRestHealthIndicatorTests {
class ElasticsearchRestClientHealthIndicatorTests {

private final RestClient restClient = mock(RestClient.class);

private final ElasticsearchRestHealthIndicator elasticsearchRestHealthIndicator = new ElasticsearchRestHealthIndicator(
private final ElasticsearchRestClientHealthIndicator elasticsearchRestClientHealthIndicator = new ElasticsearchRestClientHealthIndicator(
this.restClient);

@Test
Expand All @@ -59,7 +59,7 @@ void elasticsearchIsUp() throws IOException {
given(response.getStatusLine()).willReturn(statusLine);
given(response.getEntity()).willReturn(httpEntity);
given(this.restClient.performRequest(any(Request.class))).willReturn(response);
Health health = this.elasticsearchRestHealthIndicator.health();
Health health = this.elasticsearchRestClientHealthIndicator.health();
assertThat(health.getStatus()).isEqualTo(Status.UP);
assertHealthDetailsWithStatus(health.getDetails(), "green");
}
Expand All @@ -74,15 +74,15 @@ void elasticsearchWithYellowStatusIsUp() throws IOException {
given(response.getStatusLine()).willReturn(statusLine);
given(response.getEntity()).willReturn(httpEntity);
given(this.restClient.performRequest(any(Request.class))).willReturn(response);
Health health = this.elasticsearchRestHealthIndicator.health();
Health health = this.elasticsearchRestClientHealthIndicator.health();
assertThat(health.getStatus()).isEqualTo(Status.UP);
assertHealthDetailsWithStatus(health.getDetails(), "yellow");
}

@Test
void elasticsearchIsDown() throws IOException {
given(this.restClient.performRequest(any(Request.class))).willThrow(new IOException("Couldn't connect"));
Health health = this.elasticsearchRestHealthIndicator.health();
Health health = this.elasticsearchRestClientHealthIndicator.health();
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
assertThat(health.getDetails()).contains(entry("error", "java.io.IOException: Couldn't connect"));
}
Expand All @@ -95,7 +95,7 @@ void elasticsearchIsDownByResponseCode() throws IOException {
given(statusLine.getReasonPhrase()).willReturn("Internal server error");
given(response.getStatusLine()).willReturn(statusLine);
given(this.restClient.performRequest(any(Request.class))).willReturn(response);
Health health = this.elasticsearchRestHealthIndicator.health();
Health health = this.elasticsearchRestClientHealthIndicator.health();
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
assertThat(health.getDetails()).contains(entry("statusCode", 500),
entry("reasonPhrase", "Internal server error"));
Expand All @@ -111,7 +111,7 @@ void elasticsearchIsOutOfServiceByStatus() throws IOException {
given(response.getStatusLine()).willReturn(statusLine);
given(response.getEntity()).willReturn(httpEntity);
given(this.restClient.performRequest(any(Request.class))).willReturn(response);
Health health = this.elasticsearchRestHealthIndicator.health();
Health health = this.elasticsearchRestClientHealthIndicator.health();
assertThat(health.getStatus()).isEqualTo(Status.OUT_OF_SERVICE);
assertHealthDetailsWithStatus(health.getDetails(), "red");
}
Expand Down

0 comments on commit 27a936e

Please sign in to comment.