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?
@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
}
}
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).

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).
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)
I am facing the same issue, Can you send me sample java code for the Same
@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
DataSourceobjects with a metric namedjdbc.
I confirmed it works automatically with a user-defined DataSource bean using a sample. Please see the referenced sample.