Skip to content

Commit e03f822

Browse files
philwebbsnicoll
andcommitted
Add support for health indicator groups
Update the `HealthEndpoint` to support health groups. The `HealthEndpointSettings` interface has been replaced with `HealthEndpointGroups` which provides access to the primary group as well as an optional set of additional groups. Groups can be configured via properties and may have custom `StatusAggregator` and `HttpCodeStatusMapper` settings. Closes gh-14022 Co-authored-by: Stephane Nicoll <[email protected]>
1 parent f09e026 commit e03f822

File tree

36 files changed

+1598
-366
lines changed

36 files changed

+1598
-366
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2012-2019 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+
* https://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.Collection;
20+
import java.util.Map;
21+
22+
import org.springframework.boot.actuate.health.DefaultHealthContributorRegistry;
23+
import org.springframework.boot.actuate.health.HealthContributor;
24+
import org.springframework.boot.actuate.health.HealthContributorRegistry;
25+
import org.springframework.util.Assert;
26+
27+
/**
28+
* An auto-configured {@link HealthContributorRegistry} that ensures registered indicators
29+
* do not clash with groups names.
30+
*
31+
* @author Phillip Webb
32+
*/
33+
class AutoConfiguredHealthContributorRegistry extends DefaultHealthContributorRegistry {
34+
35+
private final Collection<String> groupNames;
36+
37+
AutoConfiguredHealthContributorRegistry(Map<String, HealthContributor> contributors,
38+
Collection<String> groupNames) {
39+
super(contributors);
40+
this.groupNames = groupNames;
41+
contributors.keySet().forEach(this::assertDoesNotClashWithGroup);
42+
}
43+
44+
@Override
45+
public void registerContributor(String name, HealthContributor contributor) {
46+
assertDoesNotClashWithGroup(name);
47+
super.registerContributor(name, contributor);
48+
}
49+
50+
private void assertDoesNotClashWithGroup(String name) {
51+
Assert.state(!this.groupNames.contains(name),
52+
() -> "HealthContributor with name \"" + name + "\" clashes with group");
53+
}
54+
55+
}
Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,24 @@
1717
package org.springframework.boot.actuate.autoconfigure.health;
1818

1919
import java.util.Collection;
20+
import java.util.function.Predicate;
2021

21-
import org.springframework.boot.actuate.autoconfigure.health.HealthEndpointProperties.ShowDetails;
22+
import org.springframework.boot.actuate.autoconfigure.health.HealthProperties.ShowDetails;
2223
import org.springframework.boot.actuate.endpoint.SecurityContext;
23-
import org.springframework.boot.actuate.health.HealthEndpointSettings;
24+
import org.springframework.boot.actuate.health.HealthEndpointGroup;
2425
import org.springframework.boot.actuate.health.HttpCodeStatusMapper;
2526
import org.springframework.boot.actuate.health.StatusAggregator;
2627
import org.springframework.util.CollectionUtils;
2728

