Skip to content

Commit 6023907

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 0fd7739 commit 6023907

File tree

9 files changed

+299
-41
lines changed

9 files changed

+299
-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;
@@ -74,7 +74,7 @@
7474
* @author Stephane Nicoll
7575
* @author Eddú Meléndez
7676
* @author Meang Akira Tanaka
77-
*
77+
* @author Vedran Pavic
7878
*/
7979
@Configuration
8080
@AutoConfigureAfter({ FlywayAutoConfiguration.class, LiquibaseAutoConfiguration.class })
@@ -83,7 +83,7 @@ public class EndpointAutoConfiguration {
8383

8484
private final HealthAggregator healthAggregator;
8585

86-
private final Map<String, HealthIndicator> healthIndicators;
86+
private final HealthIndicatorRegistry healthIndicatorRegistry;
8787

8888
private final List<InfoContributor> infoContributors;
8989

@@ -93,12 +93,12 @@ public class EndpointAutoConfiguration {
9393

9494
public EndpointAutoConfiguration(
9595
ObjectProvider<HealthAggregator> healthAggregatorProvider,
96-
ObjectProvider<Map<String, HealthIndicator>> healthIndicatorsProvider,
96+
ObjectProvider<HealthIndicatorRegistry> healthIndicatorRegistryProvider,
9797
ObjectProvider<List<InfoContributor>> infoContributorsProvider,
9898
ObjectProvider<Collection<PublicMetrics>> publicMetricsProvider,
9999
ObjectProvider<TraceRepository> traceRepositoryProvider) {
100100
this.healthAggregator = healthAggregatorProvider.getIfAvailable();
101-
this.healthIndicators = healthIndicatorsProvider.getIfAvailable();
101+
this.healthIndicatorRegistry = healthIndicatorRegistryProvider.getIfAvailable();
102102
this.infoContributors = infoContributorsProvider.getIfAvailable();
103103
this.publicMetrics = publicMetricsProvider.getIfAvailable();
104104
this.traceRepository = traceRepositoryProvider.getIfAvailable();
@@ -116,9 +116,9 @@ public HealthEndpoint healthEndpoint() {
116116
return new HealthEndpoint(
117117
this.healthAggregator == null ? new OrderedHealthAggregator()
118118
: this.healthAggregator,
119-
this.healthIndicators == null
120-
? Collections.<String, HealthIndicator>emptyMap()
121-
: this.healthIndicators);
119+
this.healthIndicatorRegistry == null
120+
? new DefaultHealthIndicatorRegistry()
121+
: this.healthIndicatorRegistry);
122122
}
123123

124124
@Bean

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

+16
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@
3636
import org.springframework.boot.actuate.health.CompositeHealthIndicator;
3737
import org.springframework.boot.actuate.health.CouchbaseHealthIndicator;
3838
import org.springframework.boot.actuate.health.DataSourceHealthIndicator;
39+
import org.springframework.boot.actuate.health.DefaultHealthIndicatorRegistry;
3940
import org.springframework.boot.actuate.health.DiskSpaceHealthIndicator;
4041
import org.springframework.boot.actuate.health.DiskSpaceHealthIndicatorProperties;
4142
import org.springframework.boot.actuate.health.ElasticsearchHealthIndicator;
4243
import org.springframework.boot.actuate.health.ElasticsearchHealthIndicatorProperties;
4344
import org.springframework.boot.actuate.health.HealthAggregator;
4445
import org.springframework.boot.actuate.health.HealthIndicator;
46+
import org.springframework.boot.actuate.health.HealthIndicatorRegistry;
4547
import org.springframework.boot.actuate.health.JmsHealthIndicator;
4648
import org.springframework.boot.actuate.health.MailHealthIndicator;
4749
import org.springframework.boot.actuate.health.MongoHealthIndicator;
@@ -90,6 +92,7 @@
9092
* @author Phillip Webb
9193
* @author Tommy Ludwig
9294
* @author Eddú Meléndez
95+
* @author Vedran Pavic
9396
* @since 1.1.0
9497
*/
9598
@Configuration
@@ -120,6 +123,19 @@ public OrderedHealthAggregator healthAggregator() {
120123
return healthAggregator;
121124
}
122125

126+
@Bean
127+
@ConditionalOnMissingBean(HealthIndicatorRegistry.class)
128+
public HealthIndicatorRegistry healthIndicatorRegistry(
129+
Map<String, HealthIndicator> healthIndicators) {
130+
HealthIndicatorRegistry registry = new DefaultHealthIndicatorRegistry();
131+
for (Map.Entry<String, HealthIndicator> entry : healthIndicators.entrySet()) {
132+
String name = entry.getKey();
133+
int index = name.toLowerCase().indexOf("healthindicator");
134+
registry.register(index > 0 ? name.substring(0, index) : name, entry.getValue());
135+
}
136+
return registry;
137+
}
138+
123139
@Bean
124140
@ConditionalOnMissingBean(HealthIndicator.class)
125141
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)