Skip to content

Commit 8364464

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 58740d7 commit 8364464

File tree

11 files changed

+314
-48
lines changed

11 files changed

+314
-48
lines changed

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

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 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.
@@ -43,8 +43,9 @@
4343
import org.springframework.boot.actuate.endpoint.RequestMappingEndpoint;
4444
import org.springframework.boot.actuate.endpoint.ShutdownEndpoint;
4545
import org.springframework.boot.actuate.endpoint.TraceEndpoint;
46+
import org.springframework.boot.actuate.health.DefaultHealthIndicatorRegistry;
4647
import org.springframework.boot.actuate.health.HealthAggregator;
47-
import org.springframework.boot.actuate.health.HealthIndicator;
48+
import org.springframework.boot.actuate.health.HealthIndicatorRegistry;
4849
import org.springframework.boot.actuate.health.OrderedHealthAggregator;
4950
import org.springframework.boot.actuate.info.InfoContributor;
5051
import org.springframework.boot.actuate.trace.InMemoryTraceRepository;
@@ -77,6 +78,7 @@
7778
* @author Eddú Meléndez
7879
* @author Meang Akira Tanaka
7980
* @author Ben Hale
81+
* @author Vedran Pavic
8082
*/
8183
@Configuration
8284
@AutoConfigureAfter({ FlywayAutoConfiguration.class, LiquibaseAutoConfiguration.class })
@@ -85,7 +87,7 @@ public class EndpointAutoConfiguration {
8587

8688
private final HealthAggregator healthAggregator;
8789

88-
private final Map<String, HealthIndicator> healthIndicators;
90+
private final HealthIndicatorRegistry healthIndicatorRegistry;
8991

9092
private final List<InfoContributor> infoContributors;
9193

@@ -94,12 +96,12 @@ public class EndpointAutoConfiguration {
9496
private final TraceRepository traceRepository;
9597

9698
public EndpointAutoConfiguration(ObjectProvider<HealthAggregator> healthAggregator,
97-
ObjectProvider<Map<String, HealthIndicator>> healthIndicators,
99+
ObjectProvider<HealthIndicatorRegistry> healthIndicatorRegistry,
98100
ObjectProvider<List<InfoContributor>> infoContributors,
99101
ObjectProvider<Collection<PublicMetrics>> publicMetrics,
100102
ObjectProvider<TraceRepository> traceRepository) {
101103
this.healthAggregator = healthAggregator.getIfAvailable();
102-
this.healthIndicators = healthIndicators.getIfAvailable();
104+
this.healthIndicatorRegistry = healthIndicatorRegistry.getIfAvailable();
103105
this.infoContributors = infoContributors.getIfAvailable();
104106
this.publicMetrics = publicMetrics.getIfAvailable();
105107
this.traceRepository = traceRepository.getIfAvailable();
@@ -117,9 +119,9 @@ public HealthEndpoint healthEndpoint() {
117119
return new HealthEndpoint(
118120
this.healthAggregator == null ? new OrderedHealthAggregator()
119121
: this.healthAggregator,
120-
this.healthIndicators == null
121-
? Collections.<String, HealthIndicator>emptyMap()
122-
: this.healthIndicators);
122+
this.healthIndicatorRegistry == null
123+
? new DefaultHealthIndicatorRegistry()
124+
: this.healthIndicatorRegistry);
123125
}
124126

125127
@Bean

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

+16
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@
3434
import org.springframework.boot.actuate.health.CassandraHealthIndicator;
3535
import org.springframework.boot.actuate.health.CouchbaseHealthIndicator;
3636
import org.springframework.boot.actuate.health.DataSourceHealthIndicator;
37+
import org.springframework.boot.actuate.health.DefaultHealthIndicatorRegistry;
3738
import org.springframework.boot.actuate.health.DiskSpaceHealthIndicator;
3839
import org.springframework.boot.actuate.health.DiskSpaceHealthIndicatorProperties;
3940
import org.springframework.boot.actuate.health.HealthAggregator;
4041
import org.springframework.boot.actuate.health.HealthIndicator;
42+
import org.springframework.boot.actuate.health.HealthIndicatorRegistry;
4143
import org.springframework.boot.actuate.health.JmsHealthIndicator;
4244
import org.springframework.boot.actuate.health.LdapHealthIndicator;
4345
import org.springframework.boot.actuate.health.MailHealthIndicator;
@@ -91,6 +93,7 @@
9193
* @author Phillip Webb
9294
* @author Tommy Ludwig
9395
* @author Eddú Meléndez
96+
* @author Vedran Pavic
9497
* @since 1.1.0
9598
*/
9699
@Configuration
@@ -125,6 +128,19 @@ public OrderedHealthAggregator healthAggregator() {
125128
return healthAggregator;
126129
}
127130

131+
@Bean
132+
@ConditionalOnMissingBean(HealthIndicatorRegistry.class)
133+
public HealthIndicatorRegistry healthIndicatorRegistry(
134+
Map<String, HealthIndicator> healthIndicators) {
135+
HealthIndicatorRegistry registry = new DefaultHealthIndicatorRegistry();
136+
for (Map.Entry<String, HealthIndicator> entry : healthIndicators.entrySet()) {
137+
String name = entry.getKey();
138+
int index = name.toLowerCase().indexOf("healthindicator");
139+
registry.register(index > 0 ? name.substring(0, index) : name, entry.getValue());
140+
}
141+
return registry;
142+
}
143+
128144
@Bean
129145
@ConditionalOnMissingBean(HealthIndicator.class)
130146
public ApplicationHealthIndicator applicationHealthIndicator() {
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 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,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 HealthEndpoint} 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,20 +77,9 @@ public void setTimeToLive(long ttl) {
7877
*/
7978
@Override
8079
public Health invoke() {
81-
return this.healthIndicator.health();
82-
}
83-
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;
80+
HealthIndicator indicator = new CompositeHealthIndicator(
81+
this.healthAggregator, this.healthIndicatorRegistry.getAll());
82+
return indicator.health();
9583
}
9684

9785
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2012-2017 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+
* @since 2.0.0
30+
*/
31+
public class DefaultHealthIndicatorRegistry implements HealthIndicatorRegistry {
32+
33+
private final Map<String, HealthIndicator> healthIndicators = new HashMap<>();
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(new HashMap<>(this.healthIndicators));
65+
}
66+
}
67+
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2012-2017 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+
* @since 2.0.0
29+
*/
30+
public interface HealthIndicatorRegistry {
31+
32+
/**
33+
* Registers the given {@code healthIndicator}, associating it with the given
34+
* {@code name}.
35+
* @param name the name of the indicator
36+
* @param healthIndicator the indicator
37+
* @throws IllegalStateException if an indicator with the given {@code name} is
38+
* already registered.
39+
*/
40+
void register(String name, HealthIndicator healthIndicator);
41+
42+
/**
43+
* Unregisters the {@code HealthIndicator} previously registered with the given
44+
* {@code name}.
45+
* @param name the name of the indicator
46+
* @return the unregistered indicator, or {@code null} if no indicator was found in
47+
* the registry for the given {@code name}.
48+
*/
49+
HealthIndicator unregister(String name);
50+
51+
/**
52+
* Returns the health indicator registered with the given {@code name}.
53+
* @param name the name of the indicator
54+
* @return the health indicator, or {@code null} if no indicator was registered with
55+
* the given {@code name}.
56+
*/
57+
HealthIndicator get(String name);
58+
59+
/**
60+
* Returns a snapshot of the registered health indicators and their names. The
61+
* contents of the map do not reflect subsequent changes to the registry.
62+
* @return the snapshot of registered health indicators
63+
*/
64+
Map<String, HealthIndicator> getAll();
65+
66+
}

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-2016 the original author or authors.
2+
* Copyright 2012-2017 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.ImportAutoConfiguration;
2830
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
@@ -43,6 +45,7 @@
4345
*
4446
* @author Dave Syer
4547
* @author Andy Wilkinson
48+
* @author Vedran Pavic
4649
*/
4750
public class HealthMvcEndpointAutoConfigurationTests {
4851

@@ -96,6 +99,13 @@ public TestHealthIndicator testHealthIndicator() {
9699
return new TestHealthIndicator();
97100
}
98101

102+
@Bean
103+
public HealthIndicatorRegistry healthIndicatorRegistry() {
104+
DefaultHealthIndicatorRegistry registry = new DefaultHealthIndicatorRegistry();
105+
registry.register("test", testHealthIndicator());
106+
return registry;
107+
}
108+
99109
}
100110

101111
static class TestHealthIndicator extends AbstractHealthIndicator {

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/cloudfoundry/CloudFoundryEndpointHandlerMappingTests.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 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.
@@ -28,7 +28,7 @@
2828
import org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint;
2929
import org.springframework.boot.actuate.endpoint.mvc.ManagementServletContext;
3030
import org.springframework.boot.actuate.endpoint.mvc.NamedMvcEndpoint;
31-
import org.springframework.boot.actuate.health.HealthIndicator;
31+
import org.springframework.boot.actuate.health.DefaultHealthIndicatorRegistry;
3232
import org.springframework.boot.actuate.health.OrderedHealthAggregator;
3333
import org.springframework.context.support.StaticApplicationContext;
3434
import org.springframework.mock.web.MockHttpServletRequest;
@@ -81,7 +81,7 @@ public void registersCloudFoundryDiscoveryEndpoint() throws Exception {
8181
public void registersCloudFoundryHealthEndpoint() throws Exception {
8282
StaticApplicationContext context = new StaticApplicationContext();
8383
HealthEndpoint delegate = new HealthEndpoint(new OrderedHealthAggregator(),
84-
Collections.<String, HealthIndicator>emptyMap());
84+
new DefaultHealthIndicatorRegistry());
8585
CloudFoundryEndpointHandlerMapping handlerMapping = new CloudFoundryEndpointHandlerMapping(
8686
Collections.singleton(new TestHealthMvcEndpoint(delegate)), null, null);
8787
handlerMapping.setPrefix("/test");

0 commit comments

Comments
 (0)