mirror of
https://github.com/fluencelabs/redis
synced 2025-04-02 07:41:04 +00:00
SLOWLOG: log offending client address and name.
This commit is contained in:
parent
ab9d398835
commit
53cb27b1d7
@ -2214,7 +2214,7 @@ void call(client *c, int flags) {
|
|||||||
char *latency_event = (c->cmd->flags & CMD_FAST) ?
|
char *latency_event = (c->cmd->flags & CMD_FAST) ?
|
||||||
"fast-command" : "command";
|
"fast-command" : "command";
|
||||||
latencyAddSampleIfNeeded(latency_event,duration/1000);
|
latencyAddSampleIfNeeded(latency_event,duration/1000);
|
||||||
slowlogPushEntryIfNeeded(c->argv,c->argc,duration);
|
slowlogPushEntryIfNeeded(c,c->argv,c->argc,duration);
|
||||||
}
|
}
|
||||||
if (flags & CMD_CALL_STATS) {
|
if (flags & CMD_CALL_STATS) {
|
||||||
c->lastcmd->microseconds += duration;
|
c->lastcmd->microseconds += duration;
|
||||||
|
@ -45,7 +45,7 @@
|
|||||||
/* Create a new slowlog entry.
|
/* Create a new slowlog entry.
|
||||||
* Incrementing the ref count of all the objects retained is up to
|
* Incrementing the ref count of all the objects retained is up to
|
||||||
* this function. */
|
* this function. */
|
||||||
slowlogEntry *slowlogCreateEntry(robj **argv, int argc, long long duration) {
|
slowlogEntry *slowlogCreateEntry(client *c, robj **argv, int argc, long long duration) {
|
||||||
slowlogEntry *se = zmalloc(sizeof(*se));
|
slowlogEntry *se = zmalloc(sizeof(*se));
|
||||||
int j, slargc = argc;
|
int j, slargc = argc;
|
||||||
|
|
||||||
@ -81,6 +81,8 @@ slowlogEntry *slowlogCreateEntry(robj **argv, int argc, long long duration) {
|
|||||||
se->time = time(NULL);
|
se->time = time(NULL);
|
||||||
se->duration = duration;
|
se->duration = duration;
|
||||||
se->id = server.slowlog_entry_id++;
|
se->id = server.slowlog_entry_id++;
|
||||||
|
se->peerid = sdsnew(getClientPeerId(c));
|
||||||
|
se->cname = c->name ? sdsnew(c->name->ptr) : sdsempty();
|
||||||
return se;
|
return se;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,6 +97,8 @@ void slowlogFreeEntry(void *septr) {
|
|||||||
for (j = 0; j < se->argc; j++)
|
for (j = 0; j < se->argc; j++)
|
||||||
decrRefCount(se->argv[j]);
|
decrRefCount(se->argv[j]);
|
||||||
zfree(se->argv);
|
zfree(se->argv);
|
||||||
|
sdsfree(se->peerid);
|
||||||
|
sdsfree(se->cname);
|
||||||
zfree(se);
|
zfree(se);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -109,10 +113,11 @@ void slowlogInit(void) {
|
|||||||
/* Push a new entry into the slow log.
|
/* Push a new entry into the slow log.
|
||||||
* This function will make sure to trim the slow log accordingly to the
|
* This function will make sure to trim the slow log accordingly to the
|
||||||
* configured max length. */
|
* configured max length. */
|
||||||
void slowlogPushEntryIfNeeded(robj **argv, int argc, long long duration) {
|
void slowlogPushEntryIfNeeded(client *c, robj **argv, int argc, long long duration) {
|
||||||
if (server.slowlog_log_slower_than < 0) return; /* Slowlog disabled */
|
if (server.slowlog_log_slower_than < 0) return; /* Slowlog disabled */
|
||||||
if (duration >= server.slowlog_log_slower_than)
|
if (duration >= server.slowlog_log_slower_than)
|
||||||
listAddNodeHead(server.slowlog,slowlogCreateEntry(argv,argc,duration));
|
listAddNodeHead(server.slowlog,
|
||||||
|
slowlogCreateEntry(c,argv,argc,duration));
|
||||||
|
|
||||||
/* Remove old entries if needed. */
|
/* Remove old entries if needed. */
|
||||||
while (listLength(server.slowlog) > server.slowlog_max_len)
|
while (listLength(server.slowlog) > server.slowlog_max_len)
|
||||||
@ -152,13 +157,15 @@ void slowlogCommand(client *c) {
|
|||||||
int j;
|
int j;
|
||||||
|
|
||||||
se = ln->value;
|
se = ln->value;
|
||||||
addReplyMultiBulkLen(c,4);
|
addReplyMultiBulkLen(c,6);
|
||||||
addReplyLongLong(c,se->id);
|
addReplyLongLong(c,se->id);
|
||||||
addReplyLongLong(c,se->time);
|
addReplyLongLong(c,se->time);
|
||||||
addReplyLongLong(c,se->duration);
|
addReplyLongLong(c,se->duration);
|
||||||
addReplyMultiBulkLen(c,se->argc);
|
addReplyMultiBulkLen(c,se->argc);
|
||||||
for (j = 0; j < se->argc; j++)
|
for (j = 0; j < se->argc; j++)
|
||||||
addReplyBulk(c,se->argv[j]);
|
addReplyBulk(c,se->argv[j]);
|
||||||
|
addReplyBulkCBuffer(c,se->peerid,sdslen(se->peerid));
|
||||||
|
addReplyBulkCBuffer(c,se->cname,sdslen(se->cname));
|
||||||
sent++;
|
sent++;
|
||||||
}
|
}
|
||||||
setDeferredMultiBulkLength(c,totentries,sent);
|
setDeferredMultiBulkLength(c,totentries,sent);
|
||||||
|
@ -37,11 +37,13 @@ typedef struct slowlogEntry {
|
|||||||
long long id; /* Unique entry identifier. */
|
long long id; /* Unique entry identifier. */
|
||||||
long long duration; /* Time spent by the query, in nanoseconds. */
|
long long duration; /* Time spent by the query, in nanoseconds. */
|
||||||
time_t time; /* Unix time at which the query was executed. */
|
time_t time; /* Unix time at which the query was executed. */
|
||||||
|
sds cname; /* Client name. */
|
||||||
|
sds peerid; /* Client network address. */
|
||||||
} slowlogEntry;
|
} slowlogEntry;
|
||||||
|
|
||||||
/* Exported API */
|
/* Exported API */
|
||||||
void slowlogInit(void);
|
void slowlogInit(void);
|
||||||
void slowlogPushEntryIfNeeded(robj **argv, int argc, long long duration);
|
void slowlogPushEntryIfNeeded(client *c, robj **argv, int argc, long long duration);
|
||||||
|
|
||||||
/* Exported commands */
|
/* Exported commands */
|
||||||
void slowlogCommand(client *c);
|
void slowlogCommand(client *c);
|
||||||
|
@ -31,12 +31,14 @@ start_server {tags {"slowlog"} overrides {slowlog-log-slower-than 1000000}} {
|
|||||||
} {0}
|
} {0}
|
||||||
|
|
||||||
test {SLOWLOG - logged entry sanity check} {
|
test {SLOWLOG - logged entry sanity check} {
|
||||||
|
r client setname foobar
|
||||||
r debug sleep 0.2
|
r debug sleep 0.2
|
||||||
set e [lindex [r slowlog get] 0]
|
set e [lindex [r slowlog get] 0]
|
||||||
assert_equal [llength $e] 4
|
assert_equal [llength $e] 6
|
||||||
assert_equal [lindex $e 0] 105
|
assert_equal [lindex $e 0] 105
|
||||||
assert_equal [expr {[lindex $e 2] > 100000}] 1
|
assert_equal [expr {[lindex $e 2] > 100000}] 1
|
||||||
assert_equal [lindex $e 3] {debug sleep 0.2}
|
assert_equal [lindex $e 3] {debug sleep 0.2}
|
||||||
|
assert_equal {foobar} [lindex $e 5]
|
||||||
}
|
}
|
||||||
|
|
||||||
test {SLOWLOG - commands with too many arguments are trimmed} {
|
test {SLOWLOG - commands with too many arguments are trimmed} {
|
||||||
@ -67,4 +69,13 @@ start_server {tags {"slowlog"} overrides {slowlog-log-slower-than 1000000}} {
|
|||||||
set e [lindex [r slowlog get] 0]
|
set e [lindex [r slowlog get] 0]
|
||||||
assert_equal [lindex $e 3] {debug sleep 0.2}
|
assert_equal [lindex $e 3] {debug sleep 0.2}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test {SLOWLOG - can clean older entires} {
|
||||||
|
r client setname lastentry_client
|
||||||
|
r config set slowlog-max-len 1
|
||||||
|
r debug sleep 0.2
|
||||||
|
assert {[llength [r slowlog get]] == 1}
|
||||||
|
set e [lindex [r slowlog get] 0]
|
||||||
|
assert_equal {lastentry_client} [lindex $e 5]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user