Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for reactive Elasticsearch healthcheck
Prior to this commit, configuring a reactive Elasticsearch client would auto-configure an Actuator Health check using a synchronous client, with the default configuration properties (so tarting localhost:9200). This would lead to false reports of unhealthy Elasticsearch clusters when using reactive clients. This commit reproduces the logic for MongoDB repositories: if a reactive variant is available, it is selected for the health check infrastructure. See gh-21042
- Loading branch information
Showing
7 changed files
with
192 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
.../autoconfigure/elasticsearch/ElasticSearchReactiveHealthContributorAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright 2012-2020 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.autoconfigure.elasticsearch; | ||
|
||
import java.util.Map; | ||
|
||
import reactor.core.publisher.Flux; | ||
|
||
import org.springframework.boot.actuate.autoconfigure.health.CompositeReactiveHealthContributorConfiguration; | ||
import org.springframework.boot.actuate.autoconfigure.health.ConditionalOnEnabledHealthIndicator; | ||
import org.springframework.boot.actuate.elasticsearch.ElasticsearchReactiveHealthIndicator; | ||
import org.springframework.boot.actuate.health.ReactiveHealthContributor; | ||
import org.springframework.boot.autoconfigure.AutoConfigureAfter; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.autoconfigure.data.elasticsearch.ReactiveElasticsearchRestClientAutoConfiguration; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient; | ||
|
||
/** | ||
* {@link EnableAutoConfiguration Auto-configuration} for | ||
* {@link ElasticsearchReactiveHealthIndicator} using the | ||
* {@link ReactiveElasticsearchClient}. | ||
* | ||
* @author Aleksander Lech | ||
* @since 2.3 | ||
*/ | ||
@Configuration(proxyBeanMethods = false) | ||
@ConditionalOnClass({ ReactiveElasticsearchClient.class, Flux.class }) | ||
@ConditionalOnBean(ReactiveElasticsearchClient.class) | ||
@ConditionalOnEnabledHealthIndicator("elasticsearch") | ||
@AutoConfigureAfter(ReactiveElasticsearchRestClientAutoConfiguration.class) | ||
public class ElasticSearchReactiveHealthContributorAutoConfiguration extends | ||
CompositeReactiveHealthContributorConfiguration<ElasticsearchReactiveHealthIndicator, ReactiveElasticsearchClient> { | ||
|
||
@Bean | ||
@ConditionalOnMissingBean(name = { "elasticsearchHealthIndicator", "elasticsearchHealthContributor" }) | ||
public ReactiveHealthContributor elasticsearchHealthContributor(Map<String, ReactiveElasticsearchClient> clients) { | ||
return createContributor(clients); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...configure/elasticsearch/ElasticsearchReactiveHealthContributorAutoConfigurationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2012-2020 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.autoconfigure.elasticsearch; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
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.autoconfigure.AutoConfigurations; | ||
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.data.elasticsearch.ReactiveElasticsearchRestClientAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientAutoConfiguration; | ||
import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests for {@link ElasticSearchReactiveHealthContributorAutoConfiguration}. | ||
* | ||
* @author Aleksander Lech | ||
*/ | ||
class ElasticsearchReactiveHealthContributorAutoConfigurationTests { | ||
|
||
private ApplicationContextRunner contextRunner = new ApplicationContextRunner().withConfiguration(AutoConfigurations | ||
.of(ElasticsearchDataAutoConfiguration.class, ReactiveElasticsearchRestClientAutoConfiguration.class, | ||
ElasticsearchRestClientAutoConfiguration.class, | ||
ElasticSearchReactiveHealthContributorAutoConfiguration.class, | ||
HealthContributorAutoConfiguration.class)); | ||
|
||
@Test | ||
void runShouldCreateIndicator() { | ||
this.contextRunner.run((context) -> assertThat(context) | ||
.hasSingleBean(ElasticsearchReactiveHealthIndicator.class).hasBean("elasticsearchHealthContributor")); | ||
} | ||
|
||
@Test | ||
void runWithRegularIndicatorShouldOnlyCreateReactiveIndicator() { | ||
this.contextRunner | ||
.withConfiguration(AutoConfigurations.of(ElasticSearchRestHealthContributorAutoConfiguration.class)) | ||
.run((context) -> assertThat(context).hasSingleBean(ElasticsearchReactiveHealthIndicator.class) | ||
.hasBean("elasticsearchHealthContributor") | ||
.doesNotHaveBean(ElasticsearchRestHealthIndicator.class)); | ||
} | ||
|
||
@Test | ||
void runWhenDisabledShouldNotCreateIndicator() { | ||
this.contextRunner.withPropertyValues("management.health.elasticsearch.enabled:false") | ||
.run((context) -> assertThat(context).doesNotHaveBean(ElasticsearchReactiveHealthIndicator.class) | ||
.doesNotHaveBean("elasticsearchHealthContributor")); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
.../org/springframework/boot/actuate/elasticsearch/ElasticsearchReactiveHealthIndicator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2012-2020 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.util.stream.Collectors; | ||
|
||
import reactor.core.publisher.Mono; | ||
|
||
import org.springframework.boot.actuate.health.AbstractReactiveHealthIndicator; | ||
import org.springframework.boot.actuate.health.Health; | ||
import org.springframework.boot.actuate.health.HealthIndicator; | ||
import org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient; | ||
|
||
/** | ||
* {@link HealthIndicator} for an Elasticsearch cluster using a | ||
* {@link ReactiveElasticsearchClient}. | ||
* | ||
* @author Aleksander Lech | ||
* @since 2.3 | ||
*/ | ||
public class ElasticsearchReactiveHealthIndicator extends AbstractReactiveHealthIndicator { | ||
|
||
private final ReactiveElasticsearchClient client; | ||
|
||
public ElasticsearchReactiveHealthIndicator(ReactiveElasticsearchClient client) { | ||
super("Elasticsearch health check failed"); | ||
this.client = client; | ||
} | ||
|
||
@Override | ||
protected Mono<Health> doHealthCheck(Health.Builder builder) { | ||
return this.client.status().map((status) -> { | ||
if (status.isOk()) { | ||
builder.up(); | ||
} | ||
else { | ||
builder.down(); | ||
} | ||
|
||
builder.withDetails(status.hosts().stream().collect(Collectors | ||
.toMap((host) -> host.getEndpoint().getHostString(), (host) -> host.getState().toString()))); | ||
|
||
return builder.build(); | ||
}); | ||
} | ||
|
||
} |