Skip to content

Commit

Permalink
Stop DataSource initialization from preventing Hikari instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkinsona committed Feb 21, 2018
1 parent 33be78a commit e7176c6
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
import com.zaxxer.hikari.HikariDataSource;
import com.zaxxer.hikari.metrics.micrometer.MicrometerMetricsTrackerFactory;
import io.micrometer.core.instrument.MeterRegistry;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.core.Ordered;

/**
* {@link BeanPostProcessor} that configures Hikari metrics. Such arrangement is necessary
Expand All @@ -30,7 +33,10 @@
*
* @author Stephane Nicoll
*/
class HikariDataSourceMetricsPostProcessor implements BeanPostProcessor {
class HikariDataSourceMetricsPostProcessor implements BeanPostProcessor, Ordered {

private static final Log logger = LogFactory
.getLog(HikariDataSourceMetricsPostProcessor.class);

private final ApplicationContext context;

Expand All @@ -53,8 +59,13 @@ private void bindMetricsRegistryToHikariDataSource(MeterRegistry registry,
HikariDataSource dataSource) {
if (dataSource.getMetricRegistry() == null
&& dataSource.getMetricsTrackerFactory() == null) {
dataSource.setMetricsTrackerFactory(
new MicrometerMetricsTrackerFactory(registry));
try {
dataSource.setMetricsTrackerFactory(
new MicrometerMetricsTrackerFactory(registry));
}
catch (Exception ex) {
logger.warn("Failed to bind Hikari metrics: " + ex.getMessage());
}
}
}

Expand All @@ -65,4 +76,9 @@ private MeterRegistry getMeterRegistry() {
return this.meterRegistry;
}

@Override
public int getOrder() {
return Ordered.HIGHEST_PRECEDENCE;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.boot.actuate.autoconfigure.metrics.jdbc;

import java.sql.SQLException;
import java.util.UUID;

import javax.sql.DataSource;
Expand All @@ -26,13 +27,17 @@
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import org.junit.Test;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.actuate.autoconfigure.metrics.test.MetricsRun;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.PriorityOrdered;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -96,6 +101,29 @@ public void autoConfiguredHikariDataSourceIsInstrumented() {
});
}

@Test
public void autoConfiguredHikariDataSourceIsInstrumentedWhenUsingDataSourceInitialization() {
this.contextRunner
.withPropertyValues(
"spring.datasource.schema:db/create-custom-schema.sql")
.run((context) -> {
context.getBean(DataSource.class).getConnection();
MeterRegistry registry = context.getBean(MeterRegistry.class);
registry.get("hikaricp.connections").meter();
});
}

@Test
public void failureToInstrumentHikariDataSourceIsTolerated() {
this.contextRunner.withUserConfiguration(HikariSealingConfiguration.class)
.run((context) -> {
assertThat(context).hasNotFailed();
context.getBean(DataSource.class).getConnection();
MeterRegistry registry = context.getBean(MeterRegistry.class);
assertThat(registry.find("hikaricp.connections").meter()).isNull();
});
}

@Test
public void hikariDataSourceInstrumentationCanBeDisabled() {
this.contextRunner.withPropertyValues("management.metrics.enable.hikaricp=false")
Expand Down Expand Up @@ -215,4 +243,37 @@ private org.apache.tomcat.jdbc.pool.DataSource createTomcatDataSource() {

}

@Configuration
static class HikariSealingConfiguration {

@Bean
public static HikariSealer hikariSealer() {
return new HikariSealer();
}

static class HikariSealer implements BeanPostProcessor, PriorityOrdered {

@Override
public int getOrder() {
return Ordered.HIGHEST_PRECEDENCE;
}

@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
if (bean instanceof HikariDataSource) {
try {
((HikariDataSource) bean).getConnection().close();
}
catch (SQLException ex) {
throw new IllegalStateException(ex);
}
}
return bean;
}

}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@
*/
class DataSourceInitializerPostProcessor implements BeanPostProcessor, Ordered {

private int order = Ordered.HIGHEST_PRECEDENCE;

@Override
public int getOrder() {
return this.order;
return Ordered.HIGHEST_PRECEDENCE + 1;
}

@Autowired
Expand Down

0 comments on commit e7176c6

Please sign in to comment.