Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
58c8c2cc
Commit
58c8c2cc
authored
Jan 22, 2015
by
Binwei Yang
Committed by
Andy Wilkinson
Apr 08, 2015
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a health indicator for an Elasticsearch cluster
Closes gh-2399
parent
1dcf18b1
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
260 additions
and
1 deletion
+260
-1
pom.xml
spring-boot-actuator/pom.xml
+5
-0
ElasticsearchHealthIndicatorConfiguration.java
...oconfigure/ElasticsearchHealthIndicatorConfiguration.java
+55
-0
ElasticsearchHealthIndicator.java
...ork/boot/actuate/health/ElasticsearchHealthIndicator.java
+62
-0
ElasticsearchHealthIndicatorProperties.java
...ctuate/health/ElasticsearchHealthIndicatorProperties.java
+52
-0
spring.factories
...oot-actuator/src/main/resources/META-INF/spring.factories
+2
-1
ElasticsearchHealthIndicatorTest.java
...boot/actuate/health/ElasticsearchHealthIndicatorTest.java
+84
-0
No files found.
spring-boot-actuator/pom.xml
View file @
58c8c2cc
...
@@ -96,6 +96,11 @@
...
@@ -96,6 +96,11 @@
<artifactId>
spring-data-solr
</artifactId>
<artifactId>
spring-data-solr
</artifactId>
<optional>
true
</optional>
<optional>
true
</optional>
</dependency>
</dependency>
<dependency>
<groupId>
org.springframework.data
</groupId>
<artifactId>
spring-data-elasticsearch
</artifactId>
<optional>
true
</optional>
</dependency>
<dependency>
<dependency>
<groupId>
org.springframework.security
</groupId>
<groupId>
org.springframework.security
</groupId>
<artifactId>
spring-security-web
</artifactId>
<artifactId>
spring-security-web
</artifactId>
...
...
spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/ElasticsearchHealthIndicatorConfiguration.java
0 → 100644
View file @
58c8c2cc
/*
* 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
();
}
}
spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/ElasticsearchHealthIndicator.java
0 → 100644
View file @
58c8c2cc
/*
* 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
);
}
}
}
spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/ElasticsearchHealthIndicatorProperties.java
0 → 100644
View file @
58c8c2cc
/*
* 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
(
","
);
}
}
}
spring-boot-actuator/src/main/resources/META-INF/spring.factories
View file @
58c8c2cc
...
@@ -12,4 +12,5 @@ org.springframework.boot.actuate.autoconfigure.MetricFilterAutoConfiguration,\
...
@@ -12,4 +12,5 @@ org.springframework.boot.actuate.autoconfigure.MetricFilterAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.MetricRepositoryAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.MetricRepositoryAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.PublicMetricsAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.PublicMetricsAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.TraceRepositoryAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.TraceRepositoryAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.TraceWebFilterAutoConfiguration
org.springframework.boot.actuate.autoconfigure.TraceWebFilterAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.ElasticsearchHealthIndicatorConfiguration
spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/ElasticsearchHealthIndicatorTest.java
0 → 100644
View file @
58c8c2cc
/*
* 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.client.Client
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.springframework.boot.actuate.autoconfigure.ElasticsearchHealthIndicatorConfiguration
;
import
org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration
;
import
org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration
;
import
org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchAutoConfiguration
;
import
org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchDataAutoConfiguration
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
/**
* Test for {@link ElasticsearchHealthIndicator} for ElasticSearch cluster.
*
* @author Binwei Yang
* @since 1.2.2
*/
public
class
ElasticsearchHealthIndicatorTest
{
private
AnnotationConfigApplicationContext
context
;
@Before
public
void
setUp
()
throws
Exception
{
this
.
context
=
new
AnnotationConfigApplicationContext
(
PropertyPlaceholderAutoConfiguration
.
class
,
ElasticsearchAutoConfiguration
.
class
,
ElasticsearchDataAutoConfiguration
.
class
,
EndpointAutoConfiguration
.
class
,
ElasticsearchHealthIndicatorConfiguration
.
class
);
}
@After
public
void
close
()
{
if
(
null
!=
context
)
{
context
.
close
();
}
}
@Test
public
void
indicatorExists
()
{
assertEquals
(
1
,
this
.
context
.
getBeanNamesForType
(
Client
.
class
).
length
);
ElasticsearchHealthIndicator
healthIndicator
=
this
.
context
.
getBean
(
ElasticsearchHealthIndicator
.
class
);
assertNotNull
(
healthIndicator
);
}
@Test
public
void
configPropertiesUsed
()
{
ElasticsearchHealthIndicatorProperties
properties
=
this
.
context
.
getBean
(
ElasticsearchHealthIndicatorProperties
.
class
);
// test default index
assertEquals
(
ElasticsearchHealthIndicatorProperties
.
ALL
,
properties
.
getIndices
());
assertEquals
(
1
,
properties
.
getIndexNamesAsArray
().
length
);
assertEquals
(
ElasticsearchHealthIndicatorProperties
.
ALL
,
properties
.
getIndexNamesAsArray
()[
0
]);
// test specific indices
properties
.
setIndices
(
"no-such-index"
);
ElasticsearchHealthIndicator
healthIndicator
=
this
.
context
.
getBean
(
ElasticsearchHealthIndicator
.
class
);
Health
health
=
healthIndicator
.
health
();
assertEquals
(
Status
.
UNKNOWN
,
health
.
getStatus
());
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment