forked from spring-projects/spring-boot
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for health indicator groups
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
Showing
21 changed files
with
1,160 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...boot-actuator/src/main/java/org/springframework/boot/actuate/health/AggregatedHealth.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
...ator/src/main/java/org/springframework/boot/actuate/health/AggregatedHealthIndicator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
.../main/java/org/springframework/boot/actuate/health/AggregatedReactiveHealthIndicator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* 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); | ||
|
||
} |
Oops, something went wrong.