java 应用基于mongo driver监控简单说明
mongo java 版本的driver提供了比较多的metrics,我们之需要实现对应的Listener就可以方便的监控mongo 应用了,以下是一些简单的说明
直接使用java driver 模式
public class TestCommandListener implements CommandListener {
@Override
public void commandStarted(final CommandStartedEvent event) {
System.out.println(String.format("Sent command '%s:%s' with id %s to database '%s' "
+ "on connection '%s' to server '%s'",
event.getCommandName(),
event.getCommand().get(event.getCommandName()),
event.getRequestId(),
event.getDatabaseName(),
event.getConnectionDescription()
.getConnectionId(),
event.getConnectionDescription().getServerAddress()));
}
@Override
public void commandSucceeded(final CommandSucceededEvent event) {
System.out.println(String.format("Successfully executed command '%s' with id %s "
+ "on connection '%s' to server '%s'",
event.getCommandName(),
event.getRequestId(),
event.getConnectionDescription()
.getConnectionId(),
event.getConnectionDescription().getServerAddress()));
}
@Override
public void commandFailed(final CommandFailedEvent event) {
System.out.println(String.format("Failed execution of command '%s' with id %s "
+ "on connection '%s' to server '%s' with exception '%s'",
event.getCommandName(),
event.getRequestId(),
event.getConnectionDescription()
.getConnectionId(),
event.getConnectionDescription().getServerAddress(),
event.getThrowable()));
}
}
MongoClientSettings settings = MongoClientSettings.builder()
.addCommandListener(new TestCommandListener())
.build();
MongoClient client = MongoClients.create(settings);
spring boot 集成
核心是基于micrometer包装的一些mongodb提供的metrics,我们需要自定义MongoClientFactoryBean进行相关
Listener 暴露metrics 的注册
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;
}
}
说明
对于spring 以及spring boot 应用推荐集成micrometer进行应用的监控,灵活高效而且支持多种监控后端,是一把利器,集成prometheus
以及grafana 我们可以比较方便的观测系统的健康情况
参考资料
https://mongodb.github.io/mongo-java-driver/3.8/driver/reference/monitoring/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
2018-09-30 hasura graphql server 集成gatsby