Skip to content

Commit 53cb27b

Browse files
committed
SLOWLOG: log offending client address and name.
1 parent ab9d398 commit 53cb27b

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

src/server.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2214,7 +2214,7 @@ void call(client *c, int flags) {
22142214
char *latency_event = (c->cmd->flags & CMD_FAST) ?
22152215
"fast-command" : "command";
22162216
latencyAddSampleIfNeeded(latency_event,duration/1000);
2217-
slowlogPushEntryIfNeeded(c->argv,c->argc,duration);
2217+
slowlogPushEntryIfNeeded(c,c->argv,c->argc,duration);
22182218
}
22192219
if (flags & CMD_CALL_STATS) {
22202220
c->lastcmd->microseconds += duration;

src/slowlog.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
/* Create a new slowlog entry.
4646
* Incrementing the ref count of all the objects retained is up to
4747
* this function. */
48-
slowlogEntry *slowlogCreateEntry(robj **argv, int argc, long long duration) {
48+
slowlogEntry *slowlogCreateEntry(client *c, robj **argv, int argc, long long duration) {
4949
slowlogEntry *se = zmalloc(sizeof(*se));
5050
int j, slargc = argc;
5151

@@ -81,6 +81,8 @@ slowlogEntry *slowlogCreateEntry(robj **argv, int argc, long long duration) {
8181
se->time = time(NULL);
8282
se->duration = duration;
8383
se->id = server.slowlog_entry_id++;
84+
se->peerid = sdsnew(getClientPeerId(c));
85+
se->cname = c->name ? sdsnew(c->name->ptr) : sdsempty();
8486
return se;
8587
}
8688

@@ -95,6 +97,8 @@ void slowlogFreeEntry(void *septr) {
9597
for (j = 0; j < se->argc; j++)
9698
decrRefCount(se->argv[j]);
9799
zfree(se->argv);
100+
sdsfree(se->peerid);
101+
sdsfree(se->cname);
98102
zfree(se);
99103
}
100104

@@ -109,10 +113,11 @@ void slowlogInit(void) {
109113
/* Push a new entry into the slow log.
110114
* This function will make sure to trim the slow log accordingly to the
111115
* configured max length. */
112-
void slowlogPushEntryIfNeeded(robj **argv, int argc, long long duration) {
116+
void slowlogPushEntryIfNeeded(client *c, robj **argv, int argc, long long duration) {
113117
if (server.slowlog_log_slower_than < 0) return; /* Slowlog disabled */
114118
if (duration >= server.slowlog_log_slower_than)
115-
listAddNodeHead(server.slowlog,slowlogCreateEntry(argv,argc,duration));
119+
listAddNodeHead(server.slowlog,
120+
slowlogCreateEntry(c,argv,argc,duration));
116121

117122
/* Remove old entries if needed. */
118123
while (listLength(server.slowlog) > server.slowlog_max_len)
@@ -152,13 +157,15 @@ void slowlogCommand(client *c) {
152157
int j;
153158

154159
se = ln->value;
155-
addReplyMultiBulkLen(c,4);
160+
addReplyMultiBulkLen(c,6);
156161
addReplyLongLong(c,se->id);
157162
addReplyLongLong(c,se->time);
158163
addReplyLongLong(c,se->duration);
159164
addReplyMultiBulkLen(c,se->argc);
160165
for (j = 0; j < se->argc; j++)
161166
addReplyBulk(c,se->argv[j]);
167+
addReplyBulkCBuffer(c,se->peerid,sdslen(se->peerid));
168+
addReplyBulkCBuffer(c,se->cname,sdslen(se->cname));
162169
sent++;
163170
}
164171
setDeferredMultiBulkLength(c,totentries,sent);

src/slowlog.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ typedef struct slowlogEntry {
3737
long long id; /* Unique entry identifier. */
3838
long long duration; /* Time spent by the query, in nanoseconds. */
3939
time_t time; /* Unix time at which the query was executed. */
40+
sds cname; /* Client name. */
41+
sds peerid; /* Client network address. */
4042
} slowlogEntry;
4143

4244
/* Exported API */
4345
void slowlogInit(void);
44-
void slowlogPushEntryIfNeeded(robj **argv, int argc, long long duration);
46+
void slowlogPushEntryIfNeeded(client *c, robj **argv, int argc, long long duration);
4547

4648
/* Exported commands */
4749
void slowlogCommand(client *c);

tests/unit/slowlog.tcl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@ start_server {tags {"slowlog"} overrides {slowlog-log-slower-than 1000000}} {
3131
} {0}
3232

3333
test {SLOWLOG - logged entry sanity check} {
34+
r client setname foobar
3435
r debug sleep 0.2
3536
set e [lindex [r slowlog get] 0]
36-
assert_equal [llength $e] 4
37+
assert_equal [llength $e] 6
3738
assert_equal [lindex $e 0] 105
3839
assert_equal [expr {[lindex $e 2] > 100000}] 1
3940
assert_equal [lindex $e 3] {debug sleep 0.2}
41+
assert_equal {foobar} [lindex $e 5]
4042
}
4143

4244
test {SLOWLOG - commands with too many arguments are trimmed} {
@@ -67,4 +69,13 @@ start_server {tags {"slowlog"} overrides {slowlog-log-slower-than 1000000}} {
6769
set e [lindex [r slowlog get] 0]
6870
assert_equal [lindex $e 3] {debug sleep 0.2}
6971
}
72+
73+
test {SLOWLOG - can clean older entires} {
74+
r client setname lastentry_client
75+
r config set slowlog-max-len 1
76+
r debug sleep 0.2
77+
assert {[llength [r slowlog get]] == 1}
78+
set e [lindex [r slowlog get] 0]
79+
assert_equal {lastentry_client} [lindex $e 5]
80+
}
7081
}

0 commit comments

Comments
 (0)