2829
/**
29-
* Auto-configured {@link HealthEndpointSettings} backed by
30-
* {@link HealthEndpointProperties}.
30+
* Auto-configured {@link HealthEndpointGroup} backed by {@link HealthProperties}.
3131
*
3232
* @author Phillip Webb
3333
* @author Andy Wilkinson
3434
*/
35-
class AutoConfiguredHealthEndpointSettings implements HealthEndpointSettings {
35+
class AutoConfiguredHealthEndpointGroup implements HealthEndpointGroup {
36+
37+
private final Predicate<String> members;
3638

3739
private final StatusAggregator statusAggregator;
3840

@@ -43,20 +45,27 @@ class AutoConfiguredHealthEndpointSettings implements HealthEndpointSettings {
4345
private final Collection<String> roles;
4446

4547
/**
46-
* Create a new {@link AutoConfiguredHealthEndpointSettings} instance.
48+
* Create a new {@link AutoConfiguredHealthEndpointGroup} instance.
49+
* @param members a predicate used to test for group membership
4750
* @param statusAggregator the status aggregator to use
4851
* @param httpCodeStatusMapper the HTTP code status mapper to use
4952
* @param showDetails the show details setting
5053
* @param roles the roles to match
5154
*/
52-
AutoConfiguredHealthEndpointSettings(StatusAggregator statusAggregator, HttpCodeStatusMapper httpCodeStatusMapper,
53-
ShowDetails showDetails, Collection<String> roles) {
55+
AutoConfiguredHealthEndpointGroup(Predicate<String> members, StatusAggregator statusAggregator,
56+
HttpCodeStatusMapper httpCodeStatusMapper, ShowDetails showDetails, Collection<String> roles) {
57+
this.members = members;
5458
this.statusAggregator = statusAggregator;
5559
this.httpCodeStatusMapper = httpCodeStatusMapper;
5660
this.showDetails = showDetails;
5761
this.roles = roles;
5862
}
5963

64+
@Override
65+
public boolean isMember(String name) {
66+
return this.members.test(name);
67+
}
68+
6069
@Override
6170
public boolean includeDetails(SecurityContext securityContext) {
6271
ShowDetails showDetails = this.showDetails;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/*
2+
* Copyright 2012-2019 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+
* https://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.ArrayList;
20+
import java.util.Collections;
21+
import java.util.LinkedHashMap;
22+
import java.util.List;
23+
import java.util.Map;
24+
import java.util.Set;
25+
import java.util.function.Predicate;
26+
import java.util.function.Supplier;
27+
28+
import org.springframework.beans.factory.BeanFactory;
29+
import org.springframework.beans.factory.BeanFactoryUtils;
30+
import org.springframework.beans.factory.ListableBeanFactory;
31+
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
32+
import org.springframework.beans.factory.annotation.BeanFactoryAnnotationUtils;
33+
import org.springframework.boot.actuate.autoconfigure.health.HealthEndpointProperties.Group;
34+
import org.springframework.boot.actuate.autoconfigure.health.HealthProperties.ShowDetails;
35+
import org.springframework.boot.actuate.autoconfigure.health.HealthProperties.Status;
36+
import org.springframework.boot.actuate.health.HealthEndpointGroup;
37+
import org.springframework.boot.actuate.health.HealthEndpointGroups;
38+
import org.springframework.boot.actuate.health.HttpCodeStatusMapper;
39+
import org.springframework.boot.actuate.health.SimpleHttpCodeStatusMapper;
40+
import org.springframework.boot.actuate.health.SimpleStatusAggregator;
41+
import org.springframework.boot.actuate.health.StatusAggregator;
42+
import org.springframework.context.ApplicationContext;
43+
import org.springframework.context.ConfigurableApplicationContext;
44+
import org.springframework.util.CollectionUtils;
45+
import org.springframework.util.ObjectUtils;
46+
47+
/**
48+
* Auto-configured {@link HealthEndpointGroups}.
49+
*
50+
* @author Phillip Webb
51+
*/
52+
class AutoConfiguredHealthEndpointGroups implements HealthEndpointGroups {
53+
54+
private static Predicate<String> ALL = (name) -> true;
55+
56+
private final HealthEndpointGroup primaryGroup;
57+
58+
private final Map<String, HealthEndpointGroup> groups;
59+
60+
/**
61+
* Create a new {@link AutoConfiguredHealthEndpointGroups} instance.
62+
* @param applicationContext the application context used to check for override beans
63+
* @param properties the health endpoint properties
64+
*/
65+
AutoConfiguredHealthEndpointGroups(ApplicationContext applicationContext, HealthEndpointProperties properties) {
66+
ListableBeanFactory beanFactory = (applicationContext instanceof ConfigurableApplicationContext)
67+
? ((ConfigurableApplicationContext) applicationContext).getBeanFactory() : applicationContext;
68+
ShowDetails showDetails = properties.getShowDetails();
69+
Set<String> roles = properties.getRoles();
70+
StatusAggregator statusAggregator = getNonQualifiedBean(beanFactory, StatusAggregator.class);
71+
if (statusAggregator == null) {
72+
statusAggregator = new SimpleStatusAggregator(properties.getStatus().getOrder());
73+
}
74+
HttpCodeStatusMapper httpCodeStatusMapper = getNonQualifiedBean(beanFactory, HttpCodeStatusMapper.class);
75+
if (httpCodeStatusMapper == null) {
76+
httpCodeStatusMapper = new SimpleHttpCodeStatusMapper(properties.getStatus().getHttpMapping());
77+
}
78+
this.primaryGroup = new AutoConfiguredHealthEndpointGroup(ALL, statusAggregator, httpCodeStatusMapper,
79+
showDetails, roles);
80+
this.groups = createGroups(properties.getGroup(), beanFactory, statusAggregator, httpCodeStatusMapper,
81+
showDetails, roles);
82+
}
83+
84+
private Map<String, HealthEndpointGroup> createGroups(Map<String, Group> groupProperties, BeanFactory beanFactory,
85+
StatusAggregator defaultStatusAggregator, HttpCodeStatusMapper defaultHttpCodeStatusMapper,
86+
ShowDetails defaultShowDetails, Set<String> defaultRoles) {
87+
Map<String, HealthEndpointGroup> groups = new LinkedHashMap<String, HealthEndpointGroup>();
88+
groupProperties.forEach((groupName, group) -> {
89+
Status status = group.getStatus();
90+
ShowDetails showDetails = (group.getShowDetails() != null) ? group.getShowDetails() : defaultShowDetails;
91+
Set<String> roles = !CollectionUtils.isEmpty(group.getRoles()) ? group.getRoles() : defaultRoles;
92+
StatusAggregator statusAggregator = getQualifiedBean(beanFactory, StatusAggregator.class, groupName, () -> {
93+
if (!CollectionUtils.isEmpty(status.getOrder())) {
94+
return new SimpleStatusAggregator(status.getOrder());
95+
}
96+
return defaultStatusAggregator;
97+
});
98+
HttpCodeStatusMapper httpCodeStatusMapper = getQualifiedBean(beanFactory, HttpCodeStatusMapper.class,
99+
groupName, () -> {
100+
if (!CollectionUtils.isEmpty(status.getHttpMapping())) {
101+
return new SimpleHttpCodeStatusMapper(status.getHttpMapping());
102+
}
103+
return defaultHttpCodeStatusMapper;
104+
});
105+
Predicate<String> members = new IncludeExcludeGroupMemberPredicate(group.getInclude(), group.getExclude());
106+
groups.put(groupName, new AutoConfiguredHealthEndpointGroup(members, statusAggregator, httpCodeStatusMapper,
107+
showDetails, roles));
108+
});
109+
return Collections.unmodifiableMap(groups);
110+
}
111+
112+
private <T> T getNonQualifiedBean(ListableBeanFactory beanFactory, Class<T> type) {
113+
List<String> candidates = new ArrayList<>();
114+
for (String beanName : BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, type)) {
115+
String[] aliases = beanFactory.getAliases(beanName);
116+
if (!BeanFactoryAnnotationUtils.isQualifierMatch(
117+
(qualifier) -> !qualifier.equals(beanName) && !ObjectUtils.containsElement(aliases, qualifier),
118+
beanName, beanFactory)) {
119+
candidates.add(beanName);
120+
}
121+
}
122+
if (candidates.isEmpty()) {
123+
return null;
124+
}
125+
if (candidates.size() == 1) {
126+
return beanFactory.getBean(candidates.get(0), type);
127+
}
128+
return beanFactory.getBean(type);
129+
}
130+
131+
private <T> T getQualifiedBean(BeanFactory beanFactory, Class<T> type, String qualifier, Supplier<T> fallback) {
132+
try {
133+
return BeanFactoryAnnotationUtils.qualifiedBeanOfType(beanFactory, type, qualifier);
134+
}
135+
catch (NoSuchBeanDefinitionException ex) {
136+
return fallback.get();
137+
}
138+
}
139+
140+
@Override
141+
public HealthEndpointGroup getPrimary() {
142+
return this.primaryGroup;
143+
}
144+
145+
@Override
146+
public Set<String> getNames() {
147+
return this.groups.keySet();
148+
}
149+
150+
@Override
151+
public HealthEndpointGroup get(String name) {
152+
return this.groups.get(name);
153+
}
154+
155+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2012-2019 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+
* https://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.Collection;
20+
import java.util.Map;
21+
22+
import org.springframework.boot.actuate.health.DefaultReactiveHealthContributorRegistry;
23+
import org.springframework.boot.actuate.health.HealthContributorRegistry;
24+
import org.springframework.boot.actuate.health.ReactiveHealthContributor;
25+
import org.springframework.util.Assert;
26+
27+
/**
28+
* An auto-configured {@link HealthContributorRegistry} that ensures registered indicators
29+
* do not clash with groups names.
30+
*
31+
* @author Phillip Webb
32+
*/
33+
class AutoConfiguredReactiveHealthContributorRegistry extends DefaultReactiveHealthContributorRegistry {
34+
35+
private final Collection<String> groupNames;
36+
37+
AutoConfiguredReactiveHealthContributorRegistry(Map<String, ReactiveHealthContributor> contributors,
38+
Collection<String> groupNames) {
39+
super(contributors);
40+
this.groupNames = groupNames;
41+
contributors.keySet().forEach(this::assertDoesNotClashWithGroup);
42+
}
43+
44+
@Override
45+
public void registerContributor(String name, ReactiveHealthContributor contributor) {
46+
assertDoesNotClashWithGroup(name);
47+
super.registerContributor(name, contributor);
48+
}
49+
50+
private void assertDoesNotClashWithGroup(String name) {
51+
Assert.state(!this.groupNames.contains(name),
52+
() -> "ReactiveHealthContributor with name \"" + name + "\" clashes with group");
53+
}
54+
55+
}

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

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,16 @@
1818

1919
import java.util.Map;
2020

21-
import org.springframework.beans.factory.ObjectProvider;
22-
import org.springframework.boot.actuate.health.DefaultHealthContributorRegistry;
2321
import org.springframework.boot.actuate.health.HealthContributor;
2422
import org.springframework.boot.actuate.health.HealthContributorRegistry;
2523
import org.springframework.boot.actuate.health.HealthEndpoint;
26-
import org.springframework.boot.actuate.health.HealthEndpointSettings;
24+
import org.springframework.boot.actuate.health.HealthEndpointGroups;
2725
import org.springframework.boot.actuate.health.HttpCodeStatusMapper;
2826
import org.springframework.boot.actuate.health.SimpleHttpCodeStatusMapper;
2927
import org.springframework.boot.actuate.health.SimpleStatusAggregator;
3028
import org.springframework.boot.actuate.health.StatusAggregator;
3129
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
30+
import org.springframework.context.ApplicationContext;
3231
import org.springframework.context.annotation.Bean;
3332
import org.springframework.context.annotation.Configuration;
3433

@@ -55,27 +54,22 @@ HttpCodeStatusMapper healthHttpCodeStatusMapper(HealthEndpointProperties propert
5554

5655
@Bean
5756
@ConditionalOnMissingBean
58-
HealthEndpointSettings healthEndpointSettings(HealthEndpointProperties properties,
59-
ObjectProvider<StatusAggregator> statusAggregatorProvider,
60-
ObjectProvider<HttpCodeStatusMapper> httpCodeStatusMapperProvider) {
61-
StatusAggregator statusAggregator = statusAggregatorProvider
62-
.getIfAvailable(() -> new SimpleStatusAggregator(properties.getStatus().getOrder()));
63-
HttpCodeStatusMapper httpCodeStatusMapper = httpCodeStatusMapperProvider
64-
.getIfAvailable(() -> new SimpleHttpCodeStatusMapper(properties.getStatus().getHttpMapping()));
65-
return new AutoConfiguredHealthEndpointSettings(statusAggregator, httpCodeStatusMapper,
66-
properties.getShowDetails(), properties.getRoles());
57+
HealthEndpointGroups healthEndpointGroups(ApplicationContext applicationContext,
58+
HealthEndpointProperties properties) {
59+
return new AutoConfiguredHealthEndpointGroups(applicationContext, properties);
6760
}
6861

6962
@Bean
7063
@ConditionalOnMissingBean
71-
HealthContributorRegistry healthContributorRegistry(Map<String, HealthContributor> healthContributors) {
72-
return new DefaultHealthContributorRegistry(healthContributors);
64+
HealthContributorRegistry healthContributorRegistry(Map<String, HealthContributor> healthContributors,
65+
HealthEndpointGroups groups) {
66+
return new AutoConfiguredHealthContributorRegistry(healthContributors, groups.getNames());
7367
}
7468

7569
@Bean
7670
@ConditionalOnMissingBean
77-
HealthEndpoint healthEndpoint(HealthContributorRegistry registry, HealthEndpointSettings settings) {
78-
return new HealthEndpoint(registry, settings);
71+
HealthEndpoint healthEndpoint(HealthContributorRegistry registry, HealthEndpointGroups groups) {
72+
return new HealthEndpoint(registry, groups);
7973
}
8074

8175
}

0 commit comments

Comments
 (0)