Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,11 @@
<artifactId>elasticsearch</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
Copy link
Member

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 both elasticsearch-rest-client and elasticsearch-rest-high-level-client (which is using the former under the covers). So I think we could just have a health indicator for elasticsearch-rest-client and cover both.

Copy link
Contributor Author

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.

<optional>true</optional>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
Expand Down
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The RestClientAutoConfiguration is @ConditionalOnClass(RestClient.class), configuring the "low-level" RestClientBuilder in all cases, and the high level one if possible. I think we should just focus on building a low level RestClient using the auto-configured RestClientBuilder.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added changes by your suggestion.

@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);
}

}
5 changes: 5 additions & 0 deletions spring-boot-project/spring-boot-actuator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@
<artifactId>jest</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.undertow</groupId>
<artifactId>undertow-servlet</artifactId>
Expand Down
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ElasticsearchJestHealthIndicator is using the the health API, which is actually requesting the "/_cluster/health/" endpoint. I think we should align on that here. The RestHighLevelClient doesn't expose such an API, so that's another reason in favor of using the RestClient and manually issuing a request to that endpoint. Ideally, we should derive the health status from the "status" information, just like in ElasticsearchJestHealthIndicator.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added changes by your suggestion.


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());
}

}