Redis

关注公众号 jb51net

关闭
首页 > 数据库 > Redis > Redis slowlog使用

Redis slowlog使用和实现

作者:卓航

slowlog是redis server记录查询命令执行时间的日志模块, 本文就来介绍一下slowlog使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

说明

slowlog是redis server记录查询命令执行时间的日志模块,

查询执行时间指一个查询命令所耗费的时间,不包括客户端响应,发送回复等IO操作。

slowlog保存在redis内存中,读写速度比较快,

配置

slow log主要由两个参数控制,

slowlog-log-slower-than指对执行时间大于多少微秒(microsecond)的查询进行记录。

slowlog-max-len指slowlog保存多少条日志,超过指定长度的日志,需要删除最早的日志.

配置由两种方法:

  1. 修改配置文件中slowlog-log-slower-than、slowlog-max-len参数
  2. 使用CONFIG SET命令动态修改slowlog-log-slower-than、slowlog-max-len参数值

命令

slow len:获取当前slow log中长度

slow reset:重置slow log

slow get [count]:返回slow log中指定数量的log信息,默认为10,log信息包括:id, timestamp, time in microseconds, arguments array, client IP and port, client name。

实现

slowlog采用list数据结构,如果执行时间大于slowlog-log-slower-than,追加到

void call(client *c, int flags) {

    //如果当前在 AOF 加载上下文,不要写 slowlog / latency / INFO stats
    int update_command_stats = !isAOFLoadingContext();

    //......
    if (update_command_stats && !(c->flags & CLIENT_BLOCKED))
        slowlogPushCurrentCommand(c, real_cmd, c->duration);
    //......
    
}

void slowlogPushCurrentCommand(client *c, struct redisCommand *cmd, ustime_t duration) {
    //是否需要纪录slowlog
    if (cmd->flags & CMD_SKIP_SLOWLOG)
        return;
    robj **argv = c->original_argv ? c->original_argv : c->argv;
    int argc = c->original_argv ? c->original_argc : c->argc;
    slowlogPushEntryIfNeeded(c,argv,argc,duration);
}

slowlogPushEntryIfNeeded函数把命令参数和执行时间写入到server.slowlog列表中

void slowlogPushEntryIfNeeded(client *c, robj **argv, int argc, long long duration) {
    //判断slowlog_log_slower_than配置是否小于0,是否记录日志
    if (server.slowlog_log_slower_than < 0) return; /* Slowlog disabled */
    //如果执行时间大于slowlog_log_slower_than时间 追加到slow log list头部中
    if (duration >= server.slowlog_log_slower_than)
        listAddNodeHead(server.slowlog,
                        slowlogCreateEntry(c,argv,argc,duration));
    //判断slow log list长度是否大于slowlog_max_len,
    //如果大于slowlog_max_len 删除list尾部元素
    /* Remove old entries if needed. */
    while (listLength(server.slowlog) > server.slowlog_max_len)
        listDelNode(server.slowlog,listLast(server.slowlog));
}

到此这篇关于Redis slowlog使用和实现的文章就介绍到这了,更多相关Redis slowlog使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文