Redis 监视器
# Redis 监视器
Redis客户端通过执行MONITOR
命令可以将自己变为一个监视器,实时地接受并打印出服务器当前处理的命令请求的相关信息。
此时,当其他客户端向服务器发送一条命令请求时,服务器除了会处理这条命令请求之外,还会将这条命令请求的信息发送给所有监视器。
# 使用monitor命令
Redis 客户端1 作为监视器:
127.0.0.1:6379> monitor
OK
1648644597.609619 [0 127.0.0.1:52467] "set" "k1" "testmonitor"
Redis 客户端2 执行命令:
127.0.0.1:6379> set k1 testmonitor
OK
# 监视器的实现
redisServer
维护一个 monitors
的链表,记录自己的监视器,每次收到 MONITOR 命令之后,将客户端追加到链表尾。
void monitorCommand(redisClient *c) {
/* ignore MONITOR if already slave or in monitor mode */
if (c->flags & REDIS_SLAVE) return;
c->flags |= (REDIS_SLAVE|REDIS_MONITOR);
listAddNodeTail(server.monitors,c);
addReply(c,shared.ok);
}
利用call函数实现向监视器发送命令:
/* Call() is the core of Redis execution of a command */
void call(redisClient *c, int flags) {
// ......
long long dirty, start = ustime(), duration;
int client_old_flags = c->flags;
/* Sent the command to clients in MONITOR mode, only if the commands are * not generated from reading an AOF. */
if (listLength(server.monitors) &&
!server.loading &&
!(c->cmd->flags & REDIS_CMD_SKIP_MONITOR)) {
// 将命令打包为协议RESP发送给监视器。
replicationFeedMonitors(c,server.monitors,c->db->id,c->argv,c->argc);
}
// ......
}
# Redis监控平台
Redis的监控平台主要有:grafana、prometheus以及redis_exporter。
- Grafana (opens new window) 是一个开箱即用的可视化工具,具有功能齐全的度量仪表盘和图形编辑器,有灵活丰富的图形化选项,可以混合多种风格,支持多个数据源特点。
- Prometheus是一个开源的服务监控系统,它通过HTTP协议从远程的机器收集数据并存储在本地的时序数据库上。
- redis_exporter为Prometheus提供了redis指标的导出,配合Prometheus以及grafana进行可视化及监控。
上次更新: 5/30/2023, 11:42:20 PM