[#461] Configure logger sampling policy
Signed-off-by: Pavel Pogodaev <p.pogodaev@yadro.com>
This commit is contained in:
parent
26baf8a94e
commit
94f28ee961
3 changed files with 28 additions and 5 deletions
|
@ -78,8 +78,10 @@ var (
|
||||||
|
|
||||||
const ( // Settings.
|
const ( // Settings.
|
||||||
// Logger.
|
// Logger.
|
||||||
cfgLoggerLevel = "logger.level"
|
cfgLoggerLevel = "logger.level"
|
||||||
cfgLoggerDestination = "logger.destination"
|
cfgLoggerDestination = "logger.destination"
|
||||||
|
cfgLoggerSamplingInitial = "logger.sampling.initial"
|
||||||
|
cfgLoggerSamplingThereafter = "logger.sampling.thereafter"
|
||||||
|
|
||||||
// HttpLogging.
|
// HttpLogging.
|
||||||
cfgHTTPLoggingEnabled = "http_logging.enabled"
|
cfgHTTPLoggingEnabled = "http_logging.enabled"
|
||||||
|
@ -787,6 +789,8 @@ func newSettings() *viper.Viper {
|
||||||
// logger:
|
// logger:
|
||||||
v.SetDefault(cfgLoggerLevel, "debug")
|
v.SetDefault(cfgLoggerLevel, "debug")
|
||||||
v.SetDefault(cfgLoggerDestination, "stdout")
|
v.SetDefault(cfgLoggerDestination, "stdout")
|
||||||
|
v.SetDefault(cfgLoggerSamplingThereafter, 100)
|
||||||
|
v.SetDefault(cfgLoggerSamplingInitial, 100)
|
||||||
|
|
||||||
// http logger
|
// http logger
|
||||||
v.SetDefault(cfgHTTPLoggingEnabled, false)
|
v.SetDefault(cfgHTTPLoggingEnabled, false)
|
||||||
|
@ -1030,7 +1034,7 @@ func pickLogger(v *viper.Viper) *Logger {
|
||||||
|
|
||||||
switch dest {
|
switch dest {
|
||||||
case destinationStdout:
|
case destinationStdout:
|
||||||
return newStdoutLogger(lvl)
|
return newStdoutLogger(v, lvl)
|
||||||
case destinationJournald:
|
case destinationJournald:
|
||||||
return newJournaldLogger(lvl)
|
return newJournaldLogger(lvl)
|
||||||
default:
|
default:
|
||||||
|
@ -1045,13 +1049,14 @@ func pickLogger(v *viper.Viper) *Logger {
|
||||||
// - parameterized level (debug by default)
|
// - parameterized level (debug by default)
|
||||||
// - console encoding
|
// - console encoding
|
||||||
// - ISO8601 time encoding
|
// - ISO8601 time encoding
|
||||||
|
// - sampling intervals
|
||||||
//
|
//
|
||||||
// and atomic log level to dynamically change it.
|
// and atomic log level to dynamically change it.
|
||||||
//
|
//
|
||||||
// Logger records a stack trace for all messages at or above fatal level.
|
// Logger records a stack trace for all messages at or above fatal level.
|
||||||
//
|
//
|
||||||
// See also zapcore.Level, zap.NewProductionConfig, zap.AddStacktrace.
|
// See also zapcore.Level, zap.NewProductionConfig, zap.AddStacktrace.
|
||||||
func newStdoutLogger(lvl zapcore.Level) *Logger {
|
func newStdoutLogger(v *viper.Viper, lvl zapcore.Level) *Logger {
|
||||||
c := zap.NewProductionConfig()
|
c := zap.NewProductionConfig()
|
||||||
c.Level = zap.NewAtomicLevelAt(lvl)
|
c.Level = zap.NewAtomicLevelAt(lvl)
|
||||||
c.Encoding = "console"
|
c.Encoding = "console"
|
||||||
|
@ -1064,8 +1069,21 @@ func newStdoutLogger(lvl zapcore.Level) *Logger {
|
||||||
panic(fmt.Sprintf("build zap logger instance: %v", err))
|
panic(fmt.Sprintf("build zap logger instance: %v", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Zap samples by logging the first cgfLoggerSamplingInitial entries with a given level
|
||||||
|
// and message within the specified time interval.
|
||||||
|
// In the above config, only the first cgfLoggerSamplingInitial log entries with the same level and message
|
||||||
|
// are recorded in a one-second interval. Every other log entry will be dropped within the interval since
|
||||||
|
// cfgLoggerSamplingThereafter is specified here.
|
||||||
|
|
||||||
|
samplingCore := zapcore.NewSamplerWithOptions(
|
||||||
|
l.Core(),
|
||||||
|
time.Second,
|
||||||
|
v.GetInt(cfgLoggerSamplingInitial),
|
||||||
|
v.GetInt(cfgLoggerSamplingThereafter),
|
||||||
|
)
|
||||||
|
|
||||||
return &Logger{
|
return &Logger{
|
||||||
logger: l,
|
logger: zap.New(samplingCore),
|
||||||
lvl: c.Level,
|
lvl: c.Level,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,6 +51,8 @@ S3_GW_CONFIG=/path/to/config/yaml
|
||||||
|
|
||||||
# Logger
|
# Logger
|
||||||
S3_GW_LOGGER_LEVEL=debug
|
S3_GW_LOGGER_LEVEL=debug
|
||||||
|
S3_GW_LOGGER_SAMPLING_INITIAL=100
|
||||||
|
S3_GW_LOGGER_SAMPLING_THEREAFTER=100
|
||||||
|
|
||||||
# HTTP logger
|
# HTTP logger
|
||||||
S3_GW_HTTP_LOGGING_ENABLED=false
|
S3_GW_HTTP_LOGGING_ENABLED=false
|
||||||
|
|
|
@ -55,6 +55,9 @@ vhs:
|
||||||
logger:
|
logger:
|
||||||
level: debug
|
level: debug
|
||||||
destination: stdout
|
destination: stdout
|
||||||
|
sampling:
|
||||||
|
initial: 100
|
||||||
|
thereafter: 100
|
||||||
|
|
||||||
# log http request data (URI, headers, query, etc)
|
# log http request data (URI, headers, query, etc)
|
||||||
http_logging:
|
http_logging:
|
||||||
|
|
Loading…
Reference in a new issue