From 5001b038cbc5488127b8048e7ad312abf3b7bfe5 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 1 Sep 2014 11:59:36 -0700 Subject: [PATCH] Expose additional pool metrics Expose `max` and `min`, alongside the existing `active` and `usage` metrics. --- .../endpoint/DataSourcePublicMetrics.java | 13 ++++--- .../asciidoc/production-ready-features.adoc | 37 ++++++++++++------- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/DataSourcePublicMetrics.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/DataSourcePublicMetrics.java index a2f3f4b431..029ee37ae8 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/DataSourcePublicMetrics.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/DataSourcePublicMetrics.java @@ -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; } diff --git a/spring-boot-docs/src/main/asciidoc/production-ready-features.adoc b/spring-boot-docs/src/main/asciidoc/production-ready-features.adoc index aeb26bbe8c..334f27a879 100644 --- a/spring-boot-docs/src/main/asciidoc/production-ready-features.adoc +++ b/spring-boot-docs/src/main/asciidoc/production-ready-features.adoc @@ -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 - -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. + 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. + + [[production-ready-recording-metrics]] === Recording your own metrics