I am in need of disabling the Spring Boot health indicator for a single data source (we have many and I don't want to disable them all or create indicators for the others, if possible).
The path I have taken is to extend the DataSourceHealthContributorAutoConfiguration. I know this used to work when DataSourceHealthIndicatorAutoConfiguration was in play, but now that it is a subinterface it is not working as expected.
Expected: Health indicator for data sources using the Snowflake driver will not be checked
Reality: The data source does appear to be filtered out on startup, however, createIndicator is still being called for all of the data sources.
Please let me know if more information is required. This is my first attempt to change java code so I might be missing some things.
package com.org.database.snowflake.config;
import com.zaxxer.hikari.HikariDataSource;
import javax.sql.DataSource;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.jdbc.metadata.DataSourcePoolMetadataProvider;
import org.springframework.context.annotation.Configuration;
import org.springframework.boot.actuate.autoconfigure.jdbc.DataSourceHealthContributorAutoConfiguration;
import java.util.Map;
import java.util.stream.Collectors;
@Configuration
public class ExcludeSnowflakeHealthIndicatorConfig extends DataSourceHealthContributorAutoConfiguration {
public ExcludeSnowflakeHealthIndicatorConfig(Map<String, DataSource> dataSources, ObjectProvider<DataSourcePoolMetadataProvider> metadataProviders) {
// Filter out data sources that use the Snowflake Driver
super(filterDataSources(dataSources), metadataProviders);
}
private static Map<String, DataSource> filterDataSources(Map<String, DataSource> dataSources) {
return dataSources.entrySet().stream()
.filter(dataSourceEntry -> {
if (dataSourceEntry.getValue() instanceof HikariDataSource) {
HikariDataSource hikariDataSource = (HikariDataSource) dataSourceEntry.getValue();
return !hikariDataSource.getDriverClassName().equals("net.snowflake.client.jdbc.SnowflakeDriver");
} else {
return true;
}
}).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
}
question from:
https://stackoverflow.com/questions/66052911/how-to-disable-individual-health-indicator-in-spring-boot 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…