Skip to content
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

Spring Boot 2 not collecting DataSource metrics #584

Closed
AlexLardschneider opened this issue May 2, 2018 · 6 comments
Closed

Spring Boot 2 not collecting DataSource metrics #584

AlexLardschneider opened this issue May 2, 2018 · 6 comments
Labels
question A user question, probably better suited for StackOverflow

Comments

@AlexLardschneider
Copy link

Hello,

I recently upgraded my spring boot application to 2.0.1 to make use of the new micrometer Prometheus feature. According to the documentation, micrometer should automatically instrument all available DataSource objects and output the metrics as gauges.

However, it seems like that feature isn't working in my case. Accessing /actuator/prometheus I can see the various metrics micrometer collects, but no JDBC DataSource metrics.

My datasource is configured in its own spring configuration class (credentials omitted):

@Configuration
class DataSourceConfiguration {

    @Bean
    @Primary
    fun dataSource(): DataSource {
        val dataSource = org.apache.tomcat.jdbc.pool.DataSource()

        dataSource.driverClassName = "org.postgresql.Driver"
        dataSource.url = "jdbc:postgresql://$POSTGRES_HOST:$POSTGRES_PORT/$POSTGRES_DB_NAME"
        dataSource.username = POSTGRES_USER
        dataSource.password = POSTGRES_PASSWORD

        dataSource.initialSize = 2
        dataSource.maxActive = 8
        dataSource.maxIdle = 4
        dataSource.minIdle = 1

        dataSource.logValidationErrors = true

        dataSource.timeBetweenEvictionRunsMillis = 34_000
        dataSource.minEvictableIdleTimeMillis = 55_000

        dataSource.maxWait = 10_000

        dataSource.isRemoveAbandoned = true
        dataSource.removeAbandonedTimeout = 60

        return dataSource
    }
}

Is there anything else I need to configure for micrometer to start collecting datasource metrics?

@jkschneider
Copy link
Contributor

jkschneider commented May 2, 2018

@AlexLardschneider Only datasources autoconfigured by Spring Boot are automatically instrumented.

However, you can wire it manually:

@Configuration
class DataSourceConfiguration {

    @Bean
    @Primary
    fun dataSource(registry: MeterRegistry): DataSource {
        val dataSource = org.apache.tomcat.jdbc.pool.DataSource()

        ...
        DataSourcePoolMetrics(dataSource, listOf({ ds -> TomcatDataSourcePoolMetadata(ds) }), "tomcatDbPool").bindTo(registry)
        return dataSource
    }
}

@jkschneider jkschneider added the question A user question, probably better suited for StackOverflow label May 2, 2018
@AlexLardschneider
Copy link
Author

Thanks for getting back to me @jkschneider.

Still having problems wiring it manually, are you sure the method parameters in your example are correct? IntelliJ is throwing errors (see screenshot).

screen shot 2018-05-02 at 21 19 47

@jkschneider
Copy link
Contributor

jkschneider commented May 2, 2018

Sorry, I was writing code in the Github UI ;). Basically you just need to implement DataSourcePoolMetadataProvider, and it is a functional interface that should return TomcatDataSourcePoolMetadata(ds).

@AlexLardschneider
Copy link
Author

Thanks for the hints. For anyone else wondering about the solution:

val provider = DataSourcePoolMetadataProvider { TomcatDataSourcePoolMetadata(dataSource) }

DataSourcePoolMetrics(dataSource as javax.sql.DataSource, provider, POSTGRES_DB_NAME, emptyList()).bindTo(registry)

@prasan89
Copy link

I am facing the same issue, Can you send me sample java code for the Same

izeye added a commit to izeye/sample-micrometer-spring-boot that referenced this issue May 20, 2019
@izeye
Copy link
Contributor

izeye commented May 20, 2019

@prasan89 Based on the current Spring Boot reference documentation, instrumentation on DataSource beans doesn't seem to be limited to auto-configured ones:

Auto-configuration enables the instrumentation of all available DataSource objects with a metric named jdbc.

I confirmed it works automatically with a user-defined DataSource bean using a sample. Please see the referenced sample.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question A user question, probably better suited for StackOverflow
Projects
None yet
Development

No branches or pull requests

4 participants