Add CONFIG SET and GET loglevel feature in Sentinel (#11214)

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
This commit is contained in:
Wen Hui 2022-11-20 05:03:00 -05:00 committed by GitHub
parent 203b12e41f
commit 2f411770c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 1 deletions

View File

@ -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

View File

@ -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];
@ -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);
@ -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);
}

View File

@ -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}}
}