Skip to content

DataSource decorator with HikariDataSourcePoolMetadata #61

@oburgosm

Description

@oburgosm

I have an SB 2.5 application with a Hikari datasource and I'm using datasource-proxy-spring-boot-starter (1.7.1).

Also I am using datasources metrics provided by SB, based on class HikariDataSourcePoolMetadata, and here I have the problem. The method getActive invoke to getHikariPool that does this:

        private HikariPool getHikariPool() {
		return (HikariPool) new DirectFieldAccessor(getDataSource()).getPropertyValue("pool");
	}

But, because the datasource is a proxy, it return null.

I¡m not sure if it's possible that proxied datasource will give access to field pool, or it's an issue that I should report to SB.

Meanwhile I have a workaround. I have created the next bean, which take into account that HikariDataSource could be a proxy and it have more priority that bean hikariPoolDataSourceMetadataProvider provides by SB :

    @Bean
    @Order(Ordered.LOWEST_PRECEDENCE - 10)
    DataSourcePoolMetadataProvider customHikariPoolDataSourceMetadataProvider() {
        return (dataSource) -> {
            HikariDataSource hikariDataSource = DataSourceUnwrapper.unwrap(dataSource, HikariConfigMXBean.class,
                    HikariDataSource.class);
            if (hikariDataSource != null) {
                // Because HikariDataSourcePoolMetadata access to HikariPool via reflection, we need to unwrap AOP
                // proxies
                if (AopUtils.isAopProxy(hikariDataSource)) {
                    Object target;
                    try {
                        target = ((Advised) hikariDataSource).getTargetSource().getTarget();
                        hikariDataSource = (HikariDataSource) target;
                    } catch (Exception ex) {
                        LOG.warn(
                                "Unwrap hikari AOP proxy failed. We instrument Hikari proxied datasource, but some metrics might not be available");
                    }
                }
                return new HikariDataSourcePoolMetadata(hikariDataSource);
            }
            return null;
        };
    }


Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions