为了实现 使用 dianping CAT组件监控标准化埋点,经过长期实践已经完成了对大部分组件埋点的支持,并实现了整个链路的串通,而对MongoDB的支持一直是个难点,曾几何时做了多次尝试,也未能成功,因为从网上根本找不到方法。或许鉴于用CAT的不多,没有人贡献这类的思路,
近段时间一直在MongoDB上折腾,从客户端的角度记录MongoDB一直是个缺失。以至于出现问题时没办法快速看到相关采集的数据指标。
活不多说,这次终于最终搞定也是源自Opentracing对Mongo做Tracing监控的灵感
组件是基于Spring Boot,所以做起自动化配置来说还是比较方便的
基本原理也很简单,Mongo的客户端中MongoClientSettings中有一个commandListeners的命令监听器, 实际这里还包含了ClusterListener集群监听器,ConnectionPoolListener连接池监听器等等,众多监听器,那么我们监控Mongo的执行语句只需要命令监听器即可。
先创建一个?CatMongoAutoConfiguration 的配置类,这里我们生成一个?MongoClientSettings的Bean,在这个Bean中增加了一个 CatMongoCommandListener 命令监听器
@Configuration(proxyBeanMethods = false)
public class CatMongoAutoConfiguration {
@Bean
public MongoClientSettings catMongoClientSettings() {
return MongoClientSettings.builder().addCommandListener(new CatMongoCommandListener()).build();
}
}
那么在MongoAutoConfiguration自动配置的时候,ObjectProvider<MongoClientSettings> settings 就会被延迟加载进来。
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(MongoClient.class)
@EnableConfigurationProperties(MongoProperties.class)
@ConditionalOnMissingBean(type = "org.springframework.data.mongodb.MongoDatabaseFactory")
public class MongoAutoConfiguration {
@Bean
@ConditionalOnMissingBean(MongoClient.class)
public MongoClient mongo(MongoProperties properties, Environment environment,
ObjectProvider<MongoClientSettingsBuilderCustomizer> builderCustomizers,
ObjectProvider<MongoClientSettings> settings) {
return new MongoClientFactory(properties, environment,
builderCustomizers.orderedStream().collect(Collectors.toList()))
.createMongoClient(settings.getIfAvailable());
}
}
因此我们创建的MongoClientImpl中就包含了 带有命令监听器的Client,
返回来我们再看?CatMongoCommandListener
public class CatMongoCommandListener implements CommandListener {
/**
* Cache for (request id, Transaction) pairs
*/
private final Map<Integer, Transaction> cache = new ConcurrentHashMap<>();
@Override
public void commandStarted(CommandStartedEvent event) {
String collectionName = event.getCommand().getString(event.getCommandName()).getValue();
Transaction t = Cat.newTransaction("MongoDB", event.getDatabaseName() + "." + collectionName + "." + event.getCommandName());
try {
t.addData("requestId", event.getRequestId());
// t.addData("databaseName", event.getDatabaseName());
t.addData("connectionDescription", event.getConnectionDescription());
// t.addData("commandName", event.getCommandName());
t.addData("command", event.getCommand().toJson());
/*Event catEvent = Cat.newEvent(collectionName, event.getCommand().toJson());
try {
t.addChild(catEvent);
} finally {
catEvent.complete();
}*/
} finally {
cache.put(event.getRequestId(), t);
}
}
@Override
public void commandSucceeded(CommandSucceededEvent event) {
Transaction t = cache.remove(event.getRequestId());
try {
t.addData("response", event.getResponse());
t.addData("elapsedTime", event.getElapsedTime(TimeUnit.MILLISECONDS));
} finally {
t.setStatus(Transaction.SUCCESS);
t.complete();
}
}
@Override
public void commandFailed(CommandFailedEvent event) {
Transaction t = cache.remove(event.getRequestId());
try {
t.addData("throwable", event.getThrowable());
t.addData("elapsedTime", event.getElapsedTime(TimeUnit.MILLISECONDS));
} finally {
Cat.logError(event.getThrowable());
t.setStatus(event.getThrowable());
t.complete();
}
}
}
监听器中有三个方法?commandStarted、commandSucceeded、commandFailed 分别代表,开始,成功,失败,因为在异步的mongo驱动中监听的方法运行在不同的线程中,所以需要使用到一个Cache来缓存传递CAT的事物。那么剩下的就用CAT记录的格式及方法了,event中可以获取到Mongo在执行命令的时候相关数据库信息,连接信息,执行命令,返回结果,还有执行时间,这种监控方式可以说比较全面基于Mongo自身的底层能力实现的埋点方案。对于不论使用MongoRepository还是MongoTemplate都能够起到监听作用
最终来看效果
?
?
|