Skip to content

Commit dbf7d54

Browse files
committed
Introduce HealthIndicatorRegistry
This commit introduces HealthIndicatorRegistry which handles registration of HealthIndicator instances. Registering new HealthIndicator instances is now possible in runtime.
1 parent 112b707 commit dbf7d54

File tree

9 files changed

+292
-41
lines changed

9 files changed

+292
-41
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointAutoConfiguration.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,7 +19,6 @@
1919
import java.util.ArrayList;
2020
import java.util.Collection;
2121
import java.util.Collections;
22-
import java.util.HashMap;
2322
import java.util.LinkedHashMap;
2423
import java.util.List;
2524
import java.util.Map;
@@ -46,8 +45,9 @@
4645
import org.springframework.boot.actuate.endpoint.RequestMappingEndpoint;
4746
import org.springframework.boot.actuate.endpoint.ShutdownEndpoint;
4847
import org.springframework.boot.actuate.endpoint.TraceEndpoint;
48+
import org.springframework.boot.actuate.health.DefaultHealthIndicatorRegistry;
4949
import org.springframework.boot.actuate.health.HealthAggregator;
50-
import org.springframework.boot.actuate.health.HealthIndicator;
50+
import org.springframework.boot.actuate.health.HealthIndicatorRegistry;
5151
import org.springframework.boot.actuate.health.OrderedHealthAggregator;
5252
import org.springframework.boot.actuate.trace.InMemoryTraceRepository;
5353
import org.springframework.boot.actuate.trace.TraceRepository;
@@ -81,6 +81,7 @@
8181
* @author Christian Dupuis
8282
* @author Stephane Nicoll
8383
* @author Eddú Meléndez
84+
* @author Vedran Pavic
8485
*/
8586
@Configuration
8687
@AutoConfigureAfter({ FlywayAutoConfiguration.class, LiquibaseAutoConfiguration.class })
@@ -94,7 +95,7 @@ public class EndpointAutoConfiguration {
9495
private HealthAggregator healthAggregator = new OrderedHealthAggregator();
9596

9697
@Autowired(required = false)
97-
private Map<String, HealthIndicator> healthIndicators = new HashMap<String, HealthIndicator>();
98+
private HealthIndicatorRegistry healthIndicatorRegistry = new DefaultHealthIndicatorRegistry();
9899

99100
@Autowired(required = false)
100101
private Collection<PublicMetrics> publicMetrics;
@@ -111,7 +112,7 @@ public EnvironmentEndpoint environmentEndpoint() {
111112
@Bean
112113
@ConditionalOnMissingBean
113114
public HealthEndpoint healthEndpoint() {
114-
return new HealthEndpoint(this.healthAggregator, this.healthIndicators);
115+
return new HealthEndpoint(this.healthAggregator, this.healthIndicatorRegistry);
115116
}
116117

117118
@Bean

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/HealthIndicatorAutoConfiguration.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,12 +33,14 @@
3333
import org.springframework.boot.actuate.health.CassandraHealthIndicator;
3434
import org.springframework.boot.actuate.health.CompositeHealthIndicator;
3535
import org.springframework.boot.actuate.health.DataSourceHealthIndicator;
36+
import org.springframework.boot.actuate.health.DefaultHealthIndicatorRegistry;
3637
import org.springframework.boot.actuate.health.DiskSpaceHealthIndicator;
3738
import org.springframework.boot.actuate.health.DiskSpaceHealthIndicatorProperties;
3839
import org.springframework.boot.actuate.health.ElasticsearchHealthIndicator;
3940
import org.springframework.boot.actuate.health.ElasticsearchHealthIndicatorProperties;
4041
import org.springframework.boot.actuate.health.HealthAggregator;
4142
import org.springframework.boot.actuate.health.HealthIndicator;
43+
import org.springframework.boot.actuate.health.HealthIndicatorRegistry;
4244
import org.springframework.boot.actuate.health.JmsHealthIndicator;
4345
import org.springframework.boot.actuate.health.MailHealthIndicator;
4446
import org.springframework.boot.actuate.health.MongoHealthIndicator;
@@ -84,6 +86,7 @@
8486
* @author Stephane Nicoll
8587
* @author Phillip Webb
8688
* @author Tommy Ludwig
89+
* @author Vedran Pavic
8790
* @since 1.1.0
8891
*/
8992
@Configuration
@@ -110,6 +113,19 @@ public OrderedHealthAggregator healthAggregator() {
110113
return healthAggregator;
111114
}
112115

116+
@Bean
117+
@ConditionalOnMissingBean(HealthIndicatorRegistry.class)
118+
public HealthIndicatorRegistry healthIndicatorRegistry(
119+
Map<String, HealthIndicator> healthIndicators) {
120+
HealthIndicatorRegistry registry = new DefaultHealthIndicatorRegistry();
121+
for (Map.Entry<String, HealthIndicator> entry : healthIndicators.entrySet()) {
122+
String name = entry.getKey();
123+
int index = name.toLowerCase().indexOf("healthindicator");
124+
registry.register(index > 0 ? name.substring(0, index) : name, entry.getValue());
125+
}
126+
return registry;
127+
}
128+
113129
@Bean
114130
@ConditionalOnMissingBean(HealthIndicator.class)
115131
public ApplicationHealthIndicator applicationHealthIndicator() {
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,12 +16,11 @@
1616

1717
package org.springframework.boot.actuate.endpoint;
1818

19-
import java.util.Map;
20-
2119
import org.springframework.boot.actuate.health.CompositeHealthIndicator;
2220
import org.springframework.boot.actuate.health.Health;
2321
import org.springframework.boot.actuate.health.HealthAggregator;
2422
import org.springframework.boot.actuate.health.HealthIndicator;
23+
import org.springframework.boot.actuate.health.HealthIndicatorRegistry;
2524
import org.springframework.boot.context.properties.ConfigurationProperties;
2625
import org.springframework.util.Assert;
2726

@@ -31,11 +30,13 @@
3130
* @author Dave Syer
3231
* @author Christian Dupuis
3332
* @author Andy Wilkinson
33+
* @author Vedran Pavic
3434
*/
3535
@ConfigurationProperties(prefix = "endpoints.health", ignoreUnknownFields = true)
3636
public class HealthEndpoint extends AbstractEndpoint<Health> {
3737

38-
private final HealthIndicator healthIndicator;
38+
private final HealthAggregator healthAggregator;
39+
private final HealthIndicatorRegistry healthIndicatorRegistry;
3940

4041
/**
4142
* Time to live for cached result, in milliseconds.
@@ -45,19 +46,16 @@ public class HealthEndpoint extends AbstractEndpoint<Health> {
4546
/**
4647
* Create a new {@link HealthIndicator} instance.
4748
* @param healthAggregator the health aggregator
48-
* @param healthIndicators the health indicators
49+
* @param healthIndicatorRegistry the health indicator registry
4950
*/
5051
public HealthEndpoint(HealthAggregator healthAggregator,
51-
Map<String, HealthIndicator> healthIndicators) {
52+
HealthIndicatorRegistry healthIndicatorRegistry) {
5253
super("health", false);
5354
Assert.notNull(healthAggregator, "HealthAggregator must not be null");
54-
Assert.notNull(healthIndicators, "HealthIndicators must not be null");
55-
CompositeHealthIndicator healthIndicator = new CompositeHealthIndicator(
56-
healthAggregator);
57-
for (Map.Entry<String, HealthIndicator> entry : healthIndicators.entrySet()) {
58-
healthIndicator.addHealthIndicator(getKey(entry.getKey()), entry.getValue());
59-
}
60-
this.healthIndicator = healthIndicator;
55+
Assert.notNull(healthIndicatorRegistry,
56+
"healthIndicatorRegistry must not be null");
57+
this.healthAggregator = healthAggregator;
58+
this.healthIndicatorRegistry = healthIndicatorRegistry;
6159
}
6260

6361
/**
@@ -78,19 +76,9 @@ public void setTimeToLive(long ttl) {
7876
*/
7977
@Override
8078
public Health invoke() {
81-
return this.healthIndicator.health();
79+
HealthIndicator indicator = new CompositeHealthIndicator(
80+
this.healthAggregator, this.healthIndicatorRegistry.getAll());
81+
return indicator.health();
8282
}
8383

84-
/**
85-
* Turns the bean name into a key that can be used in the map of health information.
86-
* @param name the bean name
87-
* @return the key
88-
*/
89-
private String getKey(String name) {
90-
int index = name.toLowerCase().indexOf("healthindicator");
91-
if (index > 0) {
92-
return name.substring(0, index);
93-
}
94-
return name;
95-
}
9684
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2012-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.health;
18+
19+
import java.util.Collections;
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
import java.util.concurrent.ConcurrentHashMap;
23+
import java.util.concurrent.ConcurrentMap;
24+
25+
import org.springframework.util.Assert;
26+
27+
/**
28+
* Default implementation of {@link HealthIndicatorRegistry}.
29+
*
30+
* @author Vedran Pavic
31+
*/
32+
public class DefaultHealthIndicatorRegistry implements HealthIndicatorRegistry {
33+
34+
private final ConcurrentMap<String, HealthIndicator> healthIndicators =
35+
new ConcurrentHashMap<String, HealthIndicator>();
36+
37+
@Override
38+
public void register(String name, HealthIndicator healthIndicator) {
39+
Assert.notNull(healthIndicator, "HealthIndicator must not be null");
40+
if (this.healthIndicators.putIfAbsent(name, healthIndicator) != null) {
41+
throw new IllegalStateException(
42+
"HealthIndicator with name '" + name + "' already registered");
43+
}
44+
}
45+
46+
@Override
47+
public HealthIndicator unregister(String name) {
48+
return this.healthIndicators.remove(name);
49+
}
50+
51+
@Override
52+
public HealthIndicator get(String name) {
53+
return this.healthIndicators.get(name);
54+
}
55+
56+
@Override
57+
public Map<String, HealthIndicator> getAll() {
58+
return Collections.unmodifiableMap(
59+
new HashMap<String, HealthIndicator>(this.healthIndicators));
60+
}
61+
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2012-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.health;
18+
19+
import java.util.Map;
20+
21+
/**
22+
* A registry of {@link HealthIndicator}s.
23+
* <p>
24+
* Implementations <strong>must</strong> be thread-safe.
25+
*
26+
* @author Andy Wilkinson
27+
* @author Vedran Pavic
28+
*/
29+
public interface HealthIndicatorRegistry {
30+
31+
/**
32+
* Registers the given {@code healthIndicator}, associating it with the given
33+
* {@code name}.
34+
* @param name the name of the indicator
35+
* @param healthIndicator the indicator
36+
* @throws IllegalStateException if an indicator with the given {@code name} is
37+
* already registered.
38+
*/
39+
void register(String name, HealthIndicator healthIndicator);
40+
41+
/**
42+
* Unregisters the {@code HealthIndicator} previously registered with the given
43+
* {@code name}.
44+
* @param name the name of the indicator
45+
* @return the unregistered indicator, or {@code null} if no indicator was found in
46+
* the registry for the given {@code name}.
47+
*/
48+
HealthIndicator unregister(String name);
49+
50+
/**
51+
* Returns the health indicator registered with the given {@code name}.
52+
* @param name the name of the indicator
53+
* @return the health indicator, or {@code null} if no indicator was registered with
54+
* the given {@code name}.
55+
*/
56+
HealthIndicator get(String name);
57+
58+
/**
59+
* Returns a snapshot of the registered health indicators and their names. The
60+
* contents of the map do not reflect subsequent changes to the registry.
61+
* @return the snapshot of registered health indicators
62+
*/
63+
Map<String, HealthIndicator> getAll();
64+
65+
}

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/HealthMvcEndpointAutoConfigurationTests.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,8 +21,10 @@
2121

2222
import org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint;
2323
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
24+
import org.springframework.boot.actuate.health.DefaultHealthIndicatorRegistry;
2425
import org.springframework.boot.actuate.health.Health;
2526
import org.springframework.boot.actuate.health.Health.Builder;
27+
import org.springframework.boot.actuate.health.HealthIndicatorRegistry;
2628
import org.springframework.boot.actuate.health.Status;
2729
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
2830
import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration;
@@ -42,6 +44,7 @@
4244
*
4345
* @author Dave Syer
4446
* @author Andy Wilkinson
47+
* @author Vedran Pavic
4548
*/
4649
public class HealthMvcEndpointAutoConfigurationTests {
4750

@@ -94,6 +97,13 @@ public TestHealthIndicator testHealthIndicator() {
9497
return new TestHealthIndicator();
9598
}
9699

100+
@Bean
101+
public HealthIndicatorRegistry healthIndicatorRegistry() {
102+
DefaultHealthIndicatorRegistry registry = new DefaultHealthIndicatorRegistry();
103+
registry.register("test", testHealthIndicator());
104+
return registry;
105+
}
106+
97107
}
98108

99109
static class TestHealthIndicator extends AbstractHealthIndicator {

0 commit comments

Comments
 (0)