Add support for ReactiveHealthIndicatorRegistry
This commit updates the initial proposal to add support for reactive use cases as well. A reactive application can use ReactiveHealthIndicatorRegistry as an alternative to HealthIndicatorRegistry. Closes gh-4965pull/13197/head
parent
95b251590e
commit
2c176a3770
@ -1,57 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2017 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
|
||||
*
|
||||
* http://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.autoconfigure.health;
|
||||
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.boot.actuate.health.CompositeReactiveHealthIndicator;
|
||||
import org.springframework.boot.actuate.health.CompositeReactiveHealthIndicatorFactory;
|
||||
import org.springframework.boot.actuate.health.HealthAggregator;
|
||||
import org.springframework.boot.actuate.health.HealthIndicator;
|
||||
import org.springframework.boot.actuate.health.OrderedHealthAggregator;
|
||||
import org.springframework.boot.actuate.health.ReactiveHealthIndicator;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
/**
|
||||
* Creates a {@link CompositeReactiveHealthIndicator} from beans in the
|
||||
* {@link ApplicationContext}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
final class HealthIndicatorBeansReactiveComposite {
|
||||
|
||||
private HealthIndicatorBeansReactiveComposite() {
|
||||
}
|
||||
|
||||
public static ReactiveHealthIndicator get(ApplicationContext applicationContext) {
|
||||
HealthAggregator healthAggregator = getHealthAggregator(applicationContext);
|
||||
return new CompositeReactiveHealthIndicatorFactory()
|
||||
.createReactiveHealthIndicator(healthAggregator,
|
||||
applicationContext.getBeansOfType(ReactiveHealthIndicator.class),
|
||||
applicationContext.getBeansOfType(HealthIndicator.class));
|
||||
}
|
||||
|
||||
private static HealthAggregator getHealthAggregator(
|
||||
ApplicationContext applicationContext) {
|
||||
try {
|
||||
return applicationContext.getBean(HealthAggregator.class);
|
||||
}
|
||||
catch (NoSuchBeanDefinitionException ex) {
|
||||
return new OrderedHealthAggregator();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Copyright 2012-2018 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
|
||||
*
|
||||
* http://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.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Default implementation of {@link ReactiveHealthIndicatorRegistry}.
|
||||
*
|
||||
* @author Vedran Pavic
|
||||
* @author Stephane Nicoll
|
||||
* @since 2.1.0
|
||||
*/
|
||||
public class DefaultReactiveHealthIndicatorRegistry
|
||||
implements ReactiveHealthIndicatorRegistry {
|
||||
|
||||
private final Object monitor = new Object();
|
||||
|
||||
private final Map<String, ReactiveHealthIndicator> healthIndicators;
|
||||
|
||||
/**
|
||||
* Create a new {@link DefaultReactiveHealthIndicatorRegistry}.
|
||||
*/
|
||||
public DefaultReactiveHealthIndicatorRegistry() {
|
||||
this(new LinkedHashMap<>());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link DefaultReactiveHealthIndicatorRegistry} from the specified
|
||||
* indicators.
|
||||
* @param healthIndicators a map of {@link HealthIndicator}s with the key
|
||||
* being used as an indicator name.
|
||||
*/
|
||||
public DefaultReactiveHealthIndicatorRegistry(
|
||||
Map<String, ReactiveHealthIndicator> healthIndicators) {
|
||||
Assert.notNull(healthIndicators, "HealthIndicators must not be null");
|
||||
this.healthIndicators = new LinkedHashMap<>(healthIndicators);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void register(String name, ReactiveHealthIndicator healthIndicator) {
|
||||
Assert.notNull(healthIndicator, "HealthIndicator must not be null");
|
||||
Assert.notNull(name, "Name must not be null");
|
||||
synchronized (this.monitor) {
|
||||
ReactiveHealthIndicator existing = this.healthIndicators.putIfAbsent(name,
|
||||
healthIndicator);
|
||||
if (existing != null) {
|
||||
throw new IllegalStateException(
|
||||
"HealthIndicator with name '" + name + "' already registered");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReactiveHealthIndicator unregister(String name) {
|
||||
Assert.notNull(name, "Name must not be null");
|
||||
synchronized (this.monitor) {
|
||||
return this.healthIndicators.remove(name);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReactiveHealthIndicator get(String name) {
|
||||
Assert.notNull(name, "Name must not be null");
|
||||
synchronized (this.monitor) {
|
||||
return this.healthIndicators.get(name);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, ReactiveHealthIndicator> getAll() {
|
||||
synchronized (this.monitor) {
|
||||
return Collections
|
||||
.unmodifiableMap(new LinkedHashMap<>(this.healthIndicators));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright 2012-2018 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
|
||||
*
|
||||
* http://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.Map;
|
||||
|
||||
/**
|
||||
* A registry of {@link ReactiveHealthIndicator ReactiveHealthIndicators}.
|
||||
* <p>
|
||||
* Implementations <strong>must</strong> be thread-safe.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Vedran Pavic
|
||||
* @author Stephane Nicoll
|
||||
* @since 2.1.0
|
||||
*/
|
||||
public interface ReactiveHealthIndicatorRegistry {
|
||||
|
||||
/**
|
||||
* Registers the given {@link ReactiveHealthIndicator}, associating it with the
|
||||
* given {@code name}.
|
||||
* @param name the name of the indicator
|
||||
* @param healthIndicator the indicator
|
||||
* @throws IllegalStateException if an indicator with the given {@code name}
|
||||
* is already registered.
|
||||
*/
|
||||
void register(String name, ReactiveHealthIndicator healthIndicator);
|
||||
|
||||
/**
|
||||
* Unregisters the {@link ReactiveHealthIndicator} previously registered with the
|
||||
* given {@code name}.
|
||||
* @param name the name of the indicator
|
||||
* @return the unregistered indicator, or {@code null} if no indicator was
|
||||
* found in the registry for the given {@code name}.
|
||||
*/
|
||||
ReactiveHealthIndicator unregister(String name);
|
||||
|
||||
/**
|
||||
* Returns the {@link ReactiveHealthIndicator} registered with the given {@code name}.
|
||||
* @param name the name of the indicator
|
||||
* @return the health indicator, or {@code null} if no indicator was
|
||||
* registered with the given {@code name}.
|
||||
*/
|
||||
ReactiveHealthIndicator get(String name);
|
||||
|
||||
/**
|
||||
* Returns a snapshot of the registered health indicators and their names.
|
||||
* The contents of the map do not reflect subsequent changes to the
|
||||
* registry.
|
||||
* @return the snapshot of registered health indicators
|
||||
*/
|
||||
Map<String, ReactiveHealthIndicator> getAll();
|
||||
|
||||
}
|
@ -0,0 +1,93 @@
|
||||
/*
|
||||
* Copyright 2012-2018 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
|
||||
*
|
||||
* http://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.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* Factory to create a {@link HealthIndicatorRegistry}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 2.1.0
|
||||
*/
|
||||
public class ReactiveHealthIndicatorRegistryFactory {
|
||||
|
||||
private final Function<String, String> healthIndicatorNameFactory;
|
||||
|
||||
public ReactiveHealthIndicatorRegistryFactory(
|
||||
Function<String, String> healthIndicatorNameFactory) {
|
||||
this.healthIndicatorNameFactory = healthIndicatorNameFactory;
|
||||
}
|
||||
|
||||
public ReactiveHealthIndicatorRegistryFactory() {
|
||||
this(new HealthIndicatorNameFactory());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link ReactiveHealthIndicatorRegistry} based on the specified health
|
||||
* indicators. Each {@link HealthIndicator} are wrapped to a
|
||||
* {@link HealthIndicatorReactiveAdapter}. If two instances share the same name, the
|
||||
* reactive variant takes precedence.
|
||||
* @param reactiveHealthIndicators the {@link ReactiveHealthIndicator} instances
|
||||
* mapped by name
|
||||
* @param healthIndicators the {@link HealthIndicator} instances mapped by name if
|
||||
* any.
|
||||
* @return a {@link ReactiveHealthIndicator} that delegates to the specified
|
||||
* {@code reactiveHealthIndicators}.
|
||||
*/
|
||||
public ReactiveHealthIndicatorRegistry createReactiveHealthIndicatorRegistry(
|
||||
Map<String, ReactiveHealthIndicator> reactiveHealthIndicators,
|
||||
Map<String, HealthIndicator> healthIndicators) {
|
||||
Assert.notNull(reactiveHealthIndicators,
|
||||
"ReactiveHealthIndicators must not be null");
|
||||
return initialize(new DefaultReactiveHealthIndicatorRegistry(),
|
||||
reactiveHealthIndicators, healthIndicators);
|
||||
}
|
||||
|
||||
protected <T extends ReactiveHealthIndicatorRegistry> T initialize(T registry,
|
||||
Map<String, ReactiveHealthIndicator> reactiveHealthIndicators,
|
||||
Map<String, HealthIndicator> healthIndicators) {
|
||||
merge(reactiveHealthIndicators, healthIndicators)
|
||||
.forEach((beanName, indicator) -> {
|
||||
String name = this.healthIndicatorNameFactory.apply(beanName);
|
||||
registry.register(name, indicator);
|
||||
});
|
||||
return registry;
|
||||
}
|
||||
|
||||
private Map<String, ReactiveHealthIndicator> merge(
|
||||
Map<String, ReactiveHealthIndicator> reactiveHealthIndicators,
|
||||
Map<String, HealthIndicator> healthIndicators) {
|
||||
if (ObjectUtils.isEmpty(healthIndicators)) {
|
||||
return reactiveHealthIndicators;
|
||||
}
|
||||
Map<String, ReactiveHealthIndicator> allIndicators = new LinkedHashMap<>(
|
||||
reactiveHealthIndicators);
|
||||
healthIndicators.forEach((beanName, indicator) -> {
|
||||
String name = this.healthIndicatorNameFactory.apply(beanName);
|
||||
allIndicators.computeIfAbsent(name,
|
||||
(n) -> new HealthIndicatorReactiveAdapter(indicator));
|
||||
});
|
||||
return allIndicators;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,111 @@
|
||||
/*
|
||||
* Copyright 2012-2018 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
|
||||
*
|
||||
* http://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.Map;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Tests for {@link DefaultReactiveHealthIndicatorRegistry}.
|
||||
*
|
||||
* @author Vedran Pavic
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class DefaultReactiveHealthIndicatorRegistryTests {
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
private ReactiveHealthIndicator one = mock(ReactiveHealthIndicator.class);
|
||||
|
||||
private ReactiveHealthIndicator two = mock(ReactiveHealthIndicator.class);
|
||||
|
||||
private DefaultReactiveHealthIndicatorRegistry registry;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
given(this.one.health()).willReturn(Mono.just(
|
||||
new Health.Builder().unknown().withDetail("1", "1").build()));
|
||||
given(this.two.health()).willReturn(Mono.just(
|
||||
new Health.Builder().unknown().withDetail("2", "2").build()));
|
||||
this.registry = new DefaultReactiveHealthIndicatorRegistry();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void register() {
|
||||
this.registry.register("one", this.one);
|
||||
this.registry.register("two", this.two);
|
||||
assertThat(this.registry.getAll()).hasSize(2);
|
||||
assertThat(this.registry.get("one")).isSameAs(this.one);
|
||||
assertThat(this.registry.get("two")).isSameAs(this.two);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void registerAlreadyUsedName() {
|
||||
this.registry.register("one", this.one);
|
||||
this.thrown.expect(IllegalStateException.class);
|
||||
this.thrown.expectMessage("HealthIndicator with name 'one' already registered");
|
||||
this.registry.register("one", this.two);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unregister() {
|
||||
this.registry.register("one", this.one);
|
||||
this.registry.register("two", this.two);
|
||||
assertThat(this.registry.getAll()).hasSize(2);
|
||||
ReactiveHealthIndicator two = this.registry.unregister("two");
|
||||
assertThat(two).isSameAs(this.two);
|
||||
assertThat(this.registry.getAll()).hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unregisterUnknown() {
|
||||
this.registry.register("one", this.one);
|
||||
assertThat(this.registry.getAll()).hasSize(1);
|
||||
ReactiveHealthIndicator two = this.registry.unregister("two");
|
||||
assertThat(two).isNull();
|
||||
assertThat(this.registry.getAll()).hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAllIsASnapshot() {
|
||||
this.registry.register("one", this.one);
|
||||
Map<String, ReactiveHealthIndicator> snapshot = this.registry.getAll();
|
||||
assertThat(snapshot).containsOnlyKeys("one");
|
||||
this.registry.register("two", this.two);
|
||||
assertThat(snapshot).containsOnlyKeys("one");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAllIsImmutable() {
|
||||
this.registry.register("one", this.one);
|
||||
Map<String, ReactiveHealthIndicator> snapshot = this.registry.getAll();
|
||||
|
||||
this.thrown.expect(UnsupportedOperationException.class);
|
||||
snapshot.clear();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright 2012-2018 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
|
||||
*
|
||||
* http://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.Collections;
|
||||
|
||||
import org.junit.Test;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link ReactiveHealthIndicatorRegistryFactory}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class ReactiveHealthIndicatorRegistryFactoryTests {
|
||||
|
||||
private static final Health UP = new Health.Builder().status(Status.UP).build();
|
||||
|
||||
private static final Health DOWN = new Health.Builder().status(Status.DOWN).build();
|
||||
|
||||
private final ReactiveHealthIndicatorRegistryFactory factory = new ReactiveHealthIndicatorRegistryFactory();
|
||||
|
||||
@Test
|
||||
public void defaultHealthIndicatorNameFactory() {
|
||||
ReactiveHealthIndicatorRegistry registry = this.factory.createReactiveHealthIndicatorRegistry(
|
||||
Collections.singletonMap("myHealthIndicator", () -> Mono.just(UP)), null);
|
||||
assertThat(registry.getAll()).containsOnlyKeys("my");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void healthIndicatorIsAdapted() {
|
||||
ReactiveHealthIndicatorRegistry registry = this.factory.createReactiveHealthIndicatorRegistry(
|
||||
Collections.singletonMap("test", () -> Mono.just(UP)),
|
||||
Collections.singletonMap("regular", () -> DOWN));
|
||||
assertThat(registry.getAll()).containsOnlyKeys("test", "regular");
|
||||
StepVerifier.create(registry.get("regular").health()).consumeNextWith((h) -> {
|
||||
assertThat(h.getStatus()).isEqualTo(Status.DOWN);
|
||||
assertThat(h.getDetails()).isEmpty();
|
||||
}).verifyComplete();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue