我使用Spring Boot 2.2.5 +千分尺1.3.5 + starter-data-mongodb
在“ io.micrometer.core.instrument.binder.mongodb”下,我可以看到2个类CommandListener和ConnectionPoolListener。我想知道这些服务的目的是什么?
在执行器指标端点中,mongo指标不可用。
如何在执行器中启用mongodb的指标?例如,执行器自动显示RabbitMQ的多个指标。我在MongoDB的情况下也期待类似的事情。 我应该创建自己的指标吗?
答案 0 :(得分:1)
添加这些侦听器并不像我想的那样简单,这完全取决于您用于配置Spring Data MongoDB的属性。
集成的关键是自定义用于创建MongoClient的com.mongodb.MongoClientSettings实例。 Connecting to MongoDB with Spring
中记录了这样做的多种可能性以下是一个基于Spring Boot 2.3的工作示例(从我们的应用程序简化),假定您正在使用 spring.data.mongodb.uri 在应用程序中指定连接字符串。属性。
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,请告诉我。
答案 1 :(得分:1)
为了使Spring Boot应用其自动配置,我建议使用定制器模式:
@Configuration
public class MongoConfiguration {
@Bean
public MongoClientSettingsBuilderCustomizer mongoClientSettingsBuilderCustomizer(MeterRegistry meterRegistry) {
return builder -> builder.addCommandListener(new MongoMetricsCommandListener(meterRegistry));
}
}
请注意,您目前不会在指标中看到与spring数据存储库或mongo集合的关系。 see open issue
答案 2 :(得分:0)
简答
用 MongoClientOptions
创建一个 addCommandListener
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
用 MongoClient
初始化 MongoClientOptions
,而不是 MongoClientSettings
。它可能适用于较新版本的 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 和千分尺设置正确。您应该在 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