Spring MongoDB 的 Boot Micrometer 指标
Spring Boot Micrometer metrics for MongoDB
我用spring boot 2.2.5 + micrometer 1.3.5 + starter-data-mongodb
在"io.micrometer.core.instrument.binder.mongodb"下我可以看到2个类 CommandListener和ConnectionPoolListener。我想知道这些有什么用?
在执行器指标端点中,mongo 个指标不可用。
如何在执行器中启用 mongodb 的指标?例如,执行器会自动显示 RabbitMQ 的几个指标。在 MongoDB 的情况下,我也期待类似的东西。
我应该创建自己的指标吗?
添加这些侦听器并不像我想的那么简单,这完全取决于您使用哪些属性来配置 Spring 数据 MongoDB。
集成的关键是自定义用于创建 MongoClient 的 com.mongodb.MongoClientSettings 实例。如 Connecting to MongoDB with Spring
中所述,有多种可能性可以这样做
以下是一个基于 Spring Boot 2.3 的工作示例(从我们的应用程序中简化而来),假设您使用 spring.data.mongodb.uri 来指定application.properties.
中的连接字符串
package com.example.demo;
import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.binder.mongodb.MongoMetricsCommandListener;
import io.micrometer.core.instrument.binder.mongodb.MongoMetricsConnectionPoolListener;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.MongoClientFactoryBean;
@Configuration
public class MongoConfiguration {
@Bean
public MongoClientFactoryBean mongoClientFactoryBean(MongoProperties properties, MeterRegistry meterRegistry) {
MongoClientFactoryBean mongoClientFactoryBean = new MongoClientFactoryBean();
mongoClientFactoryBean.setConnectionString(new ConnectionString(properties.getUri()));
MongoClientSettings settings = MongoClientSettings.builder()
.addCommandListener(new MongoMetricsCommandListener(meterRegistry))
.applyToConnectionPoolSettings(builder ->
builder.addConnectionPoolListener(new MongoMetricsConnectionPoolListener(meterRegistry)))
.build();
mongoClientFactoryBean.setMongoClientSettings(settings);
return mongoClientFactoryBean;
}
}
不幸的是,configuring MongoDB from Spring Boot 2.2 to 2.3 中有很多东西。如果您不能使用 Spring Boot 2.3,并且您无法将其向后移植到 2.2,请告诉我。
为了启用 Spring 应用其自动配置的引导,我建议使用定制器模式:
科特林:
@Configuration
class MongoConfiguration {
@Bean
fun mongoClientSettingsBuilderCustomizer(meterRegistry: MeterRegistry) =
MongoClientSettingsBuilderCustomizer {
it.addCommandListener(MongoMetricCommandListener(meterRegistry))}
}
Java:
@Configuration
public class MongoConfiguration {
@Bean
public MongoClientSettingsBuilderCustomizer mongoClientSettingsBuilderCustomizer(MeterRegistry meterRegistry) {
return builder -> builder.addCommandListener(new MongoMetricsCommandListener(meterRegistry));
}
}
请注意,您目前不会在指标中看到与 spring 数据存储库或 mongo 集合的关系。 see open issue
编辑 (07/30/2021):
问题已得到修复,因此您可能会在当前版本中获得收集指标。
简答
用 addCommandListener
创建一个 MongoClientOptions
bean 就可以了。
@Configuration
public class MongoConfiguration {
@Autowired
private MeterRegistry meterRegistry;
@Bean
public MongoClientOptions myMongoClientOptions() {
return MongoClientOptions.builder()
.addCommandListener(new MongoMetricsCommandListener(meterRegistry)).build();
}
}
@chargue 的回答不适用于某些版本的 spring-data-mongodb
。因为 org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration
使用 MongoClientOptions
而不是 MongoClientSettings
来初始化 MongoClient
。它可能在较新版本的 spring-data-mongodb
中工作,因为根据 mongodb 的文档,MongoClientSettings
是推荐的方式。
@Bean
@ConditionalOnMissingBean(type = { "com.mongodb.MongoClient", "com.mongodb.client.MongoClient" })
public MongoClient mongo(MongoProperties properties, ObjectProvider<MongoClientOptions> options,
Environment environment) {
return new MongoClientFactory(properties, environment).createMongoClient(options.getIfAvailable());
}
确保正确设置了 prometheus 和 micrometer。您应该会在 prometheus 端点中看到 mongo 指标,如下所示:
# HELP process_cpu_usage The "recent cpu usage" for the Java Virtual Machine process
# TYPE process_cpu_usage gauge
process_cpu_usage{application="",} 0.004362672325272289
# HELP mongodb_driver_commands_seconds_max Timer of mongodb commands
# TYPE mongodb_driver_commands_seconds_max gauge
mongodb_driver_commands_seconds_max{application="",cluster_id="60b0d12d73b6df671cb4d882",command="find",server_address="",status="SUCCESS",} 34.684200332
mongodb_driver_commands_seconds_max{application="",cluster_id="60b0d12d73b6df671cb4d882",command="buildInfo",server_address="",status="SUCCESS",} 0.263514375
# HELP mongodb_driver_commands_seconds Timer of mongodb commands
# TYPE mongodb_driver_commands_seconds summary
mongodb_driver_commands_seconds_count{application="",cluster_id="60b0d12d73b6df671cb4d882",command="find",server_address="",status="SUCCESS",} 1.0
mongodb_driver_commands_seconds_sum{application="",cluster_id="60b0d12d73b6df671cb4d882",command="find",server_address="",status="SUCCESS",} 34.684200332
mongodb_driver_commands_seconds_count{application="",cluster_id="60b0d12d73b6df671cb4d882",command="buildInfo",server_address="",status="SUCCESS",} 1.0
mongodb_driver_commands_seconds_sum{application="",cluster_id="60b0d12d73b6df671cb4d882",command="buildInfo",server_address="",status="SUCCESS",} 0.263514375
我用spring boot 2.2.5 + micrometer 1.3.5 + starter-data-mongodb
在"io.micrometer.core.instrument.binder.mongodb"下我可以看到2个类 CommandListener和ConnectionPoolListener。我想知道这些有什么用?
在执行器指标端点中,mongo 个指标不可用。
如何在执行器中启用 mongodb 的指标?例如,执行器会自动显示 RabbitMQ 的几个指标。在 MongoDB 的情况下,我也期待类似的东西。 我应该创建自己的指标吗?
添加这些侦听器并不像我想的那么简单,这完全取决于您使用哪些属性来配置 Spring 数据 MongoDB。
集成的关键是自定义用于创建 MongoClient 的 com.mongodb.MongoClientSettings 实例。如 Connecting to MongoDB with Spring
中所述,有多种可能性可以这样做以下是一个基于 Spring Boot 2.3 的工作示例(从我们的应用程序中简化而来),假设您使用 spring.data.mongodb.uri 来指定application.properties.
中的连接字符串package com.example.demo;
import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.binder.mongodb.MongoMetricsCommandListener;
import io.micrometer.core.instrument.binder.mongodb.MongoMetricsConnectionPoolListener;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.MongoClientFactoryBean;
@Configuration
public class MongoConfiguration {
@Bean
public MongoClientFactoryBean mongoClientFactoryBean(MongoProperties properties, MeterRegistry meterRegistry) {
MongoClientFactoryBean mongoClientFactoryBean = new MongoClientFactoryBean();
mongoClientFactoryBean.setConnectionString(new ConnectionString(properties.getUri()));
MongoClientSettings settings = MongoClientSettings.builder()
.addCommandListener(new MongoMetricsCommandListener(meterRegistry))
.applyToConnectionPoolSettings(builder ->
builder.addConnectionPoolListener(new MongoMetricsConnectionPoolListener(meterRegistry)))
.build();
mongoClientFactoryBean.setMongoClientSettings(settings);
return mongoClientFactoryBean;
}
}
不幸的是,configuring MongoDB from Spring Boot 2.2 to 2.3 中有很多东西。如果您不能使用 Spring Boot 2.3,并且您无法将其向后移植到 2.2,请告诉我。
为了启用 Spring 应用其自动配置的引导,我建议使用定制器模式:
科特林:
@Configuration
class MongoConfiguration {
@Bean
fun mongoClientSettingsBuilderCustomizer(meterRegistry: MeterRegistry) =
MongoClientSettingsBuilderCustomizer {
it.addCommandListener(MongoMetricCommandListener(meterRegistry))}
}
Java:
@Configuration
public class MongoConfiguration {
@Bean
public MongoClientSettingsBuilderCustomizer mongoClientSettingsBuilderCustomizer(MeterRegistry meterRegistry) {
return builder -> builder.addCommandListener(new MongoMetricsCommandListener(meterRegistry));
}
}
请注意,您目前不会在指标中看到与 spring 数据存储库或 mongo 集合的关系。 see open issue
编辑 (07/30/2021):
问题已得到修复,因此您可能会在当前版本中获得收集指标。
简答
用 addCommandListener
创建一个 MongoClientOptions
bean 就可以了。
@Configuration
public class MongoConfiguration {
@Autowired
private MeterRegistry meterRegistry;
@Bean
public MongoClientOptions myMongoClientOptions() {
return MongoClientOptions.builder()
.addCommandListener(new MongoMetricsCommandListener(meterRegistry)).build();
}
}
@chargue 的回答不适用于某些版本的 spring-data-mongodb
。因为 org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration
使用 MongoClientOptions
而不是 MongoClientSettings
来初始化 MongoClient
。它可能在较新版本的 spring-data-mongodb
中工作,因为根据 mongodb 的文档,MongoClientSettings
是推荐的方式。
@Bean
@ConditionalOnMissingBean(type = { "com.mongodb.MongoClient", "com.mongodb.client.MongoClient" })
public MongoClient mongo(MongoProperties properties, ObjectProvider<MongoClientOptions> options,
Environment environment) {
return new MongoClientFactory(properties, environment).createMongoClient(options.getIfAvailable());
}
确保正确设置了 prometheus 和 micrometer。您应该会在 prometheus 端点中看到 mongo 指标,如下所示:
# HELP process_cpu_usage The "recent cpu usage" for the Java Virtual Machine process
# TYPE process_cpu_usage gauge
process_cpu_usage{application="",} 0.004362672325272289
# HELP mongodb_driver_commands_seconds_max Timer of mongodb commands
# TYPE mongodb_driver_commands_seconds_max gauge
mongodb_driver_commands_seconds_max{application="",cluster_id="60b0d12d73b6df671cb4d882",command="find",server_address="",status="SUCCESS",} 34.684200332
mongodb_driver_commands_seconds_max{application="",cluster_id="60b0d12d73b6df671cb4d882",command="buildInfo",server_address="",status="SUCCESS",} 0.263514375
# HELP mongodb_driver_commands_seconds Timer of mongodb commands
# TYPE mongodb_driver_commands_seconds summary
mongodb_driver_commands_seconds_count{application="",cluster_id="60b0d12d73b6df671cb4d882",command="find",server_address="",status="SUCCESS",} 1.0
mongodb_driver_commands_seconds_sum{application="",cluster_id="60b0d12d73b6df671cb4d882",command="find",server_address="",status="SUCCESS",} 34.684200332
mongodb_driver_commands_seconds_count{application="",cluster_id="60b0d12d73b6df671cb4d882",command="buildInfo",server_address="",status="SUCCESS",} 1.0
mongodb_driver_commands_seconds_sum{application="",cluster_id="60b0d12d73b6df671cb4d882",command="buildInfo",server_address="",status="SUCCESS",} 0.263514375