Skip to content

Commit f11c11c

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 6f70d53 commit f11c11c

File tree

9 files changed

+296
-41
lines changed

9 files changed

+296
-41
lines changed

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.util.Collection;
2121
import java.util.Collections;
2222
import java.util.List;
23-
import java.util.Map;
2423

2524
import liquibase.integration.spring.SpringLiquibase;
2625
import org.flywaydb.core.Flyway;
@@ -42,8 +41,9 @@
4241
import org.springframework.boot.actuate.endpoint.RequestMappingEndpoint;
4342
import org.springframework.boot.actuate.endpoint.ShutdownEndpoint;
4443
import org.springframework.boot.actuate.endpoint.TraceEndpoint;
44+
import org.springframework.boot.actuate.health.DefaultHealthIndicatorRegistry;
4545
import org.springframework.boot.actuate.health.HealthAggregator;
46-
import org.springframework.boot.actuate.health.HealthIndicator;
46+
import org.springframework.boot.actuate.health.HealthIndicatorRegistry;
4747
import org.springframework.boot.actuate.health.OrderedHealthAggregator;
4848
import org.springframework.boot.actuate.info.InfoContributor;
4949
import org.springframework.boot.actuate.trace.InMemoryTraceRepository;
@@ -75,7 +75,7 @@
7575
* @author Stephane Nicoll
7676
* @author Eddú Meléndez
7777
* @author Meang Akira Tanaka
78-
*
78+
* @author Vedran Pavic
7979
*/
8080
@Configuration
8181
@AutoConfigureAfter({ FlywayAutoConfiguration.class, LiquibaseAutoConfiguration.class })
@@ -84,7 +84,7 @@ public class EndpointAutoConfiguration {
8484

8585
private final HealthAggregator healthAggregator;
8686

87-
private final Map<String, HealthIndicator> healthIndicators;
87+
private final HealthIndicatorRegistry healthIndicatorRegistry;
8888

8989
private final List<InfoContributor> infoContributors;
9090

@@ -94,12 +94,12 @@ public class EndpointAutoConfiguration {
9494

9595
public EndpointAutoConfiguration(
9696
ObjectProvider<HealthAggregator> healthAggregatorProvider,
97-
ObjectProvider<Map<String, HealthIndicator>> healthIndicatorsProvider,
97+
ObjectProvider<HealthIndicatorRegistry> healthIndicatorRegistryProvider,
9898
ObjectProvider<List<InfoContributor>> infoContributorsProvider,
9999
ObjectProvider<Collection<PublicMetrics>> publicMetricsProvider,
100100
ObjectProvider<TraceRepository> traceRepositoryProvider) {
101101
this.healthAggregator = healthAggregatorProvider.getIfAvailable();
102-
this.healthIndicators = healthIndicatorsProvider.getIfAvailable();
102+
this.healthIndicatorRegistry = healthIndicatorRegistryProvider.getIfAvailable();
103103
this.infoContributors = infoContributorsProvider.getIfAvailable();
104104
this.publicMetrics = publicMetricsProvider.getIfAvailable();
105105
this.traceRepository = traceRepositoryProvider.getIfAvailable();
@@ -117,9 +117,9 @@ public HealthEndpoint healthEndpoint() {
117117
return new HealthEndpoint(
118118
this.healthAggregator == null ? new OrderedHealthAggregator()
119119
: this.healthAggregator,
120-
this.healthIndicators == null
121-
? Collections.<String, HealthIndicator>emptyMap()
122-
: this.healthIndicators);
120+
this.healthIndicatorRegistry == null
121+
? new DefaultHealthIndicatorRegistry()
122+
: this.healthIndicatorRegistry);
123123
}
124124

125125
@Bean

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

+16
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@
3333
import org.springframework.boot.actuate.health.CassandraHealthIndicator;
3434
import org.springframework.boot.actuate.health.CouchbaseHealthIndicator;
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.HealthAggregator;
3940
import org.springframework.boot.actuate.health.HealthIndicator;
41+
import org.springframework.boot.actuate.health.HealthIndicatorRegistry;
4042
import org.springframework.boot.actuate.health.JmsHealthIndicator;
4143
import org.springframework.boot.actuate.health.MailHealthIndicator;
4244
import org.springframework.boot.actuate.health.MongoHealthIndicator;
@@ -86,6 +88,7 @@
8688
* @author Phillip Webb
8789
* @author Tommy Ludwig
8890
* @author Eddú Meléndez
91+
* @author Vedran Pavic
8992
* @since 1.1.0
9093
*/
9194
@Configuration
@@ -118,6 +121,19 @@ public OrderedHealthAggregator healthAggregator() {
118121
return healthAggregator;
119122
}
120123

124+
@Bean
125+
@ConditionalOnMissingBean(HealthIndicatorRegistry.class)
126+
public HealthIndicatorRegistry healthIndicatorRegistry(
127+
Map<String, HealthIndicator> healthIndicators) {
128+
HealthIndicatorRegistry registry = new DefaultHealthIndicatorRegistry();
129+
for (Map.Entry<String, HealthIndicator> entry : healthIndicators.entrySet()) {
130+
String name = entry.getKey();
131+
int index = name.toLowerCase().indexOf("healthindicator");
132+
registry.register(index > 0 ? name.substring(0, index) : name, entry.getValue());
133+
}
134+
return registry;
135+
}
136+
121137
@Bean
122138
@ConditionalOnMissingBean(HealthIndicator.class)
123139
public ApplicationHealthIndicator applicationHealthIndicator() {

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/HealthEndpoint.java

+14-25
Original file line numberDiff line numberDiff line change
@@ -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,14 @@
3130
* @author Dave Syer
3231
* @author Christian Dupuis
3332
* @author Andy Wilkinson
33+
* @author Vedran Pavic
3434
*/
3535
@ConfigurationProperties(prefix = "endpoints.health")
3636
public class HealthEndpoint extends AbstractEndpoint<Health> {
3737

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

4042
/**
4143
* Time to live for cached result, in milliseconds.
@@ -45,19 +47,16 @@ public class HealthEndpoint extends AbstractEndpoint<Health> {
4547
/**
4648
* Create a new {@link HealthIndicator} instance.
4749
* @param healthAggregator the health aggregator
48-
* @param healthIndicators the health indicators
50+
* @param healthIndicatorRegistry the health indicator registry
4951
*/
5052
public HealthEndpoint(HealthAggregator healthAggregator,
51-
Map<String, HealthIndicator> healthIndicators) {
53+
HealthIndicatorRegistry healthIndicatorRegistry) {
5254
super("health", false);
5355
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;
56+
Assert.notNull(healthIndicatorRegistry,
57+
"healthIndicatorRegistry must not be null");
58+
this.healthAggregator = healthAggregator;
59+
this.healthIndicatorRegistry = healthIndicatorRegistry;
6160
}
6261

6362
/**
@@ -78,19 +77,9 @@ public void setTimeToLive(long ttl) {
7877
*/
7978
@Override
8079
public Health invoke() {
81-
return this.healthIndicator.health();
80+
HealthIndicator indicator = new CompositeHealthIndicator(
81+
this.healthAggregator, this.healthIndicatorRegistry.getAll());
82+
return indicator.health();
8283
}
8384

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-
}
9685
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
23+
import org.springframework.util.Assert;
24+
25+
/**
26+
* Default implementation of {@link HealthIndicatorRegistry}.
27+
*
28+
* @author Vedran Pavic
29+
*/
30+
public class DefaultHealthIndicatorRegistry implements HealthIndicatorRegistry {
31+
32+
private final Map<String, HealthIndicator> healthIndicators =
33+
new HashMap<String, HealthIndicator>();
34+
35+
@Override
36+
public void register(String name, HealthIndicator healthIndicator) {
37+
Assert.notNull(healthIndicator, "HealthIndicator must not be null");
38+
synchronized (this.healthIndicators) {
39+
if (this.healthIndicators.get(name) != null) {
40+
throw new IllegalStateException(
41+
"HealthIndicator with name '" + name + "' already registered");
42+
}
43+
this.healthIndicators.put(name, healthIndicator);
44+
}
45+
}
46+
47+
@Override
48+
public HealthIndicator unregister(String name) {
49+
synchronized (this.healthIndicators) {
50+
return this.healthIndicators.remove(name);
51+
}
52+
}
53+
54+
@Override
55+
public HealthIndicator get(String name) {
56+
synchronized (this.healthIndicators) {
57+
return this.healthIndicators.get(name);
58+
}
59+
}
60+
61+
@Override
62+
public Map<String, HealthIndicator> getAll() {
63+
synchronized (this.healthIndicators) {
64+
return Collections.unmodifiableMap(
65+
new HashMap<String, HealthIndicator>(this.healthIndicators));
66+
}
67+
}
68+
69+
}
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

+10
Original file line numberDiff line numberDiff line change
@@ -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.ImportAutoConfiguration;
2830
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
@@ -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)