Polish "Auto-configure Mongo metrics"

See gh-23990
This commit is contained in:
Andy Wilkinson 2021-04-06 18:15:09 +01:00
parent 81c18214d1
commit 73e1dd8728
9 changed files with 150 additions and 21 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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,\

View File

@ -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;

View File

@ -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")

View File

@ -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]
----

View File

@ -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<Tag> commandTags(CommandEvent commandEvent) {
return java.util.Collections.emptyList();
}
}

View File

@ -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<Tag> connectionPoolTags(ConnectionPoolCreatedEvent event) {
return java.util.Collections.emptyList();
}
}

View File

@ -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;