Expose additional pool metrics
Expose `max` and `min`, alongside the existing `active` and `usage` metrics.
This commit is contained in:
parent
4c8d35db43
commit
5001b038cb
|
|
@ -28,9 +28,9 @@ import javax.sql.DataSource;
|
|||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.actuate.metrics.Metric;
|
||||
import org.springframework.boot.autoconfigure.jdbc.metadata.DataSourcePoolMetadataProviders;
|
||||
import org.springframework.boot.autoconfigure.jdbc.metadata.DataSourcePoolMetadata;
|
||||
import org.springframework.boot.autoconfigure.jdbc.metadata.DataSourcePoolMetadataProvider;
|
||||
import org.springframework.boot.autoconfigure.jdbc.metadata.DataSourcePoolMetadataProviders;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
|
||||
|
|
@ -62,7 +62,8 @@ public class DataSourcePublicMetrics implements PublicMetrics {
|
|||
String beanName = entry.getKey();
|
||||
DataSource bean = entry.getValue();
|
||||
String prefix = createPrefix(beanName, bean, bean.equals(primaryDataSource));
|
||||
DataSourcePoolMetadata poolMetadata = provider.getDataSourcePoolMetadata(bean);
|
||||
DataSourcePoolMetadata poolMetadata = provider
|
||||
.getDataSourcePoolMetadata(bean);
|
||||
if (poolMetadata != null) {
|
||||
this.metadataByPrefix.put(prefix, poolMetadata);
|
||||
}
|
||||
|
|
@ -76,9 +77,11 @@ public class DataSourcePublicMetrics implements PublicMetrics {
|
|||
.entrySet()) {
|
||||
String prefix = entry.getKey();
|
||||
prefix = (prefix.endsWith(".") ? prefix : prefix + ".");
|
||||
DataSourcePoolMetadata dataSourceMetadata = entry.getValue();
|
||||
addMetric(metrics, prefix + "active", dataSourceMetadata.getActive());
|
||||
addMetric(metrics, prefix + "usage", dataSourceMetadata.getUsage());
|
||||
DataSourcePoolMetadata metadata = entry.getValue();
|
||||
addMetric(metrics, prefix + "max", metadata.getMax());
|
||||
addMetric(metrics, prefix + "min", metadata.getMin());
|
||||
addMetric(metrics, prefix + "active", metadata.getActive());
|
||||
addMetric(metrics, prefix + "usage", metadata.getUsage());
|
||||
}
|
||||
return metrics;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -634,26 +634,35 @@ The `gauge` shows the last response time for a request. So the last request to `
|
|||
NOTE: In this example we are actually accessing the endpoint over HTTP using the
|
||||
`/metrics` URL, this explains why `metrics` appears in the response.
|
||||
|
||||
|
||||
|
||||
[[production-ready-datasource-metrics]]
|
||||
=== DataSource metrics
|
||||
The following metrics are exposed for each supported `DataSource` defined in your
|
||||
application:
|
||||
|
||||
The following metrics are available for each data source defined in the application: the
|
||||
number of allocated connection(s) (`.active`) and the current usage of the connection
|
||||
pool (`.usage`).
|
||||
* The maximum number connections (`datasource.xxx.max`).
|
||||
* The minimum number of connections (`datasource.xxx.min`).
|
||||
* The number of active connections (`datasource.xxx.active`)
|
||||
* The current usage of the connection pool (`datasource.xxx.usage`).
|
||||
|
||||
All data source metrics share the `datasource.` prefix. The prefix is further qualified for
|
||||
each data source:
|
||||
All data source metrics share the `datasource.` prefix. The prefix is further qualified
|
||||
for each data source:
|
||||
|
||||
* If the data source is the primary data source (that is either the only available data
|
||||
source or the one flagged `@Primary` amongst the existing ones), the prefix is `datasource.primary`
|
||||
* If the data source bean name ends with `dataSource`, the prefix is the name of the bean without
|
||||
it (i.e. `datasource.batch` for `batchDataSource`)
|
||||
* In all other cases, the name of the bean is used
|
||||
source or the one flagged `@Primary` amongst the existing ones), the prefix is
|
||||
`datasource.primary`.
|
||||
* If the data source bean name ends with `dataSource`, the prefix is the name of the bean
|
||||
without `dataSource` (i.e. `datasource.batch` for `batchDataSource`).
|
||||
* In all other cases, the name of the bean is used.
|
||||
|
||||
It is possible to override part or all of those defaults by registering a bean with a
|
||||
customized version of `DataSourcePublicMetrics`. By default, Spring Boot provides metadata
|
||||
for all supported datasources; you can add additional `DataSourcePoolMetadataProvider`
|
||||
beans if your favorite data source isn't supported out of the box. See
|
||||
`DataSourcePoolMetadataProvidersConfiguration` for examples.
|
||||
|
||||
|
||||
It is possible to override part or all of those defaults by registering a bean with a customized
|
||||
version of `DataSourcePublicMetrics`. Spring Boot provides those metadata for all supported
|
||||
datasource; you can provide a `DataSourceMetadata` implementation for your favorite data source,
|
||||
check `DatasourceMetadataProvidersConfiguration` for more details.
|
||||
|
||||
[[production-ready-recording-metrics]]
|
||||
=== Recording your own metrics
|
||||
|
|
|
|||
Loading…
Reference in New Issue