Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I configure HikariCP and Dropwizard/Coda-Hale metrics in Spring Boot application

Reading the instructions on the HikariCP wiki about how to enable the Dropwizard metrics, it says to just configure a MetricsRegistry instance in HikariConfig or HikariDatasource.

Problem is, in Spring Boot, all the configuration is handled by autoconfiguration so I'm not manually configuring the HikariCP pool at all.

Any instructions on how to do this? Do I have to completely override the autoconfiguration by defining my own bean and setting all the settings in a @Configuration file?

like image 302
Kevin M Avatar asked Feb 19 '15 19:02

Kevin M


People also ask

What is the use of HikariCP in spring boot?

Hikari is a JDBC DataSource implementation that provides a connection pooling mechanism. Compared to other implementations, it promises to be lightweight and better performing.

What is io Dropwizard metrics?

Metrics is a Java library which gives you unparalleled insight into what your code does in production. Metrics provides a powerful toolkit of ways to measure the behavior of critical components in your production environment.

What is HikariCP DataSource?

"HikariCP is solid high-performance JDBC connection pool. A connection pool is a cache of database connections maintained so that the connections can be reused when future requests to the database are required. Connection pools may significantly reduce the overall resource usage." - You can find out more here.


2 Answers

Or let Spring Boot configure your data source, @Autowire the DataSource and MetricRegistry in your @Configuration/@SpringBootApplication class and wire them together in a @PostConstruct:

@Autowired
private DataSource dataSource;

@Autowired
private MetricRegistry metricRegistry;


@PostConstruct
public void setUpHikariWithMetrics() {
    if(dataSource instanceof HikariDataSource) {
        ((HikariDataSource) dataSource).setMetricRegistry(metricRegistry);
    }
}
like image 181
Svante Avatar answered Oct 22 '22 10:10

Svante


So I was able to figure this out by manually configuring HikariCP in a java configuration file. That allowed me to get a reference to the Spring Boot MetricRegistry, which I could then set in HikariConfig. Here's my configuration class:

@Configuration
public class DatasourceConfiguration {

    @Value("${spring.datasource.username}")
    private String user;

    @Value("${spring.datasource.password}")
    private String password;

    @Value("${spring.datasource.url}")
    private String dataSourceUrl;

    @Value("${spring.datasource.driverClassName}")
    private String driverClassName;

    @Value("${spring.datasource.connectionTestQuery}")
    private String connectionTestQuery;

    @Autowired
    private MetricRegistry metricRegistry;

    @Bean
    public DataSource primaryDataSource() {
        Properties dsProps = new Properties();
        dsProps.setProperty("url", dataSourceUrl);
        dsProps.setProperty("user", user);
        dsProps.setProperty("password", password);

        Properties configProps = new Properties();
        configProps.setProperty("connectionTestQuery", connectionTestQuery);
        configProps.setProperty("driverClassName", driverClassName);
        configProps.setProperty("jdbcUrl", dataSourceUrl);

        HikariConfig hc = new HikariConfig(configProps);
        hc.setDataSourceProperties(dsProps);
        hc.setMetricRegistry(metricRegistry);
        return new HikariDataSource(hc);
    }
}
like image 16
Kevin M Avatar answered Oct 22 '22 10:10

Kevin M