Skip to content

Commit 13d1d23

Browse files
juliojgdwilkinsona
authored andcommitted
Optionally ignore routing data sources when creating DB health indicators
See gh-22222
1 parent 523dd93 commit 13d1d23

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/jdbc/DataSourceHealthContributorAutoConfiguration.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
3838
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
3939
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
40+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
4041
import org.springframework.boot.jdbc.metadata.CompositeDataSourcePoolMetadataProvider;
4142
import org.springframework.boot.jdbc.metadata.DataSourcePoolMetadata;
4243
import org.springframework.boot.jdbc.metadata.DataSourcePoolMetadataProvider;
@@ -61,6 +62,7 @@
6162
@ConditionalOnBean(DataSource.class)
6263
@ConditionalOnEnabledHealthIndicator("db")
6364
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
65+
@EnableConfigurationProperties(DataSourceHealthIndicatorProperties.class)
6466
public class DataSourceHealthContributorAutoConfiguration extends
6567
CompositeHealthContributorConfiguration<AbstractHealthIndicator, DataSource> implements InitializingBean {
6668

@@ -80,7 +82,14 @@ public void afterPropertiesSet() throws Exception {
8082

8183
@Bean
8284
@ConditionalOnMissingBean(name = { "dbHealthIndicator", "dbHealthContributor" })
83-
public HealthContributor dbHealthContributor(Map<String, DataSource> dataSources) {
85+
public HealthContributor dbHealthContributor(Map<String, DataSource> dataSources,
86+
DataSourceHealthIndicatorProperties dataSourceHealthIndicatorProperties) {
87+
if (dataSourceHealthIndicatorProperties.isIgnoreRoutingDataSources()) {
88+
Map<String, DataSource> filteredDatasources = dataSources.entrySet().stream()
89+
.filter((e) -> !(e.getValue() instanceof AbstractRoutingDataSource))
90+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
91+
return createContributor(filteredDatasources);
92+
}
8493
return createContributor(dataSources);
8594
}
8695

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2012-2020 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.jdbc;
18+
19+
import org.springframework.boot.actuate.jdbc.DataSourceHealthIndicator;
20+
import org.springframework.boot.context.properties.ConfigurationProperties;
21+
22+
/**
23+
* External configuration properties for {@link DataSourceHealthIndicator}.
24+
*
25+
* @author Julio Gomez
26+
* @since 2.4.0
27+
*/
28+
@ConfigurationProperties(prefix = "management.health.db")
29+
public class DataSourceHealthIndicatorProperties {
30+
31+
/**
32+
* Whether to ignore the creation of health indicators for AbstractRoutingDatasource.
33+
*/
34+
private boolean ignoreRoutingDataSources = false;
35+
36+
public boolean isIgnoreRoutingDataSources() {
37+
return this.ignoreRoutingDataSources;
38+
}
39+
40+
public void setIgnoreRoutingDataSources(boolean ignoreRoutingDataSources) {
41+
this.ignoreRoutingDataSources = ignoreRoutingDataSources;
42+
}
43+
44+
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/jdbc/DataSourceHealthContributorAutoConfigurationTests.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,30 @@ void runWithRoutingAndEmbeddedDataSourceShouldIncludeRoutingDataSource() {
8282
});
8383
}
8484

85+
@Test
86+
void runWithRoutingAndEmbeddedDataSourceShouldNotIncludeRoutingDataSourceWhenIgnored() {
87+
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class, RoutingDatasourceConfig.class)
88+
.withPropertyValues("management.health.db.ignore-routing-datasources:true").run((context) -> {
89+
assertThat(context).doesNotHaveBean(CompositeHealthContributor.class);
90+
assertThat(context).hasSingleBean(DataSourceHealthIndicator.class);
91+
assertThat(context).doesNotHaveBean(RoutingDataSourceHealthIndicator.class);
92+
});
93+
}
94+
8595
@Test
8696
void runWithOnlyRoutingDataSourceShouldIncludeRoutingDataSource() {
8797
this.contextRunner.withUserConfiguration(RoutingDatasourceConfig.class)
8898
.run((context) -> assertThat(context).hasSingleBean(RoutingDataSourceHealthIndicator.class));
8999
}
90100

101+
@Test
102+
void runWithOnlyRoutingDataSourceShouldCrashWhenIgnored() {
103+
this.contextRunner.withUserConfiguration(RoutingDatasourceConfig.class)
104+
.withPropertyValues("management.health.db.ignore-routing-datasources:true")
105+
.run((context) -> assertThat(context).hasFailed().getFailure()
106+
.hasRootCauseInstanceOf(IllegalArgumentException.class));
107+
}
108+
91109
@Test
92110
void runWithValidationQueryPropertyShouldUseCustomQuery() {
93111
this.contextRunner

0 commit comments

Comments
 (0)