Skip to content

Commit 95b2515

Browse files
snicollwilkinsona
andcommitted
Polish "Introduce HealthIndicatorRegistry"
See gh-4965 Co-authored-by: Andy Wilkinson <[email protected]>
1 parent d829d52 commit 95b2515

File tree

20 files changed

+354
-222
lines changed

20 files changed

+354
-222
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/health/CompositeHealthIndicatorConfiguration.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020

2121
import org.springframework.beans.factory.annotation.Autowired;
2222
import org.springframework.boot.actuate.health.CompositeHealthIndicator;
23+
import org.springframework.boot.actuate.health.DefaultHealthIndicatorRegistry;
2324
import org.springframework.boot.actuate.health.HealthAggregator;
2425
import org.springframework.boot.actuate.health.HealthIndicator;
26+
import org.springframework.boot.actuate.health.HealthIndicatorRegistry;
2527
import org.springframework.core.ResolvableType;
2628

2729
/**
@@ -42,11 +44,10 @@ protected HealthIndicator createHealthIndicator(Map<String, S> beans) {
4244
if (beans.size() == 1) {
4345
return createHealthIndicator(beans.values().iterator().next());
4446
}
45-
CompositeHealthIndicator composite = new CompositeHealthIndicator(
46-
this.healthAggregator);
47-
beans.forEach((name, source) -> composite.addHealthIndicator(name,
48-
createHealthIndicator(source)));
49-
return composite;
47+
HealthIndicatorRegistry registry = new DefaultHealthIndicatorRegistry();
48+
beans.forEach(
49+
(name, source) -> registry.register(name, createHealthIndicator(source)));
50+
return new CompositeHealthIndicator(this.healthAggregator, registry);
5051
}
5152

5253
@SuppressWarnings("unchecked")

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.java

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,32 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.health;
1818

19-
import org.springframework.beans.factory.ObjectProvider;
2019
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
20+
import org.springframework.boot.actuate.health.CompositeHealthIndicator;
2121
import org.springframework.boot.actuate.health.HealthAggregator;
2222
import org.springframework.boot.actuate.health.HealthEndpoint;
2323
import org.springframework.boot.actuate.health.HealthIndicatorRegistry;
24-
import org.springframework.boot.actuate.health.OrderedHealthAggregator;
2524
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
25+
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
2626
import org.springframework.context.annotation.Bean;
2727
import org.springframework.context.annotation.Configuration;
2828

2929
/**
3030
* Configuration for {@link HealthEndpoint}.
3131
*
3232
* @author Stephane Nicoll
33-
* @author Vedran Pavic
3433
*/
3534
@Configuration
35+
@ConditionalOnSingleCandidate(HealthIndicatorRegistry.class)
3636
class HealthEndpointConfiguration {
3737

38-
private final HealthAggregator healthAggregator;
39-
40-
private final HealthIndicatorRegistry healthIndicatorRegistry;
41-
42-
HealthEndpointConfiguration(ObjectProvider<HealthAggregator> healthAggregator,
43-
ObjectProvider<HealthIndicatorRegistry> healthIndicatorRegistry) {
44-
this.healthAggregator = healthAggregator
45-
.getIfAvailable(OrderedHealthAggregator::new);
46-
this.healthIndicatorRegistry = healthIndicatorRegistry.getObject();
47-
}
48-
4938
@Bean
5039
@ConditionalOnMissingBean
5140
@ConditionalOnEnabledEndpoint
52-
public HealthEndpoint healthEndpoint() {
53-
return new HealthEndpoint(this.healthAggregator, this.healthIndicatorRegistry);
41+
public HealthEndpoint healthEndpoint(HealthAggregator healthAggregator,
42+
HealthIndicatorRegistry registry) {
43+
return new HealthEndpoint(
44+
new CompositeHealthIndicator(healthAggregator, registry));
5445
}
5546

5647
}

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

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.health;
1818

19-
import java.util.LinkedHashMap;
20-
import java.util.Map;
21-
2219
import org.springframework.boot.actuate.health.ApplicationHealthIndicator;
23-
import org.springframework.boot.actuate.health.DefaultHealthIndicatorRegistry;
2420
import org.springframework.boot.actuate.health.HealthAggregator;
2521
import org.springframework.boot.actuate.health.HealthIndicator;
2622
import org.springframework.boot.actuate.health.HealthIndicatorRegistry;
@@ -32,7 +28,6 @@
3228
import org.springframework.context.ApplicationContext;
3329
import org.springframework.context.annotation.Bean;
3430
import org.springframework.context.annotation.Configuration;
35-
import org.springframework.util.ClassUtils;
3631

3732
/**
3833
* {@link EnableAutoConfiguration Auto-configuration} for {@link HealthIndicator}s.
@@ -73,30 +68,7 @@ public OrderedHealthAggregator healthAggregator() {
7368
@ConditionalOnMissingBean(HealthIndicatorRegistry.class)
7469
public HealthIndicatorRegistry healthIndicatorRegistry(
7570
ApplicationContext applicationContext) {
76-
HealthIndicatorRegistry registry = new DefaultHealthIndicatorRegistry();
77-
Map<String, HealthIndicator> indicators = new LinkedHashMap<>();
78-
indicators.putAll(applicationContext.getBeansOfType(HealthIndicator.class));
79-
if (ClassUtils.isPresent("reactor.core.publisher.Flux", null)) {
80-
new ReactiveHealthIndicators().get(applicationContext)
81-
.forEach(indicators::putIfAbsent);
82-
}
83-
indicators.forEach(registry::register);
84-
return registry;
85-
}
86-
87-
private static class ReactiveHealthIndicators {
88-
89-
public Map<String, HealthIndicator> get(ApplicationContext applicationContext) {
90-
Map<String, HealthIndicator> indicators = new LinkedHashMap<>();
91-
applicationContext.getBeansOfType(ReactiveHealthIndicator.class)
92-
.forEach((name, indicator) -> indicators.put(name, adapt(indicator)));
93-
return indicators;
94-
}
95-
96-
private HealthIndicator adapt(ReactiveHealthIndicator indicator) {
97-
return () -> indicator.health().block();
98-
}
99-
71+
return HealthIndicatorRegistryBeans.get(applicationContext);
10072
}
10173

10274
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2012-2018 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.autoconfigure.health;
18+
19+
import java.util.LinkedHashMap;
20+
import java.util.Map;
21+
22+
import org.springframework.boot.actuate.health.HealthIndicator;
23+
import org.springframework.boot.actuate.health.HealthIndicatorRegistry;
24+
import org.springframework.boot.actuate.health.HealthIndicatorRegistryFactory;
25+
import org.springframework.boot.actuate.health.ReactiveHealthIndicator;
26+
import org.springframework.context.ApplicationContext;
27+
import org.springframework.util.ClassUtils;
28+
29+
/**
30+
* Creates a {@link HealthIndicatorRegistry} from beans in the {@link ApplicationContext}.
31+
*
32+
* @author Phillip Webb
33+
* @author Stephane Nicoll
34+
*/
35+
final class HealthIndicatorRegistryBeans {
36+
37+
private HealthIndicatorRegistryBeans() {
38+
}
39+
40+
public static HealthIndicatorRegistry get(ApplicationContext applicationContext) {
41+
Map<String, HealthIndicator> indicators = new LinkedHashMap<>();
42+
indicators.putAll(applicationContext.getBeansOfType(HealthIndicator.class));
43+
if (ClassUtils.isPresent("reactor.core.publisher.Flux", null)) {
44+
new ReactiveHealthIndicators().get(applicationContext)
45+
.forEach(indicators::putIfAbsent);
46+
}
47+
HealthIndicatorRegistryFactory factory = new HealthIndicatorRegistryFactory();
48+
return factory.createHealthIndicatorRegistry(indicators);
49+
}
50+
51+
private static class ReactiveHealthIndicators {
52+
53+
public Map<String, HealthIndicator> get(ApplicationContext applicationContext) {
54+
Map<String, HealthIndicator> indicators = new LinkedHashMap<>();
55+
applicationContext.getBeansOfType(ReactiveHealthIndicator.class)
56+
.forEach((name, indicator) -> indicators.put(name, adapt(indicator)));
57+
return indicators;
58+
}
59+
60+
private HealthIndicator adapt(ReactiveHealthIndicator indicator) {
61+
return () -> indicator.health().block();
62+
}
63+
64+
}
65+
66+
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/CloudFoundryWebEndpointDiscovererTests.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@
3434
import org.springframework.boot.actuate.endpoint.web.PathMapper;
3535
import org.springframework.boot.actuate.endpoint.web.WebOperation;
3636
import org.springframework.boot.actuate.endpoint.web.annotation.EndpointWebExtension;
37-
import org.springframework.boot.actuate.health.HealthAggregator;
3837
import org.springframework.boot.actuate.health.HealthEndpoint;
39-
import org.springframework.boot.actuate.health.HealthIndicatorRegistry;
38+
import org.springframework.boot.actuate.health.HealthIndicator;
4039
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
4140
import org.springframework.context.annotation.Bean;
4241
import org.springframework.context.annotation.Configuration;
@@ -110,8 +109,7 @@ public TestEndpointWebExtension testEndpointWebExtension() {
110109

111110
@Bean
112111
public HealthEndpoint healthEndpoint() {
113-
return new HealthEndpoint(mock(HealthAggregator.class),
114-
mock(HealthIndicatorRegistry.class));
112+
return new HealthEndpoint(mock(HealthIndicator.class));
115113
}
116114

117115
@Bean

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/documentation/HealthEndpointDocumentationTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323

2424
import org.junit.Test;
2525

26-
import org.springframework.boot.actuate.health.DefaultHealthIndicatorRegistry;
26+
import org.springframework.boot.actuate.health.CompositeHealthIndicator;
2727
import org.springframework.boot.actuate.health.HealthEndpoint;
2828
import org.springframework.boot.actuate.health.HealthIndicator;
29-
import org.springframework.boot.actuate.health.HealthIndicatorRegistry;
29+
import org.springframework.boot.actuate.health.HealthIndicatorRegistryFactory;
3030
import org.springframework.boot.actuate.health.OrderedHealthAggregator;
3131
import org.springframework.boot.actuate.jdbc.DataSourceHealthIndicator;
3232
import org.springframework.boot.actuate.system.DiskSpaceHealthIndicator;
@@ -73,9 +73,9 @@ static class TestConfiguration {
7373

7474
@Bean
7575
public HealthEndpoint endpoint(Map<String, HealthIndicator> healthIndicators) {
76-
HealthIndicatorRegistry registry = new DefaultHealthIndicatorRegistry();
77-
healthIndicators.forEach(registry::register);
78-
return new HealthEndpoint(new OrderedHealthAggregator(), registry);
76+
return new HealthEndpoint(new CompositeHealthIndicator(
77+
new OrderedHealthAggregator(), new HealthIndicatorRegistryFactory()
78+
.createHealthIndicatorRegistry(healthIndicators)));
7979
}
8080

8181
@Bean

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/integrationtest/JmxEndpointIntegrationTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ public class JmxEndpointIntegrationTests {
4848
private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
4949
.withConfiguration(AutoConfigurations.of(JmxAutoConfiguration.class,
5050
EndpointAutoConfiguration.class, JmxEndpointAutoConfiguration.class,
51-
HttpTraceAutoConfiguration.class, HealthIndicatorAutoConfiguration.class))
51+
HealthIndicatorAutoConfiguration.class,
52+
HttpTraceAutoConfiguration.class))
5253
.withConfiguration(
5354
AutoConfigurations.of(EndpointAutoConfigurationClasses.ALL));
5455

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 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,8 +19,6 @@
1919
import java.util.LinkedHashMap;
2020
import java.util.Map;
2121

22-
import org.springframework.util.Assert;
23-
2422
/**
2523
* {@link HealthIndicator} that returns health indications from all registered delegates.
2624
*
@@ -31,43 +29,71 @@
3129
*/
3230
public class CompositeHealthIndicator implements HealthIndicator {
3331

34-
private final Map<String, HealthIndicator> indicators;
32+
private final HealthIndicatorRegistry registry;
3533

36-
private final HealthAggregator healthAggregator;
34+
private final HealthAggregator aggregator;
3735

3836
/**
3937
* Create a new {@link CompositeHealthIndicator}.
4038
* @param healthAggregator the health aggregator
39+
* @deprecated since 2.1.0 in favour of
40+
* {@link #CompositeHealthIndicator(HealthAggregator, HealthIndicatorRegistry)}
4141
*/
42+
@Deprecated
4243
public CompositeHealthIndicator(HealthAggregator healthAggregator) {
43-
this(healthAggregator, new LinkedHashMap<>());
44+
this(healthAggregator, new DefaultHealthIndicatorRegistry());
4445
}
4546

4647
/**
47-
* Create a new {@link CompositeHealthIndicator} from the specified indicators.
48+
* Create a new {@link CompositeHealthIndicator} from the specified
49+
* indicators.
4850
* @param healthAggregator the health aggregator
49-
* @param indicators a map of {@link HealthIndicator}s with the key being used as an
50-
* indicator name.
51+
* @param indicators a map of {@link HealthIndicator HealthIndicators} with
52+
* the key being used as an indicator name.
53+
* @deprecated since 2.1.0 in favour of
54+
* {@link #CompositeHealthIndicator(HealthAggregator, HealthIndicatorRegistry)}
5155
*/
56+
@Deprecated
5257
public CompositeHealthIndicator(HealthAggregator healthAggregator,
5358
Map<String, HealthIndicator> indicators) {
54-
Assert.notNull(healthAggregator, "HealthAggregator must not be null");
55-
Assert.notNull(indicators, "Indicators must not be null");
56-
this.indicators = new LinkedHashMap<>(indicators);
57-
this.healthAggregator = healthAggregator;
59+
this(healthAggregator, new DefaultHealthIndicatorRegistry(indicators));
5860
}
5961

62+
/**
63+
* Create a new {@link CompositeHealthIndicator} from the indicators in the
64+
* given {@code registry}.
65+
* @param healthAggregator the health aggregator
66+
* @param registry the registry of {@link HealthIndicator HealthIndicators}.
67+
*/
68+
public CompositeHealthIndicator(HealthAggregator healthAggregator,
69+
HealthIndicatorRegistry registry) {
70+
this.aggregator = healthAggregator;
71+
this.registry = registry;
72+
}
73+
74+
/**
75+
* Adds the given {@code healthIndicator}, associating it with the given
76+
* {@code name}.
77+
* @param name the name of the indicator
78+
* @param indicator the indicator
79+
* @throws IllegalStateException if an indicator with the given {@code name}
80+
* is already registered.
81+
* @deprecated since 2.1.0 in favour of
82+
* {@link HealthIndicatorRegistry#register(String, HealthIndicator)}
83+
*/
84+
@Deprecated
6085
public void addHealthIndicator(String name, HealthIndicator indicator) {
61-
this.indicators.put(name, indicator);
86+
this.registry.register(name, indicator);
6287
}
6388

6489
@Override
6590
public Health health() {
6691
Map<String, Health> healths = new LinkedHashMap<>();
67-
for (Map.Entry<String, HealthIndicator> entry : this.indicators.entrySet()) {
92+
for (Map.Entry<String, HealthIndicator> entry : this.registry.getAll()
93+
.entrySet()) {
6894
healths.put(entry.getKey(), entry.getValue().health());
6995
}
70-
return this.healthAggregator.aggregate(healths);
96+
return this.aggregator.aggregate(healths);
7197
}
7298

7399
}

0 commit comments

Comments
 (0)