lib/logger: add -loggerWarnsPerSecondLimit command-line flag for rate limiting of WARN log messages

Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/905
This commit is contained in:
Aliaksandr Valialkin 2020-11-18 03:40:12 +02:00
parent c2afa3fdd7
commit a6b2b2c005

View File

@ -25,6 +25,8 @@ var (
errorsPerSecondLimit = flag.Int("loggerErrorsPerSecondLimit", 10, "Per-second limit on the number of ERROR messages. If more than the given number of errors "+
"are emitted per second, then the remaining errors are suppressed. Zero value disables the rate limit")
warnsPerSecondLimit = flag.Int("loggerWarnsPerSecondLimit", 10, "Per-second limit on the number of WARN messages. If more than the given number of warns "+
"are emitted per second, then the remaining warns are suppressed. Zero value disables the rate limit")
)
// Init initializes the logger.
@ -36,7 +38,7 @@ func Init() {
setLoggerOutput()
validateLoggerLevel()
validateLoggerFormat()
go errorsLoggedCleaner()
go errorsAndWarnsLoggedCleaner()
logAllFlags()
}
@ -125,14 +127,18 @@ func logLevelSkipframes(skipframes int, level, format string, args ...interface{
logMessage(level, msg, 3+skipframes)
}
func errorsLoggedCleaner() {
func errorsAndWarnsLoggedCleaner() {
for {
time.Sleep(time.Second)
atomic.StoreUint64(&errorsLogged, 0)
atomic.StoreUint64(&warnsLogged, 0)
}
}
var errorsLogged uint64
var (
errorsLogged uint64
warnsLogged uint64
)
type logWriter struct {
}
@ -149,6 +155,11 @@ func logMessage(level, msg string, skipframes int) {
return
}
}
if level == "WARN" {
if n := atomic.AddUint64(&warnsLogged, 1); *warnsPerSecondLimit > 0 && n > uint64(*warnsPerSecondLimit) {
return
}
}
timestamp := ""
if !*disableTimestamps {