Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.autoconfigure.health.CompositeHealthContributorConfiguration;
import org.springframework.boot.actuate.autoconfigure.health.ConditionalOnEnabledHealthIndicator;
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
Expand Down Expand Up @@ -64,6 +65,9 @@
public class DataSourceHealthContributorAutoConfiguration extends
CompositeHealthContributorConfiguration<AbstractHealthIndicator, DataSource> implements InitializingBean {

@Value("${management.health.db.ignore-routing-datasources:false}")
private boolean ignoreRoutingDataSources;

private final Collection<DataSourcePoolMetadataProvider> metadataProviders;

private DataSourcePoolMetadataProvider poolMetadataProvider;
Expand All @@ -81,6 +85,12 @@ public void afterPropertiesSet() throws Exception {
@Bean
@ConditionalOnMissingBean(name = { "dbHealthIndicator", "dbHealthContributor" })
public HealthContributor dbHealthContributor(Map<String, DataSource> dataSources) {
if (this.ignoreRoutingDataSources) {
Map<String, DataSource> filteredDatasources = dataSources.entrySet().stream()
.filter((e) -> !(e.getValue() instanceof AbstractRoutingDataSource))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
return createContributor(filteredDatasources);
}
return createContributor(dataSources);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@
"description": "Whether to enable database health check.",
"defaultValue": true
},
{
"name": "management.health.db.ignore-routing-datasources",
"type": "java.lang.Boolean",
"description": "Whether to ignore the creation of health indicators for AbstractRoutingDatasource s",
"defaultValue": false
},
{
"name": "management.health.defaults.enabled",
"type": "java.lang.Boolean",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,30 @@ void runWithRoutingAndEmbeddedDataSourceShouldIncludeRoutingDataSource() {
});
}

@Test
void runWithRoutingAndEmbeddedDataSourceShouldNotIncludeRoutingDataSourceWhenIgnored() {
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class, RoutingDatasourceConfig.class)
.withPropertyValues("management.health.db.ignore-routing-datasources:true").run((context) -> {
assertThat(context).doesNotHaveBean(CompositeHealthContributor.class);
assertThat(context).hasSingleBean(DataSourceHealthIndicator.class);
assertThat(context).doesNotHaveBean(RoutingDataSourceHealthIndicator.class);
});
}

@Test
void runWithOnlyRoutingDataSourceShouldIncludeRoutingDataSource() {
this.contextRunner.withUserConfiguration(RoutingDatasourceConfig.class)
.run((context) -> assertThat(context).hasSingleBean(RoutingDataSourceHealthIndicator.class));
}

@Test
void runWithOnlyRoutingDataSourceShouldCrashWhenIgnored() {
this.contextRunner.withUserConfiguration(RoutingDatasourceConfig.class)
.withPropertyValues("management.health.db.ignore-routing-datasources:true")
.run((context) -> assertThat(context).hasFailed().getFailure()
.hasRootCauseInstanceOf(IllegalArgumentException.class));
}

@Test
void runWithValidationQueryPropertyShouldUseCustomQuery() {
this.contextRunner
Expand Down