diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/elasticsearch/ElasticSearchRestHealthContributorAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/elasticsearch/ElasticSearchRestHealthContributorAutoConfiguration.java index 095c8e5f3e5a..adc2194cd7ff 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/elasticsearch/ElasticSearchRestHealthContributorAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/elasticsearch/ElasticSearchRestHealthContributorAutoConfiguration.java @@ -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; @@ -34,7 +34,7 @@ /** * {@link EnableAutoConfiguration Auto-configuration} for - * {@link ElasticsearchRestHealthIndicator} using the {@link RestClient}. + * {@link ElasticsearchRestClientHealthIndicator}. * * @author Artsiom Yudovin * @since 2.1.1 @@ -44,7 +44,7 @@ @ConditionalOnBean(RestClient.class) @ConditionalOnEnabledHealthIndicator("elasticsearch") public class ElasticSearchRestHealthContributorAutoConfiguration - extends CompositeHealthContributorConfiguration { + extends CompositeHealthContributorConfiguration { @Bean @ConditionalOnMissingBean(name = { "elasticsearchHealthIndicator", "elasticsearchHealthContributor" }) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/elasticsearch/ElasticSearchRestHealthContributorAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/elasticsearch/ElasticSearchRestHealthContributorAutoConfigurationTests.java index 56972fc4ba43..5c1f4d9aa4c3 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/elasticsearch/ElasticSearchRestHealthContributorAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/elasticsearch/ElasticSearchRestHealthContributorAutoConfigurationTests.java @@ -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; @@ -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 @@ -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")); } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/elasticsearch/ElasticsearchReactiveHealthContributorAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/elasticsearch/ElasticsearchReactiveHealthContributorAutoConfigurationTests.java index c14fc96e4f62..7cf3bce2fc27 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/elasticsearch/ElasticsearchReactiveHealthContributorAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/elasticsearch/ElasticsearchReactiveHealthContributorAutoConfigurationTests.java @@ -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. @@ -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; @@ -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 diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchRestClientHealthIndicator.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchRestClientHealthIndicator.java new file mode 100644 index 000000000000..ad1d44f2e38f --- /dev/null +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchRestClientHealthIndicator.java @@ -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 response = this.jsonParser.parseMap(json); + String status = (String) response.get("status"); + if (RED_STATUS.equals(status)) { + builder.outOfService(); + } + else { + builder.up(); + } + builder.withDetails(response); + } + +} diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchRestHealthIndicator.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchRestHealthIndicator.java index 57c9235d1dc0..1e9f78e8e632 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchRestHealthIndicator.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchRestHealthIndicator.java @@ -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}. @@ -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 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); } } diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchRestHealthIndicatorTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchRestClientHealthIndicatorTests.java similarity index 90% rename from spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchRestHealthIndicatorTests.java rename to spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchRestClientHealthIndicatorTests.java index c283c049e2b3..a718c6dc9119 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchRestHealthIndicatorTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchRestClientHealthIndicatorTests.java @@ -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 @@ -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"); } @@ -74,7 +74,7 @@ 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"); } @@ -82,7 +82,7 @@ void elasticsearchWithYellowStatusIsUp() throws IOException { @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")); } @@ -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")); @@ -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"); }