- 
                Notifications
    You must be signed in to change notification settings 
- Fork 41.6k
adding actuator health check support for Elasticsearch REST Clients #15211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
8de8403
              765e263
              da11afd
              893478f
              e9b2557
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /* | ||
| * Copyright 2012-2018 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 | ||
| * | ||
| * http://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 org.elasticsearch.client.RestHighLevelClient; | ||
|  | ||
| import org.springframework.boot.actuate.autoconfigure.health.CompositeHealthIndicatorConfiguration; | ||
| import org.springframework.boot.actuate.autoconfigure.health.ConditionalOnEnabledHealthIndicator; | ||
| import org.springframework.boot.actuate.autoconfigure.health.HealthIndicatorAutoConfiguration; | ||
| import org.springframework.boot.actuate.elasticsearch.ElasticsearchRestHealthIndicator; | ||
| import org.springframework.boot.actuate.health.HealthIndicator; | ||
| import org.springframework.boot.autoconfigure.AutoConfigureAfter; | ||
| import org.springframework.boot.autoconfigure.AutoConfigureBefore; | ||
| 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.elasticsearch.rest.RestClientAutoConfiguration; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
|  | ||
| /** | ||
| * {@link EnableAutoConfiguration Auto-configuration} for | ||
| * {@link ElasticsearchRestHealthIndicator} using the {@link RestHighLevelClient}. | ||
| * | ||
| * @author Artsiom Yudovin | ||
| * @since 2.1.0 | ||
| */ | ||
|  | ||
| @Configuration | ||
| @ConditionalOnClass(RestHighLevelClient.class) | ||
|          | ||
| @ConditionalOnBean(RestHighLevelClient.class) | ||
| @ConditionalOnEnabledHealthIndicator("elasticsearch") | ||
| @AutoConfigureBefore(HealthIndicatorAutoConfiguration.class) | ||
| @AutoConfigureAfter({ RestClientAutoConfiguration.class, | ||
| ElasticSearchClientHealthIndicatorAutoConfiguration.class }) | ||
| public class ElasticSearchRestHealthIndicatorAutoConfiguration extends | ||
| CompositeHealthIndicatorConfiguration<ElasticsearchRestHealthIndicator, RestHighLevelClient> { | ||
|  | ||
| private final Map<String, RestHighLevelClient> clients; | ||
|  | ||
| public ElasticSearchRestHealthIndicatorAutoConfiguration( | ||
| Map<String, RestHighLevelClient> clients) { | ||
| this.clients = clients; | ||
| } | ||
|  | ||
| @Bean | ||
| @ConditionalOnMissingBean(name = "elasticsearchRestHealthIndicator") | ||
| public HealthIndicator elasticsearchRestHealthIndicator() { | ||
| return createHealthIndicator(this.clients); | ||
| } | ||
|  | ||
| @Override | ||
| protected ElasticsearchRestHealthIndicator createHealthIndicator( | ||
| RestHighLevelClient client) { | ||
| return new ElasticsearchRestHealthIndicator(client); | ||
| } | ||
|  | ||
| } | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /* | ||
| * Copyright 2012-2018 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 | ||
| * | ||
| * http://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 org.elasticsearch.action.main.MainResponse; | ||
| import org.elasticsearch.client.RequestOptions; | ||
| import org.elasticsearch.client.RestHighLevelClient; | ||
|  | ||
| import org.springframework.boot.actuate.health.AbstractHealthIndicator; | ||
| import org.springframework.boot.actuate.health.Health; | ||
| import org.springframework.boot.actuate.health.HealthIndicator; | ||
|  | ||
| /** | ||
| * {@link HealthIndicator} for an Elasticsearch cluster by REST. | ||
| * | ||
| * @author Artsiom Yudovin | ||
| * @since 2.1.0 | ||
| */ | ||
| public class ElasticsearchRestHealthIndicator extends AbstractHealthIndicator { | ||
|  | ||
| private final RestHighLevelClient client; | ||
|  | ||
| public ElasticsearchRestHealthIndicator(RestHighLevelClient client) { | ||
| super("Elasticsearch health check failed"); | ||
| this.client = client; | ||
| } | ||
|  | ||
| @Override | ||
| protected void doHealthCheck(Health.Builder builder) throws Exception { | ||
| MainResponse info = this.client.info(RequestOptions.DEFAULT); | ||
|          | ||
|  | ||
| if (info.isAvailable()) { | ||
| builder.up(); | ||
| } | ||
| else { | ||
| builder.down(); | ||
| } | ||
|  | ||
| builder.withDetail("clusterName", info.getClusterName()); | ||
| builder.withDetail("nodeName", info.getNodeName()); | ||
| builder.withDetail("clusterUuid", info.getClusterUuid()); | ||
| builder.withDetail("version", info.getVersion()); | ||
| } | ||
|  | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why
elasticsearch-rest-high-level-client? Spring Boot supports bothelasticsearch-rest-clientandelasticsearch-rest-high-level-client(which is using the former under the covers). So I think we could just have a health indicator forelasticsearch-rest-clientand cover both.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, I've added changes by your suggestion.