From 90e9374533f1630b3a9598277d1a7f34f1a2775d Mon Sep 17 00:00:00 2001 From: Nam Nguyen Date: Mon, 24 Jun 2024 16:36:57 +0200 Subject: [PATCH] CLOUDP-253916 - readinessProbe: send to stdout as well, give option for compression and reduce default size (#1561) --- cmd/readiness/main.go | 21 +++++++++++++++++---- pkg/readiness/config/config.go | 27 ++++++++++++++++++--------- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/cmd/readiness/main.go b/cmd/readiness/main.go index 14d369b22..ca3bfffb1 100644 --- a/cmd/readiness/main.go +++ b/cmd/readiness/main.go @@ -208,12 +208,25 @@ func parseHealthStatus(reader io.Reader) (health.Status, error) { } func initLogger(l *lumberjack.Logger) { - log := zap.New(zapcore.NewCore( + consoleCore := zapcore.NewCore( zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()), - zapcore.AddSync(l), - zap.DebugLevel, - ), zap.Development()) + zapcore.AddSync(os.Stdout), + zap.DebugLevel) + + cores := []zapcore.Core{consoleCore} + if config.ReadBoolWitDefault(config.WithAgentFileLogging, "true") { + fileCore := zapcore.NewCore( + zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()), + zapcore.AddSync(l), + zap.DebugLevel) + cores = append(cores, fileCore) + } + + core := zapcore.NewTee(cores...) + log := zap.New(core, zap.Development()) logger = log.Sugar() + + logger.Infof("logging configuration: %+v", l) } func main() { diff --git a/pkg/readiness/config/config.go b/pkg/readiness/config/config.go index d520faf7e..aa0357c33 100644 --- a/pkg/readiness/config/config.go +++ b/pkg/readiness/config/config.go @@ -15,15 +15,17 @@ import ( const ( DefaultAgentHealthStatusFilePath = "/var/log/mongodb-mms-automation/agent-health-status.json" AgentHealthStatusFilePathEnv = "AGENT_STATUS_FILEPATH" + WithAgentFileLogging = "MDB_WITH_AGENT_FILE_LOGGING" - defaultLogPath = "/var/log/mongodb-mms-automation/readiness.log" - podNamespaceEnv = "POD_NAMESPACE" - automationConfigSecretEnv = "AUTOMATION_CONFIG_MAP" //nolint - logPathEnv = "LOG_FILE_PATH" - hostNameEnv = "HOSTNAME" - readinessProbeLoggerBackups = "READINESS_PROBE_LOGGER_BACKUPS" - readinessProbeLoggerMaxSize = "READINESS_PROBE_LOGGER_MAX_SIZE" - readinessProbeLoggerMaxAge = "READINESS_PROBE_LOGGER_MAX_AGE" + defaultLogPath = "/var/log/mongodb-mms-automation/readiness.log" + podNamespaceEnv = "POD_NAMESPACE" + automationConfigSecretEnv = "AUTOMATION_CONFIG_MAP" //nolint + logPathEnv = "LOG_FILE_PATH" + hostNameEnv = "HOSTNAME" + readinessProbeLoggerBackups = "READINESS_PROBE_LOGGER_BACKUPS" + readinessProbeLoggerMaxSize = "READINESS_PROBE_LOGGER_MAX_SIZE" + readinessProbeLoggerMaxAge = "READINESS_PROBE_LOGGER_MAX_AGE" + readinessProbeLoggerCompress = "READINESS_PROBE_LOGGER_COMPRESS" ) type Config struct { @@ -71,8 +73,9 @@ func GetLogger() *lumberjack.Logger { logger := &lumberjack.Logger{ Filename: readinessProbeLogFilePath(), MaxBackups: readIntOrDefault(readinessProbeLoggerBackups, 5), - MaxSize: readInt(readinessProbeLoggerMaxSize), + MaxSize: readIntOrDefault(readinessProbeLoggerMaxSize, 5), MaxAge: readInt(readinessProbeLoggerMaxAge), + Compress: ReadBoolWitDefault(readinessProbeLoggerCompress, "false"), } return logger } @@ -105,3 +108,9 @@ func readIntOrDefault(envVarName string, defaultValue int) int { } return intValue } + +// ReadBoolWitDefault returns the boolean value of an envvar of the given name. +func ReadBoolWitDefault(envVarName string, defaultValue string) bool { + envVar := GetEnvOrDefault(envVarName, defaultValue) + return strings.TrimSpace(strings.ToLower(envVar)) == "true" +}