Skip to content

Commit

Permalink
Add CONFIG SET and GET loglevel feature in Sentinel (redis#11214)
Browse files Browse the repository at this point in the history
Till now Sentinel allowed modifying the log level in the config file, but not at runtime.
this makes it possible to tune the log level at runtime
  • Loading branch information
hwware authored Nov 20, 2022
1 parent 203b12e commit 2f41177
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
8 changes: 8 additions & 0 deletions sentinel.conf
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ daemonize no
# location here.
pidfile /var/run/redis-sentinel.pid

# Specify the server verbosity level.
# This can be one of:
# debug (a lot of information, useful for development/testing)
# verbose (many rarely useful info, but not a mess like the debug level)
# notice (moderately verbose, what you want in production probably)
# warning (only very important / critical messages are logged)
loglevel notice

# Specify the log file name. Also the empty string can be used to force
# Sentinel to log on the standard output. Note that if you use standard
# output for logging but daemonize, logs will be sent to /dev/null
Expand Down
28 changes: 27 additions & 1 deletion src/sentinel.c
Original file line number Diff line number Diff line change
Expand Up @@ -3182,7 +3182,17 @@ void sentinelSendPeriodicCommands(sentinelRedisInstance *ri) {

/* =========================== SENTINEL command ============================= */

/* SENTINEL CONFIG SET <option> */
const char* getLogLevel() {
switch (server.verbosity) {
case LL_DEBUG: return "debug";
case LL_VERBOSE: return "verbose";
case LL_NOTICE: return "notice";
case LL_WARNING: return "warning";
}
return "unknown";
}

/* SENTINEL CONFIG SET <option> <value>*/
void sentinelConfigSetCommand(client *c) {
robj *o = c->argv[3];
robj *val = c->argv[4];
Expand Down Expand Up @@ -3213,6 +3223,17 @@ void sentinelConfigSetCommand(client *c) {
sentinel.sentinel_auth_pass = sdslen(val->ptr) == 0 ?
NULL : sdsdup(val->ptr);
drop_conns = 1;
} else if (!strcasecmp(o->ptr, "loglevel")) {
if (!strcasecmp(val->ptr, "debug"))
server.verbosity = LL_DEBUG;
else if (!strcasecmp(val->ptr, "verbose"))
server.verbosity = LL_VERBOSE;
else if (!strcasecmp(val->ptr, "notice"))
server.verbosity = LL_NOTICE;
else if (!strcasecmp(val->ptr, "warning"))
server.verbosity = LL_WARNING;
else
goto badfmt;
} else {
addReplyErrorFormat(c, "Invalid argument '%s' to SENTINEL CONFIG SET",
(char *) o->ptr);
Expand Down Expand Up @@ -3275,6 +3296,11 @@ void sentinelConfigGetCommand(client *c) {
matches++;
}

if (stringmatch(pattern, "loglevel", 1)) {
addReplyBulkCString(c, "loglevel");
addReplyBulkCString(c, getLogLevel());
matches++;
}
setDeferredMapLen(c, replylen, matches);
}

Expand Down
8 changes: 8 additions & 0 deletions tests/sentinel/tests/01-conf-update.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,11 @@ test "After Sentinel 1 is restarted, its config gets updated" {
test "New master [join $addr {:}] role matches" {
assert {[RI $master_id role] eq {master}}
}

test "Update log level" {
set current_loglevel [S 0 SENTINEL CONFIG GET loglevel]
assert {[lindex $current_loglevel 1] == {notice}}
S 0 SENTINEL CONFIG SET loglevel warning
set updated_loglevel [S 0 SENTINEL CONFIG GET loglevel]
assert {[lindex $updated_loglevel 1] == {warning}}
}

0 comments on commit 2f41177

Please sign in to comment.