Add a health indicator for an Elasticsearch cluster
Closes gh-2399
This commit is contained in:
committed by
Andy Wilkinson
parent
1dcf18b16e
commit
58c8c2ccdc
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright 2014 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;
|
||||
|
||||
import org.elasticsearch.client.Client;
|
||||
import org.springframework.boot.actuate.health.ElasticsearchHealthIndicator;
|
||||
import org.springframework.boot.actuate.health.ElasticsearchHealthIndicatorProperties;
|
||||
import org.springframework.boot.actuate.health.HealthIndicator;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration Auto-configuration} for
|
||||
* {@link org.springframework.boot.actuate.health.ElasticsearchHealthIndicator}.
|
||||
*
|
||||
* @author Binwei Yang
|
||||
* @since 1.2.2
|
||||
*/
|
||||
@Configuration
|
||||
@AutoConfigureBefore({EndpointAutoConfiguration.class})
|
||||
@AutoConfigureAfter({HealthIndicatorAutoConfiguration.class})
|
||||
@ConditionalOnProperty(prefix = "management.health.elasticsearch", name = "enabled", matchIfMissing = true)
|
||||
public class ElasticsearchHealthIndicatorConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(Client.class)
|
||||
@ConditionalOnMissingBean(name = "elasticsearchHealthIndicator")
|
||||
public HealthIndicator elasticsearchHealthIndicator() {
|
||||
return new ElasticsearchHealthIndicator();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ElasticsearchHealthIndicatorProperties elasticsearchHealthIndicatorProperties() {
|
||||
return new ElasticsearchHealthIndicatorProperties();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright 2014 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.health;
|
||||
|
||||
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
|
||||
import org.elasticsearch.client.Client;
|
||||
import org.elasticsearch.client.Requests;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
/**
|
||||
* Simple implementation of a {@link HealthIndicator} returning health status information for
|
||||
* ElasticSearch cluster.
|
||||
*
|
||||
* @author Binwei Yang
|
||||
* @since 1.2.2
|
||||
*/
|
||||
public class ElasticsearchHealthIndicator extends ApplicationHealthIndicator {
|
||||
@Autowired
|
||||
private Client client;
|
||||
|
||||
@Autowired
|
||||
private ElasticsearchHealthIndicatorProperties properties;
|
||||
|
||||
@Override
|
||||
protected void doHealthCheck(Health.Builder builder) throws Exception {
|
||||
try {
|
||||
ClusterHealthResponse response = client.admin().cluster().health(Requests.clusterHealthRequest(
|
||||
properties.getIndexNamesAsArray()
|
||||
)).actionGet(100);
|
||||
|
||||
switch (response.getStatus()) {
|
||||
case GREEN:
|
||||
builder.up();
|
||||
break;
|
||||
case RED:
|
||||
builder.down();
|
||||
break;
|
||||
case YELLOW:
|
||||
default:
|
||||
builder.unknown();
|
||||
break;
|
||||
}
|
||||
builder.withDetail("clusterHealth", response);
|
||||
} catch (Exception handled) {
|
||||
builder.unknown().withDetail("exception", handled);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright 2014 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.health;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* External configuration properties for {@link ElasticsearchHealthIndicator}
|
||||
*
|
||||
* @author Binwei Yang
|
||||
* @since 1.2.2
|
||||
*/
|
||||
@ConfigurationProperties("management.health.elasticsearch")
|
||||
public class ElasticsearchHealthIndicatorProperties {
|
||||
|
||||
public static final String ALL = "_all";
|
||||
|
||||
/**
|
||||
* comma separated index names. the default includes all indices.
|
||||
*/
|
||||
private String indices = ALL;
|
||||
|
||||
public String getIndices() {
|
||||
return indices;
|
||||
}
|
||||
|
||||
public void setIndices(String indices) {
|
||||
this.indices = indices;
|
||||
}
|
||||
|
||||
String[] getIndexNamesAsArray() {
|
||||
if (null == indices) {
|
||||
return new String[]{ALL};
|
||||
} else {
|
||||
return indices.split(",");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,4 +12,5 @@ org.springframework.boot.actuate.autoconfigure.MetricFilterAutoConfiguration,\
|
||||
org.springframework.boot.actuate.autoconfigure.MetricRepositoryAutoConfiguration,\
|
||||
org.springframework.boot.actuate.autoconfigure.PublicMetricsAutoConfiguration,\
|
||||
org.springframework.boot.actuate.autoconfigure.TraceRepositoryAutoConfiguration,\
|
||||
org.springframework.boot.actuate.autoconfigure.TraceWebFilterAutoConfiguration
|
||||
org.springframework.boot.actuate.autoconfigure.TraceWebFilterAutoConfiguration,\
|
||||
org.springframework.boot.actuate.autoconfigure.ElasticsearchHealthIndicatorConfiguration
|
||||
|
||||
Reference in New Issue
Block a user