Skip to content

Commit

Permalink
Add support for health indicator groups
Browse files Browse the repository at this point in the history
This commit allows a user to define an arbitrary number of health
indicator groups using configuration. If a given health indicator is
defined in more than one group, a single invocation is ensured using
a AggregateHealth. A reactive counter-part is also provided.

See spring-projectsgh-14022
  • Loading branch information
snicoll committed May 13, 2019
1 parent 6544d19 commit 9b2527e
Show file tree
Hide file tree
Showing 21 changed files with 1,242 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnExposedEndpoint;
import org.springframework.boot.actuate.health.CompositeHealthIndicator;
import org.springframework.boot.actuate.health.HealthAggregator;
import org.springframework.boot.actuate.health.HealthEndpoint;
import org.springframework.boot.actuate.health.HealthIndicatorRegistry;
import org.springframework.boot.actuate.health.OverallHealthIndicator;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.context.annotation.Bean;
Expand All @@ -42,8 +42,7 @@ class HealthEndpointConfiguration {
@ConditionalOnMissingBean
public HealthEndpoint healthEndpoint(HealthAggregator healthAggregator,
HealthIndicatorRegistry registry) {
return new HealthEndpoint(
new CompositeHealthIndicator(healthAggregator, registry));
return new HealthEndpoint(new OverallHealthIndicator(healthAggregator, registry));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@

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

import java.util.Map;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnExposedEndpoint;
import org.springframework.boot.actuate.health.CompositeReactiveHealthIndicator;
import org.springframework.boot.actuate.health.HealthAggregator;
import org.springframework.boot.actuate.health.HealthEndpoint;
import org.springframework.boot.actuate.health.HealthEndpointWebExtension;
import org.springframework.boot.actuate.health.HealthStatusHttpMapper;
import org.springframework.boot.actuate.health.HealthWebEndpointResponseMapper;
import org.springframework.boot.actuate.health.OrderedHealthAggregator;
import org.springframework.boot.actuate.health.OverallReactiveHealthIndicator;
import org.springframework.boot.actuate.health.ReactiveHealthEndpointWebExtension;
import org.springframework.boot.actuate.health.ReactiveHealthIndicatorRegistry;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
Expand All @@ -51,10 +53,11 @@ class HealthEndpointWebExtensionConfiguration {
@Bean
@ConditionalOnMissingBean
public HealthStatusHttpMapper createHealthStatusHttpMapper(
HealthIndicatorProperties healthIndicatorProperties) {
HealthIndicatorProperties properties) {
HealthStatusHttpMapper statusHttpMapper = new HealthStatusHttpMapper();
if (healthIndicatorProperties.getHttpMapping() != null) {
statusHttpMapper.addStatusMapping(healthIndicatorProperties.getHttpMapping());
Map<String, Integer> httpMapping = properties.getStatus().getHttpMapping();
if (httpMapping != null) {
statusHttpMapper.addStatusMapping(httpMapping);
}
return statusHttpMapper;
}
Expand All @@ -81,7 +84,7 @@ public ReactiveHealthEndpointWebExtension reactiveHealthEndpointWebExtension(
ReactiveHealthIndicatorRegistry registry,
HealthWebEndpointResponseMapper responseMapper) {
return new ReactiveHealthEndpointWebExtension(
new CompositeReactiveHealthIndicator(
new OverallReactiveHealthIndicator(
healthAggregator.getIfAvailable(OrderedHealthAggregator::new),
registry),
responseMapper);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@

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

import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;

import reactor.core.publisher.Flux;

import org.springframework.boot.actuate.health.ApplicationHealthIndicator;
import org.springframework.boot.actuate.health.GroupHealthIndicator;
import org.springframework.boot.actuate.health.GroupReactiveHealthIndicator;
import org.springframework.boot.actuate.health.HealthAggregator;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.actuate.health.HealthIndicatorRegistry;
Expand Down Expand Up @@ -60,17 +66,46 @@ public ApplicationHealthIndicator applicationHealthIndicator() {
public OrderedHealthAggregator healthAggregator(
HealthIndicatorProperties properties) {
OrderedHealthAggregator healthAggregator = new OrderedHealthAggregator();
if (properties.getOrder() != null) {
healthAggregator.setStatusOrder(properties.getOrder());
if (properties.getStatus().getOrder() != null) {
healthAggregator.setStatusOrder(properties.getStatus().getOrder());
}
return healthAggregator;
}

@Bean
@ConditionalOnMissingBean(HealthIndicatorRegistry.class)
public HealthIndicatorRegistry healthIndicatorRegistry(
HealthIndicatorProperties properties, HealthAggregator healthAggregator,
ApplicationContext applicationContext) {
return HealthIndicatorRegistryBeans.get(applicationContext);
HealthIndicatorRegistry registry = HealthIndicatorRegistryBeans
.get(applicationContext);
extractGroups(properties, registry::get)
.forEach((groupName, groupHealthIndicators) -> registry
.register(groupName, new GroupHealthIndicator(healthAggregator,
registry, groupHealthIndicators)));
return registry;
}

private static <T> Map<String, Set<String>> extractGroups(
HealthIndicatorProperties properties,
Function<String, T> healthIndicatorByName) {
Map<String, Set<String>> groupDefinitions = new LinkedHashMap<>();
properties.getGroups().forEach((groupName, indicatorNames) -> {
if (healthIndicatorByName.apply(groupName) != null) {
throw new IllegalArgumentException(
"Could not register health indicator group named '" + groupName
+ "', an health indicator with that name is already registered");
}
Set<String> groupHealthIndicators = new LinkedHashSet<>();
indicatorNames.forEach((name) -> {
T healthIndicator = healthIndicatorByName.apply(name);
if (healthIndicator != null) {
groupHealthIndicators.add(name);
}
});
groupDefinitions.put(groupName, groupHealthIndicators);
});
return groupDefinitions;
}

@Configuration(proxyBeanMethods = false)
Expand All @@ -80,11 +115,17 @@ static class ReactiveHealthIndicatorConfiguration {
@Bean
@ConditionalOnMissingBean
public ReactiveHealthIndicatorRegistry reactiveHealthIndicatorRegistry(
HealthIndicatorProperties properties, HealthAggregator healthAggregator,
Map<String, ReactiveHealthIndicator> reactiveHealthIndicators,
Map<String, HealthIndicator> healthIndicators) {
return new ReactiveHealthIndicatorRegistryFactory()
ReactiveHealthIndicatorRegistry registry = new ReactiveHealthIndicatorRegistryFactory()
.createReactiveHealthIndicatorRegistry(reactiveHealthIndicators,
healthIndicators);
extractGroups(properties, registry::get).forEach(
(groupName, groupHealthIndicators) -> registry.register(groupName,
new GroupReactiveHealthIndicator(healthAggregator, registry,
groupHealthIndicators)));
return registry;
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,32 +28,52 @@
* @author Christian Dupuis
* @since 2.0.0
*/
@ConfigurationProperties(prefix = "management.health.status")
@ConfigurationProperties(prefix = "management.health")
public class HealthIndicatorProperties {

/**
* Comma-separated list of health statuses in order of severity.
* Health indicator groups. Each entry maps the name of a group with a list of health
* indicators to associate with the group.
*/
private List<String> order = null;
private final Map<String, List<String>> groups = new HashMap<>();

/**
* Mapping of health statuses to HTTP status codes. By default, registered health
* statuses map to sensible defaults (for example, UP maps to 200).
*/
private final Map<String, Integer> httpMapping = new HashMap<>();
private final Status status = new Status();

public List<String> getOrder() {
return this.order;
public Map<String, List<String>> getGroups() {
return this.groups;
}

public void setOrder(List<String> statusOrder) {
if (statusOrder != null && !statusOrder.isEmpty()) {
this.order = statusOrder;
}
public Status getStatus() {
return this.status;
}

public Map<String, Integer> getHttpMapping() {
return this.httpMapping;
public static class Status {

/**
* Comma-separated list of health statuses in order of severity.
*/
private List<String> order = null;

/**
* Mapping of health statuses to HTTP status codes. By default, registered health
* statuses map to sensible defaults (for example, UP maps to 200).
*/
private final Map<String, Integer> httpMapping = new HashMap<>();

public List<String> getOrder() {
return this.order;
}

public void setOrder(List<String> statusOrder) {
if (statusOrder != null && !statusOrder.isEmpty()) {
this.order = statusOrder;
}
}

public Map<String, Integer> getHttpMapping() {
return this.httpMapping;
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.actuate.health;

import java.util.HashMap;
import java.util.Map;

/**
* Aggregates the overall health of a system by caching {@link Health} per indicator.
*
* @author Stephane Nicoll
* @since 2.2.0
*/
public class AggregatedHealth {

private static final Health UNKNOWN_HEALTH = Health.unknown().build();

private final HealthIndicatorRegistry registry;

private final Map<String, Health> healths;

/**
* Create an instance based on the specified {@link HealthIndicatorRegistry}.
* @param registry the registry to use to retrieve an indicator by name
*/
public AggregatedHealth(HealthIndicatorRegistry registry) {
this.registry = registry;
this.healths = new HashMap<>();
}

/**
* Return the {@link Health} of the indicator with the specified {@code name} for this
* instance or {@code null} if no such indicator exists. When calling this method
* several times for a given indicator, the same {@link Health} instance is returned.
* @param name the name of a {@link HealthIndicator}
* @return the {@link Health} of the indicator with the specified name
*/
public Health health(String name) {
Health health = this.healths.computeIfAbsent(name, this::determineHealth);
return (health != UNKNOWN_HEALTH) ? health : null;
}

private Health determineHealth(String name) {
HealthIndicator healthIndicator = this.registry.get(name);
if (healthIndicator == null) {
return UNKNOWN_HEALTH;
}
if (healthIndicator instanceof AggregatedHealthIndicator) {
return ((AggregatedHealthIndicator) healthIndicator).health(this);
}
return healthIndicator.health();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.actuate.health;

/**
* An extended {@link HealthIndicator} that computes a global {@link Health} based on a
* {@link AggregatedHealth}.
*
* @author Stephane Nicoll
* @since 2.2.0
*/
@FunctionalInterface
public interface AggregatedHealthIndicator extends HealthIndicator {

@Override
default Health health() {
return health(new AggregatedHealth(new DefaultHealthIndicatorRegistry()));
}

/**
* Return an indication of health based on the specified {@link AggregatedHealth}.
* @param aggregatedHealth the already computed health
* @return the health that this aggregate represents
*/
Health health(AggregatedHealth aggregatedHealth);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.actuate.health;

import reactor.core.publisher.Mono;

/**
* An extended {@link ReactiveHealthIndicator} that computes a global {@link Health} based
* on a {@link ReactiveAggregatedHealth}.
*
* @author Stephane Nicoll
* @since 2.2.0
*/
@FunctionalInterface
public interface AggregatedReactiveHealthIndicator extends ReactiveHealthIndicator {

@Override
default Mono<Health> health() {
return health(new ReactiveAggregatedHealth(
new DefaultReactiveHealthIndicatorRegistry()));
}

/**
* Return an indication of health based on the specified
* {@link ReactiveAggregatedHealth}.
* @param aggregatedHealth the already computed health
* @return a {@link Mono} to the health that this aggregate represents
*/
Mono<Health> health(ReactiveAggregatedHealth aggregatedHealth);

}
Loading

0 comments on commit 9b2527e

Please sign in to comment.