Not monitor a specific Datasource for Health Check

jabrena picture jabrena · Oct 14, 2017 · Viewed 10.7k times · Source

I would like to know if exist some way to disable the monitoring of a Specific DataSource by SpringBoot Actuator.

Scenario: One Microservice uses 3 Datasources but for some Business Reason, one Datasource of them, it is not necessary to be monitored by Spring Boot Health Indicator.

How to disable the monitoring of one specific DataSource?

Many thanks in advance

Juan Antonio

Answer

glytching picture glytching · Oct 14, 2017

I think you'd have to disable the default datasources health indicator, which you can do with this property:

management.health.db.enabled=false

And then configure your own health indicators which only address the datasources you are interested in, something like this perhaps:

@Autowired
private DataSource dataSourceA;

@Autowired
private DataSource dataSourceB;

@Bean
public DataSourceHealthIndicator dataSourceHealthIndicatorA() {
    return new DataSourceHealthIndicator(dataSourceA);
}

@Bean
public DataSourceHealthIndicator dataSourceHealthIndicatorB() {
    return new DataSourceHealthIndicator(dataSourceB);
}

Or, alternatively write your own 'multiple datasources health indicator' by extending AbstractHealthIndicator and injecting into it only the Datasources you are interested in monitoring. Any Spring bean of type HealthIndicator will be automatically registered with the health actuator so you only have to let Spring create your custom HealthIndicator and it will be exposed by the actuator.

For background, you can see how Spring configures the default datasource health check in: org.springframework.boot.actuate.autoconfigure.DataSourcesHealthIndicatorConfiguration.