Skip to content

DataSource Metrics are not created for DataSources behind @RefreshScope proxies #6596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
oxygensar opened this issue Aug 9, 2016 · 4 comments
Labels
status: duplicate A duplicate of another issue

Comments

@oxygensar
Copy link

It seems that DataSource metrics are not created for DataSources annotated with @RefreshScope.

@SpringBootApplication
public class DemoApplication {

    @Autowired
    private DataSource dataSource;

    @Bean
// uncomment to breate datasource metrics
//  @RefreshScope
    public DataSource dataSource(DataSourceProperties properties) {

        DataSourceBuilder factory = DataSourceBuilder
                .create(properties.getClassLoader())
                .driverClassName(properties.getDriverClassName())
                .url(properties.getUrl()).username(properties.getUsername())
                .password(properties.getPassword());
        if (properties.getType() != null) {
            factory.type(properties.getType());
        }
        return factory.build();
    }

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

will give us:

curl http://localhost:8080/health

{"status":"UP","diskSpace":{"status":"UP","total":179729068032,"free":44992753664,"threshold":10485760},"db":{"status":"UP","database":"HSQL Database Engine","hello":1},"refreshScope":{"status":"UP"},"configServer":{"status":"UNKNOWN","error":"no property sources located"}}

and

curl http://localhost:8080/metrics/datasource.*
{"datasource.primary.active":0,"datasource.primary.usage":0.0}

but if one uncomments @RefreshScope line health endpoint will continue working but metrics won't be created:

curl http://localhost:8080/metrics/datasource.*
{}

Tested on Spring Boot 1.3.5 and 1.4.0

Looks like RefreshScope always creates JDK Dynamic Proxies and classes like HikariPoolDataSourceMetadataProviderConfiguration check dataSource using instanceof operator which doesn't allow them to identify original DataSource behind the proxy.

Please advice are there any plans to add metrics support for RefreshScope beans.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Aug 9, 2016
@philwebb philwebb added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Aug 9, 2016
@philwebb philwebb changed the title Metrics are not created for DataSources with @RefreshScope DataSource Metrics are not created for DataSources behind @RefreshScope proxies Aug 9, 2016
@philwebb
Copy link
Member

philwebb commented Aug 9, 2016

There are no immediate plans to support this. It might be possible to extract the underlying target from the proxy and whilst this might work for @RefreshScope it probably wouldn't work for other proxy types. Perhaps there's something we can do when #4353 is resolved.

@oxygensar
Copy link
Author

I see, by the way at least in case of Hikari DataSource it's possible to use instanceof HikariConfigMXBean (instead of HikariDataSource) which is an interface and thus implemented by proxy as well as DataSource. This interface provides almost all methods required by DataSourcePoolMetadataProvider. I can submit a pull request in case you think this might be useful.
Another way could be for RefreshScope to respect proxy-target-class...

@philwebb
Copy link
Member

philwebb commented Aug 9, 2016

@oxygensar I pull request would be most welcome if you have time. I took a quick look at HikariDataSourcePoolMetadata and thought the DirectFieldAccessor would cause problems.

@snicoll
Copy link
Member

snicoll commented Dec 14, 2018

Duplicate of #15227

@snicoll snicoll marked this as a duplicate of #15227 Dec 14, 2018
@snicoll snicoll closed this as completed Dec 14, 2018
@snicoll snicoll added status: duplicate A duplicate of another issue and removed type: enhancement A general enhancement labels Dec 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

4 participants