From 73e1dd8728525af6e81a216b5d923c7d5400ad85 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 6 Apr 2021 18:15:09 +0100 Subject: [PATCH] Polish "Auto-configure Mongo metrics" See gh-23990 --- .../MongoMetricsAutoConfiguration.java | 6 ++- .../metrics/mongo/package-info.java | 20 +++++++++ .../main/resources/META-INF/spring.factories | 2 +- .../MongoMetricsAutoConfigurationTests.java | 2 +- .../spring-boot-docs/build.gradle | 1 + .../asciidoc/production-ready-features.adoc | 30 ++++++------- ...ampleCommandTagsProviderConfiguration.java | 45 +++++++++++++++++++ ...nnectionPoolTagsProviderConfiguration.java | 45 +++++++++++++++++++ .../metrics/mongo/package-info.java | 20 +++++++++ 9 files changed, 150 insertions(+), 21 deletions(-) rename spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/{ => mongo}/MongoMetricsAutoConfiguration.java (93%) create mode 100644 spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/mongo/package-info.java rename spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/{ => mongo}/MongoMetricsAutoConfigurationTests.java (99%) create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/productionreadyfeatures/metrics/mongo/SampleCommandTagsProviderConfiguration.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/productionreadyfeatures/metrics/mongo/SampleConnectionPoolTagsProviderConfiguration.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/productionreadyfeatures/metrics/mongo/package-info.java diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/MongoMetricsAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/mongo/MongoMetricsAutoConfiguration.java similarity index 93% rename from spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/MongoMetricsAutoConfiguration.java rename to spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/mongo/MongoMetricsAutoConfiguration.java index 5be419b23d..45ca98fd8f 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/MongoMetricsAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/mongo/MongoMetricsAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.actuate.autoconfigure.metrics; +package org.springframework.boot.actuate.autoconfigure.metrics.mongo; import com.mongodb.MongoClientSettings; import io.micrometer.core.instrument.MeterRegistry; @@ -25,6 +25,8 @@ import io.micrometer.core.instrument.binder.mongodb.MongoMetricsCommandTagsProvi import io.micrometer.core.instrument.binder.mongodb.MongoMetricsConnectionPoolListener; import io.micrometer.core.instrument.binder.mongodb.MongoMetricsConnectionPoolTagsProvider; +import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration; +import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/mongo/package-info.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/mongo/package-info.java new file mode 100644 index 0000000000..ac78ff948a --- /dev/null +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/mongo/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright 2012-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Auto-configuration for Mongo metrics. + */ +package org.springframework.boot.actuate.autoconfigure.metrics.mongo; diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/spring.factories b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/spring.factories index b6d9f718e3..b19921a89c 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/spring.factories +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/spring.factories @@ -46,7 +46,6 @@ org.springframework.boot.actuate.autoconfigure.metrics.Log4J2MetricsAutoConfigur org.springframework.boot.actuate.autoconfigure.metrics.LogbackMetricsAutoConfiguration,\ org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration,\ org.springframework.boot.actuate.autoconfigure.metrics.MetricsEndpointAutoConfiguration,\ -org.springframework.boot.actuate.autoconfigure.metrics.MongoMetricsAutoConfiguration,\ org.springframework.boot.actuate.autoconfigure.metrics.SystemMetricsAutoConfiguration,\ org.springframework.boot.actuate.autoconfigure.metrics.amqp.RabbitMetricsAutoConfiguration,\ org.springframework.boot.actuate.autoconfigure.metrics.cache.CacheMetricsAutoConfiguration,\ @@ -71,6 +70,7 @@ org.springframework.boot.actuate.autoconfigure.metrics.export.wavefront.Wavefron org.springframework.boot.actuate.autoconfigure.metrics.integration.IntegrationMetricsAutoConfiguration,\ org.springframework.boot.actuate.autoconfigure.metrics.jdbc.DataSourcePoolMetricsAutoConfiguration,\ org.springframework.boot.actuate.autoconfigure.metrics.jersey.JerseyServerMetricsAutoConfiguration,\ +org.springframework.boot.actuate.autoconfigure.metrics.mongo.MongoMetricsAutoConfiguration,\ org.springframework.boot.actuate.autoconfigure.metrics.orm.jpa.HibernateMetricsAutoConfiguration,\ org.springframework.boot.actuate.autoconfigure.metrics.r2dbc.ConnectionPoolMetricsAutoConfiguration,\ org.springframework.boot.actuate.autoconfigure.metrics.web.client.HttpClientMetricsAutoConfiguration,\ diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/MongoMetricsAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/mongo/MongoMetricsAutoConfigurationTests.java similarity index 99% rename from spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/MongoMetricsAutoConfigurationTests.java rename to spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/mongo/MongoMetricsAutoConfigurationTests.java index aeabc32e12..a3d110258b 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/MongoMetricsAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/mongo/MongoMetricsAutoConfigurationTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.actuate.autoconfigure.metrics; +package org.springframework.boot.actuate.autoconfigure.metrics.mongo; import java.util.List; diff --git a/spring-boot-project/spring-boot-docs/build.gradle b/spring-boot-project/spring-boot-docs/build.gradle index c5a120403e..cb05ee94ef 100644 --- a/spring-boot-project/spring-boot-docs/build.gradle +++ b/spring-boot-project/spring-boot-docs/build.gradle @@ -80,6 +80,7 @@ dependencies { implementation("org.glassfish.jersey.core:jersey-server") implementation("org.hibernate:hibernate-jcache") implementation("org.jooq:jooq") + implementation("org.mongodb:mongodb-driver-sync") implementation("org.slf4j:jul-to-slf4j") implementation("org.springframework:spring-jdbc") implementation("org.springframework:spring-test") diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/production-ready-features.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/production-ready-features.adoc index 9a3d5f55f5..1c26e7ba03 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/production-ready-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/production-ready-features.adoc @@ -2400,8 +2400,11 @@ For more details refer to {spring-kafka-docs}#micrometer-native[Micrometer Nativ [[production-ready-metrics-mongodb]] ==== MongoDB Metrics + + +[[production-ready-metrics-mongodb-command]] ===== Command Metrics -Auto-configuration will register a `MongoMetricsCommandListener` for the auto-configured MongoClient. +Auto-configuration will register a `MongoMetricsCommandListener` with the auto-configured `MongoClient`. A timer metric with the name `mongodb.driver.commands` is created for each command issued to the underlying MongoDB driver. Each metric is tagged with the following information by default: @@ -2421,17 +2424,14 @@ Each metric is tagged with the following information by default: | Outcome of the command - one of (`SUCCESS`, `FAILED`) |=== -You can replace the default metric tags by providing a `MongoMetricsCommandTagsProvider` bean, as shown in the following example: +To replace the default metric tags, define a `MongoMetricsCommandTagsProvider` bean, as shown in the following example: -[source,java,pending-extract=true,indent=0] +[source,java,indent=0] ---- - @Bean - MongoMetricsCommandTagsProvider mongoMetricsCommandTagsProvider() { - return new MyCustomMongoMetricsCommandTagsProvider(); - } +include::{include-productionreadyfeatures}/metrics/mongo/SampleCommandTagsProviderConfiguration.java[tag=*] ---- -If you want to disable the auto-configured command metrics, you can set the following property: +To disable the auto-configured command metrics, set the following property: [source,yaml,indent=0,configprops,configblocks] ---- @@ -2443,7 +2443,7 @@ If you want to disable the auto-configured command metrics, you can set the foll ---- ===== Connection Pool Metrics -Auto-configuration will register a `MongoMetricsConnectionPoolListener` for the auto-configured MongoClient. +Auto-configuration will register a `MongoMetricsConnectionPoolListener` with the auto-configured `MongoClient`. The following gauge metrics are created for the connection pool: @@ -2462,18 +2462,14 @@ Each metric is tagged with the following information by default: | Address of the server the connection pool corresponds to |=== -You can replace the default metric tags by providing a `MongoMetricsConnectionPoolTagsProvider` bean, as shown in the following example: +To replace the default metric tags, define a `MongoMetricsConnectionPoolTagsProvider` bean, as shown in the following example: -[source,java,pending-extract=true,indent=0] +[source,java,indent=0] ---- - @Bean - @ConditionalOnMissingBean - MongoMetricsConnectionPoolTagsProvider mongoMetricsConnectionPoolTagsProvider() { - return new DefaultMongoMetricsConnectionPoolTagsProvider(); - } +include::{include-productionreadyfeatures}/metrics/mongo/SampleConnectionPoolTagsProviderConfiguration.java[tag=*] ---- -If you want to disable the auto-configured connection pool metrics, you can set the following property: +To disable the auto-configured connection pool metrics, set the following property: [source,yaml,indent=0,configprops,configblocks] ---- diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/productionreadyfeatures/metrics/mongo/SampleCommandTagsProviderConfiguration.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/productionreadyfeatures/metrics/mongo/SampleCommandTagsProviderConfiguration.java new file mode 100644 index 0000000000..62262bfcef --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/productionreadyfeatures/metrics/mongo/SampleCommandTagsProviderConfiguration.java @@ -0,0 +1,45 @@ +/* + * Copyright 2012-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.productionreadyfeatures.metrics.mongo; + +// tag::code[] +import com.mongodb.event.CommandEvent; +import io.micrometer.core.instrument.Tag; +import io.micrometer.core.instrument.binder.mongodb.MongoMetricsCommandTagsProvider; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration(proxyBeanMethods = false) +public class SampleCommandTagsProviderConfiguration { + + @Bean + MongoMetricsCommandTagsProvider customCommandTagsProvider() { + return new CustomCommandTagsProvider(); + } + +} +// end::code[] + +class CustomCommandTagsProvider implements MongoMetricsCommandTagsProvider { + + @Override + public Iterable commandTags(CommandEvent commandEvent) { + return java.util.Collections.emptyList(); + } + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/productionreadyfeatures/metrics/mongo/SampleConnectionPoolTagsProviderConfiguration.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/productionreadyfeatures/metrics/mongo/SampleConnectionPoolTagsProviderConfiguration.java new file mode 100644 index 0000000000..498687ace7 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/productionreadyfeatures/metrics/mongo/SampleConnectionPoolTagsProviderConfiguration.java @@ -0,0 +1,45 @@ +/* + * Copyright 2012-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.productionreadyfeatures.metrics.mongo; + +// tag::code[] +import com.mongodb.event.ConnectionPoolCreatedEvent; +import io.micrometer.core.instrument.Tag; +import io.micrometer.core.instrument.binder.mongodb.MongoMetricsConnectionPoolTagsProvider; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration(proxyBeanMethods = false) +public class SampleConnectionPoolTagsProviderConfiguration { + + @Bean + MongoMetricsConnectionPoolTagsProvider customConnectionPoolTagsProvider() { + return new CustomConnectionPoolTagsProvider(); + } + +} +// end::code[] + +class CustomConnectionPoolTagsProvider implements MongoMetricsConnectionPoolTagsProvider { + + @Override + public Iterable connectionPoolTags(ConnectionPoolCreatedEvent event) { + return java.util.Collections.emptyList(); + } + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/productionreadyfeatures/metrics/mongo/package-info.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/productionreadyfeatures/metrics/mongo/package-info.java new file mode 100644 index 0000000000..f3f6b51685 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/productionreadyfeatures/metrics/mongo/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright 2012-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Examples for the "Production Ready Features - Metrics - MongoDB" section. + */ +package org.springframework.boot.docs.productionreadyfeatures.metrics.mongo;