Skip to content

Commit fe19190

Browse files
committed
Merge pull request #22222 from juliojgd
* gh-22222: Polish "Optionally ignore routing data sources when creating DB health indicators" Optionally ignore routing data sources when creating DB health indicators Closes gh-22222
2 parents 523dd93 + b627918 commit fe19190

File tree

3 files changed

+77
-3
lines changed

3 files changed

+77
-3
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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;
@@ -54,13 +55,15 @@
5455
* @author Andy Wilkinson
5556
* @author Stephane Nicoll
5657
* @author Arthur Kalimullin
58+
* @author Julio Gomez
5759
* @since 2.0.0
5860
*/
5961
@Configuration(proxyBeanMethods = false)
6062
@ConditionalOnClass({ JdbcTemplate.class, AbstractRoutingDataSource.class })
6163
@ConditionalOnBean(DataSource.class)
6264
@ConditionalOnEnabledHealthIndicator("db")
6365
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
66+
@EnableConfigurationProperties(DataSourceHealthIndicatorProperties.class)
6467
public class DataSourceHealthContributorAutoConfiguration extends
6568
CompositeHealthContributorConfiguration<AbstractHealthIndicator, DataSource> implements InitializingBean {
6669

@@ -80,7 +83,14 @@ public void afterPropertiesSet() throws Exception {
8083

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 AbstractRoutingDataSources when creating database health
33+
* indicators.
34+
*/
35+
private boolean ignoreRoutingDataSources = false;
36+
37+
public boolean isIgnoreRoutingDataSources() {
38+
return this.ignoreRoutingDataSources;
39+
}
40+
41+
public void setIgnoreRoutingDataSources(boolean ignoreRoutingDataSources) {
42+
this.ignoreRoutingDataSources = ignoreRoutingDataSources;
43+
}
44+
45+
}

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -44,6 +44,7 @@
4444
* Tests for {@link DataSourceHealthContributorAutoConfiguration}.
4545
*
4646
* @author Phillip Webb
47+
* @author Julio Gomez
4748
*/
4849
class DataSourceHealthContributorAutoConfigurationTests {
4950

@@ -82,12 +83,30 @@ void runWithRoutingAndEmbeddedDataSourceShouldIncludeRoutingDataSource() {
8283
});
8384
}
8485

86+
@Test
87+
void runWithRoutingAndEmbeddedDataSourceShouldNotIncludeRoutingDataSourceWhenIgnored() {
88+
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class, RoutingDatasourceConfig.class)
89+
.withPropertyValues("management.health.db.ignore-routing-datasources:true").run((context) -> {
90+
assertThat(context).doesNotHaveBean(CompositeHealthContributor.class);
91+
assertThat(context).hasSingleBean(DataSourceHealthIndicator.class);
92+
assertThat(context).doesNotHaveBean(RoutingDataSourceHealthIndicator.class);
93+
});
94+
}
95+
8596
@Test
8697
void runWithOnlyRoutingDataSourceShouldIncludeRoutingDataSource() {
8798
this.contextRunner.withUserConfiguration(RoutingDatasourceConfig.class)
8899
.run((context) -> assertThat(context).hasSingleBean(RoutingDataSourceHealthIndicator.class));
89100
}
90101

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

0 commit comments

Comments
 (0)