Restore HealthIndicatorRegistry beans
Restore `HealthIndicatorRegistry` and `ReactiveHealthIndicatorRegistry` auto-configured beans with a version that adapts to the new contributor interfaces. Closes gh-16903pull/18337/head
parent
5076d8562a
commit
d7a472b8a6
@ -0,0 +1,79 @@
|
||||
/*
|
||||
* 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.autoconfigure.health;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.boot.actuate.health.HealthContributor;
|
||||
import org.springframework.boot.actuate.health.HealthContributorRegistry;
|
||||
import org.springframework.boot.actuate.health.HealthIndicator;
|
||||
import org.springframework.boot.actuate.health.HealthIndicatorRegistry;
|
||||
import org.springframework.boot.actuate.health.NamedContributor;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Adapter class to convert a {@link HealthContributorRegistry} to a legacy
|
||||
* {@link HealthIndicatorRegistry}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
class HealthContributorRegistryHealthIndicatorRegistryAdapter implements HealthIndicatorRegistry {
|
||||
|
||||
private final HealthContributorRegistry contributorRegistry;
|
||||
|
||||
HealthContributorRegistryHealthIndicatorRegistryAdapter(HealthContributorRegistry contributorRegistry) {
|
||||
Assert.notNull(contributorRegistry, "ContributorRegistry must not be null");
|
||||
this.contributorRegistry = contributorRegistry;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void register(String name, HealthIndicator healthIndicator) {
|
||||
this.contributorRegistry.registerContributor(name, healthIndicator);
|
||||
}
|
||||
|
||||
@Override
|
||||
public HealthIndicator unregister(String name) {
|
||||
HealthContributor contributor = this.contributorRegistry.unregisterContributor(name);
|
||||
if (contributor instanceof HealthIndicator) {
|
||||
return (HealthIndicator) contributor;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HealthIndicator get(String name) {
|
||||
HealthContributor contributor = this.contributorRegistry.getContributor(name);
|
||||
if (contributor instanceof HealthIndicator) {
|
||||
return (HealthIndicator) contributor;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, HealthIndicator> getAll() {
|
||||
Map<String, HealthIndicator> all = new LinkedHashMap<String, HealthIndicator>();
|
||||
for (NamedContributor<?> namedContributor : this.contributorRegistry) {
|
||||
if (namedContributor.getContributor() instanceof HealthIndicator) {
|
||||
all.put(namedContributor.getName(), (HealthIndicator) namedContributor.getContributor());
|
||||
}
|
||||
}
|
||||
return all;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,83 @@
|
||||
/*
|
||||
* 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.autoconfigure.health;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.boot.actuate.health.HealthContributorRegistry;
|
||||
import org.springframework.boot.actuate.health.HealthIndicatorRegistry;
|
||||
import org.springframework.boot.actuate.health.NamedContributor;
|
||||
import org.springframework.boot.actuate.health.ReactiveHealthContributor;
|
||||
import org.springframework.boot.actuate.health.ReactiveHealthContributorRegistry;
|
||||
import org.springframework.boot.actuate.health.ReactiveHealthIndicator;
|
||||
import org.springframework.boot.actuate.health.ReactiveHealthIndicatorRegistry;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Adapter class to convert a {@link HealthContributorRegistry} to a legacy
|
||||
* {@link HealthIndicatorRegistry}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
class ReactiveHealthContributorRegistryReactiveHealthIndicatorRegistryAdapter
|
||||
implements ReactiveHealthIndicatorRegistry {
|
||||
|
||||
private final ReactiveHealthContributorRegistry contributorRegistry;
|
||||
|
||||
ReactiveHealthContributorRegistryReactiveHealthIndicatorRegistryAdapter(
|
||||
ReactiveHealthContributorRegistry contributorRegistry) {
|
||||
Assert.notNull(contributorRegistry, "ContributorRegistry must not be null");
|
||||
this.contributorRegistry = contributorRegistry;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void register(String name, ReactiveHealthIndicator healthIndicator) {
|
||||
this.contributorRegistry.registerContributor(name, healthIndicator);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReactiveHealthIndicator unregister(String name) {
|
||||
ReactiveHealthContributor contributor = this.contributorRegistry.unregisterContributor(name);
|
||||
if (contributor instanceof ReactiveHealthIndicator) {
|
||||
return (ReactiveHealthIndicator) contributor;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReactiveHealthIndicator get(String name) {
|
||||
ReactiveHealthContributor contributor = this.contributorRegistry.getContributor(name);
|
||||
if (contributor instanceof ReactiveHealthIndicator) {
|
||||
return (ReactiveHealthIndicator) contributor;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, ReactiveHealthIndicator> getAll() {
|
||||
Map<String, ReactiveHealthIndicator> all = new LinkedHashMap<>();
|
||||
for (NamedContributor<?> namedContributor : this.contributorRegistry) {
|
||||
if (namedContributor.getContributor() instanceof ReactiveHealthIndicator) {
|
||||
all.put(namedContributor.getName(), (ReactiveHealthIndicator) namedContributor.getContributor());
|
||||
}
|
||||
}
|
||||
return all;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,109 @@
|
||||
/*
|
||||
* 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.autoconfigure.health;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.actuate.health.CompositeHealthContributor;
|
||||
import org.springframework.boot.actuate.health.DefaultHealthContributorRegistry;
|
||||
import org.springframework.boot.actuate.health.HealthContributorRegistry;
|
||||
import org.springframework.boot.actuate.health.HealthIndicator;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.assertj.core.api.Assertions.entry;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Tests for {@link HealthContributorRegistryHealthIndicatorRegistryAdapter}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
class HealthContributorRegistryHealthIndicatorRegistryAdapterTests {
|
||||
|
||||
private HealthContributorRegistry contributorRegistry = new DefaultHealthContributorRegistry();
|
||||
|
||||
private HealthContributorRegistryHealthIndicatorRegistryAdapter adapter = new HealthContributorRegistryHealthIndicatorRegistryAdapter(
|
||||
this.contributorRegistry);
|
||||
|
||||
@Test
|
||||
void createWhenContributorRegistryIsNullThrowsException() {
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> new HealthContributorRegistryHealthIndicatorRegistryAdapter(null))
|
||||
.withMessage("ContributorRegistry must not be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void registerDelegatesToContributorRegistry() {
|
||||
HealthIndicator healthIndicator = mock(HealthIndicator.class);
|
||||
this.adapter.register("test", healthIndicator);
|
||||
assertThat(this.contributorRegistry.getContributor("test")).isSameAs(healthIndicator);
|
||||
}
|
||||
|
||||
@Test
|
||||
void unregisterWhenDelegatesToContributorRegistry() {
|
||||
HealthIndicator healthIndicator = mock(HealthIndicator.class);
|
||||
this.contributorRegistry.registerContributor("test", healthIndicator);
|
||||
HealthIndicator unregistered = this.adapter.unregister("test");
|
||||
assertThat(unregistered).isSameAs(healthIndicator);
|
||||
assertThat(this.contributorRegistry.getContributor("test")).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void unregisterWhenContributorRegistryResultIsNotHealthIndicatorReturnsNull() {
|
||||
CompositeHealthContributor healthContributor = mock(CompositeHealthContributor.class);
|
||||
this.contributorRegistry.registerContributor("test", healthContributor);
|
||||
HealthIndicator unregistered = this.adapter.unregister("test");
|
||||
assertThat(unregistered).isNull();
|
||||
assertThat(this.contributorRegistry.getContributor("test")).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getDelegatesToContributorRegistry() {
|
||||
HealthIndicator healthIndicator = mock(HealthIndicator.class);
|
||||
this.contributorRegistry.registerContributor("test", healthIndicator);
|
||||
assertThat(this.adapter.get("test")).isSameAs(healthIndicator);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getWhenContributorRegistryResultIsNotHealthIndicatorReturnsNull() {
|
||||
CompositeHealthContributor healthContributor = mock(CompositeHealthContributor.class);
|
||||
this.contributorRegistry.registerContributor("test", healthContributor);
|
||||
assertThat(this.adapter.get("test")).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getAllDelegatesContributorRegistry() {
|
||||
HealthIndicator healthIndicator = mock(HealthIndicator.class);
|
||||
this.contributorRegistry.registerContributor("test", healthIndicator);
|
||||
Map<String, HealthIndicator> all = this.adapter.getAll();
|
||||
assertThat(all).containsOnly(entry("test", healthIndicator));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getAllWhenContributorRegistryContainsNonHealthIndicatorInstancesReturnsFilteredMap() {
|
||||
CompositeHealthContributor healthContributor = mock(CompositeHealthContributor.class);
|
||||
this.contributorRegistry.registerContributor("test1", healthContributor);
|
||||
HealthIndicator healthIndicator = mock(HealthIndicator.class);
|
||||
this.contributorRegistry.registerContributor("test2", healthIndicator);
|
||||
Map<String, HealthIndicator> all = this.adapter.getAll();
|
||||
assertThat(all).containsOnly(entry("test2", healthIndicator));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,92 @@
|
||||
/*
|
||||
* 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.autoconfigure.health;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import io.micrometer.core.instrument.Gauge;
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
|
||||
import org.springframework.boot.actuate.health.CompositeHealthIndicator;
|
||||
import org.springframework.boot.actuate.health.HealthAggregator;
|
||||
import org.springframework.boot.actuate.health.HealthIndicator;
|
||||
import org.springframework.boot.actuate.health.HealthIndicatorRegistry;
|
||||
import org.springframework.boot.actuate.health.Status;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Integration test to ensure that the legacy {@link HealthIndicatorRegistry} can still be
|
||||
* injected.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
@SpringBootTest(webEnvironment = WebEnvironment.NONE)
|
||||
public class HealthIndicatorRegistryInjectionIntegrationTests {
|
||||
|
||||
// gh-18194
|
||||
|
||||
@Test
|
||||
void meterRegistryBeanHasBeenConfigured(@Autowired MeterRegistry meterRegistry) {
|
||||
assertThat(meterRegistry).isNotNull();
|
||||
assertThat(meterRegistry.get("health").gauge()).isNotNull();
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ImportAutoConfiguration({ HealthEndpointAutoConfiguration.class, HealthContributorAutoConfiguration.class,
|
||||
CompositeMeterRegistryAutoConfiguration.class, MetricsAutoConfiguration.class })
|
||||
static class Config {
|
||||
|
||||
Config(HealthAggregator healthAggregator, HealthIndicatorRegistry healthIndicatorRegistry,
|
||||
List<HealthIndicator> healthIndicators, MeterRegistry registry) {
|
||||
CompositeHealthIndicator healthIndicator = new CompositeHealthIndicator(healthAggregator,
|
||||
healthIndicatorRegistry);
|
||||
for (int i = 0; i < healthIndicators.size(); i++) {
|
||||
healthIndicatorRegistry.register(Integer.toString(i), healthIndicators.get(i));
|
||||
}
|
||||
Gauge.builder("health", healthIndicator, this::getGuageValue)
|
||||
.description("Spring boot health indicator. 3=UP, 2=OUT_OF_SERVICE, 1=DOWN, 0=UNKNOWN")
|
||||
.strongReference(true).register(registry);
|
||||
}
|
||||
|
||||
private double getGuageValue(CompositeHealthIndicator health) {
|
||||
Status status = health.health().getStatus();
|
||||
switch (status.getCode()) {
|
||||
case "UP":
|
||||
return 3;
|
||||
case "OUT_OF_SERVICE":
|
||||
return 2;
|
||||
case "DOWN":
|
||||
return 1;
|
||||
case "UNKNOWN":
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,110 @@
|
||||
/*
|
||||
* 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.autoconfigure.health;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.actuate.health.CompositeReactiveHealthContributor;
|
||||
import org.springframework.boot.actuate.health.DefaultReactiveHealthContributorRegistry;
|
||||
import org.springframework.boot.actuate.health.ReactiveHealthContributorRegistry;
|
||||
import org.springframework.boot.actuate.health.ReactiveHealthIndicator;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.assertj.core.api.Assertions.entry;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Test for
|
||||
* {@link ReactiveHealthContributorRegistryReactiveHealthIndicatorRegistryAdapter}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
class ReactiveHealthContributorRegistryReactiveHealthIndicatorRegistryAdapterTests {
|
||||
|
||||
private ReactiveHealthContributorRegistry contributorRegistry = new DefaultReactiveHealthContributorRegistry();
|
||||
|
||||
private ReactiveHealthContributorRegistryReactiveHealthIndicatorRegistryAdapter adapter = new ReactiveHealthContributorRegistryReactiveHealthIndicatorRegistryAdapter(
|
||||
this.contributorRegistry);
|
||||
|
||||
@Test
|
||||
void createWhenContributorRegistryIsNullThrowsException() {
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> new ReactiveHealthContributorRegistryReactiveHealthIndicatorRegistryAdapter(null))
|
||||
.withMessage("ContributorRegistry must not be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void registerDelegatesToContributorRegistry() {
|
||||
ReactiveHealthIndicator healthIndicator = mock(ReactiveHealthIndicator.class);
|
||||
this.adapter.register("test", healthIndicator);
|
||||
assertThat(this.contributorRegistry.getContributor("test")).isSameAs(healthIndicator);
|
||||
}
|
||||
|
||||
@Test
|
||||
void unregisterWhenDelegatesToContributorRegistry() {
|
||||
ReactiveHealthIndicator healthIndicator = mock(ReactiveHealthIndicator.class);
|
||||
this.contributorRegistry.registerContributor("test", healthIndicator);
|
||||
ReactiveHealthIndicator unregistered = this.adapter.unregister("test");
|
||||
assertThat(unregistered).isSameAs(healthIndicator);
|
||||
assertThat(this.contributorRegistry.getContributor("test")).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void unregisterWhenContributorRegistryResultIsNotHealthIndicatorReturnsNull() {
|
||||
CompositeReactiveHealthContributor healthContributor = mock(CompositeReactiveHealthContributor.class);
|
||||
this.contributorRegistry.registerContributor("test", healthContributor);
|
||||
ReactiveHealthIndicator unregistered = this.adapter.unregister("test");
|
||||
assertThat(unregistered).isNull();
|
||||
assertThat(this.contributorRegistry.getContributor("test")).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getDelegatesToContributorRegistry() {
|
||||
ReactiveHealthIndicator healthIndicator = mock(ReactiveHealthIndicator.class);
|
||||
this.contributorRegistry.registerContributor("test", healthIndicator);
|
||||
assertThat(this.adapter.get("test")).isSameAs(healthIndicator);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getWhenContributorRegistryResultIsNotHealthIndicatorReturnsNull() {
|
||||
CompositeReactiveHealthContributor healthContributor = mock(CompositeReactiveHealthContributor.class);
|
||||
this.contributorRegistry.registerContributor("test", healthContributor);
|
||||
assertThat(this.adapter.get("test")).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getAllDelegatesContributorRegistry() {
|
||||
ReactiveHealthIndicator healthIndicator = mock(ReactiveHealthIndicator.class);
|
||||
this.contributorRegistry.registerContributor("test", healthIndicator);
|
||||
Map<String, ReactiveHealthIndicator> all = this.adapter.getAll();
|
||||
assertThat(all).containsOnly(entry("test", healthIndicator));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getAllWhenContributorRegistryContainsNonHealthIndicatorInstancesReturnsFilteredMap() {
|
||||
CompositeReactiveHealthContributor healthContributor = mock(CompositeReactiveHealthContributor.class);
|
||||
this.contributorRegistry.registerContributor("test1", healthContributor);
|
||||
ReactiveHealthIndicator healthIndicator = mock(ReactiveHealthIndicator.class);
|
||||
this.contributorRegistry.registerContributor("test2", healthIndicator);
|
||||
Map<String, ReactiveHealthIndicator> all = this.adapter.getAll();
|
||||
assertThat(all).containsOnly(entry("test2", healthIndicator));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue