From 6ef392d4851fdd94b2139ea169fd04b5a1622619 Mon Sep 17 00:00:00 2001 From: "sivakami.sde@gmail.com" Date: Sun, 21 Apr 2024 09:58:29 -0700 Subject: [PATCH 01/13] Added ETW logging support for CNS. --- cns/logger/cnslogger.go | 18 ++++++++++++++++- cns/logger/cnslogger_linux.go | 14 ++++++++++++++ cns/logger/cnslogger_windows.go | 34 +++++++++++++++++++++++++++++++++ cns/logger/zaplogger.go | 18 +++++++++++++++++ 4 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 cns/logger/cnslogger_linux.go create mode 100644 cns/logger/cnslogger_windows.go create mode 100644 cns/logger/zaplogger.go diff --git a/cns/logger/cnslogger.go b/cns/logger/cnslogger.go index 294a89c761..7d0d9f06fb 100644 --- a/cns/logger/cnslogger.go +++ b/cns/logger/cnslogger.go @@ -8,6 +8,8 @@ import ( "github.com/Azure/azure-container-networking/cns/types" "github.com/Azure/azure-container-networking/log" "github.com/pkg/errors" + "go.uber.org/zap" + "go.uber.org/zap/zapcore" ) type CNSLogger struct { @@ -17,6 +19,8 @@ type CNSLogger struct { DisableMetricLogging bool DisableEventLogging bool + ETWLogger *zap.Logger + m sync.RWMutex Orchestrator string NodeID string @@ -28,7 +32,15 @@ func NewCNSLogger(fileName string, logLevel, logTarget int, logDir string) (*CNS return nil, errors.Wrap(err, "could not get new logger") } - return &CNSLogger{logger: l}, nil + etwLogger, err := initZapLogger(zapcore.DebugLevel) + if err != nil { + return nil, errors.Wrap(err, "could not get ETW logger") + } + + return &CNSLogger{ + logger: l, + ETWLogger: etwLogger, + }, nil } func (c *CNSLogger) InitAI(aiConfig aitelemetry.AIConfig, disableTraceLogging, disableMetricLogging, disableEventLogging bool) { @@ -69,6 +81,7 @@ func (c *CNSLogger) SetContextDetails(orchestrator, nodeID string) { func (c *CNSLogger) Printf(format string, args ...any) { c.logger.Logf(format, args...) + c.ETWLogger.Info(fmt.Sprintf(format, args...)) if c.th == nil || c.DisableTraceLogging { return @@ -80,6 +93,7 @@ func (c *CNSLogger) Printf(format string, args ...any) { func (c *CNSLogger) Debugf(format string, args ...any) { c.logger.Debugf(format, args...) + c.ETWLogger.Debug(fmt.Sprintf(format, args...)) if c.th == nil || c.DisableTraceLogging { return @@ -91,6 +105,7 @@ func (c *CNSLogger) Debugf(format string, args ...any) { func (c *CNSLogger) Warnf(format string, args ...any) { c.logger.Warnf(format, args...) + c.ETWLogger.Warn(fmt.Sprintf(format, args...)) if c.th == nil || c.DisableTraceLogging { return @@ -102,6 +117,7 @@ func (c *CNSLogger) Warnf(format string, args ...any) { func (c *CNSLogger) Errorf(format string, args ...any) { c.logger.Errorf(format, args...) + c.ETWLogger.Error(fmt.Sprintf(format, args...)) if c.th == nil || c.DisableTraceLogging { return diff --git a/cns/logger/cnslogger_linux.go b/cns/logger/cnslogger_linux.go new file mode 100644 index 0000000000..eaf29b74c7 --- /dev/null +++ b/cns/logger/cnslogger_linux.go @@ -0,0 +1,14 @@ +package logger + +import ( + "go.uber.org/zap/zapcore" +) + +const ( + // LogPath is the path where log files are stored. + LogPath = "/var/log/" +) + +func GetPlatformCores(loggingLevel zapcore.Level) (zapcore.Core, error) { + return nil, nil +} diff --git a/cns/logger/cnslogger_windows.go b/cns/logger/cnslogger_windows.go new file mode 100644 index 0000000000..c5af828b0e --- /dev/null +++ b/cns/logger/cnslogger_windows.go @@ -0,0 +1,34 @@ +package logger + +import ( + "github.com/Azure/azure-container-networking/zapetw" + "github.com/pkg/errors" + "go.uber.org/zap" + "go.uber.org/zap/zapcore" +) + +const ( + // LogPath is the path where log files are stored. + LogPath = "" + etwCNSEventName = "AzureCNS" +) + +func GetPlatformCores(loggingLevel zapcore.Level) (zapcore.Core, error) { + etwcore, err := GetETWCore(loggingLevel) + if err != nil { + return nil, errors.Wrap(err, "failed to get ETW core.") + } + return etwcore, nil +} + +func GetETWCore(loggingLevel zapcore.Level) (zapcore.Core, error) { + encoderConfig := zap.NewProductionEncoderConfig() + encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder + jsonEncoder := zapcore.NewJSONEncoder(encoderConfig) + + etwcore, err := zapetw.NewETWCore(etwCNSEventName, jsonEncoder, loggingLevel) + if err != nil { + return nil, errors.Wrap(err, "failed to create ETW core") + } + return etwcore, nil +} diff --git a/cns/logger/zaplogger.go b/cns/logger/zaplogger.go new file mode 100644 index 0000000000..13944e0997 --- /dev/null +++ b/cns/logger/zaplogger.go @@ -0,0 +1,18 @@ +package logger + +import ( + "os" + + "github.com/pkg/errors" + "go.uber.org/zap" + "go.uber.org/zap/zapcore" +) + +func initZapLogger(loggingLevel zapcore.Level) (*zap.Logger, error) { + platformCore, err := GetPlatformCores(loggingLevel) + if err != nil { + return nil, errors.Wrap(err, "failed to get Platform cores") + } + // Create a new logger with the platform core. + return zap.New(platformCore, zap.AddCaller()).With(zap.Int("pid", os.Getpid())), nil +} From 08adae3f24a59165e8d4896d056d45feebfb0d7a Mon Sep 17 00:00:00 2001 From: "sivakami.sde@gmail.com" Date: Sun, 21 Apr 2024 10:47:23 -0700 Subject: [PATCH 02/13] Renamed unused parameter. Removed punctuation mark from error message. --- cns/logger/cnslogger_linux.go | 2 +- cns/logger/cnslogger_windows.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cns/logger/cnslogger_linux.go b/cns/logger/cnslogger_linux.go index eaf29b74c7..9074bfa492 100644 --- a/cns/logger/cnslogger_linux.go +++ b/cns/logger/cnslogger_linux.go @@ -9,6 +9,6 @@ const ( LogPath = "/var/log/" ) -func GetPlatformCores(loggingLevel zapcore.Level) (zapcore.Core, error) { +func GetPlatformCores(_ zapcore.Level) (zapcore.Core, error) { return nil, nil } diff --git a/cns/logger/cnslogger_windows.go b/cns/logger/cnslogger_windows.go index c5af828b0e..9857d0de1a 100644 --- a/cns/logger/cnslogger_windows.go +++ b/cns/logger/cnslogger_windows.go @@ -16,7 +16,7 @@ const ( func GetPlatformCores(loggingLevel zapcore.Level) (zapcore.Core, error) { etwcore, err := GetETWCore(loggingLevel) if err != nil { - return nil, errors.Wrap(err, "failed to get ETW core.") + return nil, errors.Wrap(err, "failed to get ETW core") } return etwcore, nil } From cac5febe90fd1064809e97b7b2ff54b5609e2786 Mon Sep 17 00:00:00 2001 From: "sivakami.sde@gmail.com" Date: Mon, 22 Apr 2024 07:23:30 -0700 Subject: [PATCH 03/13] Added flag to write logs to ETW. Zap logger writes only to ETW in windows. And is NIL in Linux. --- cns/configuration/configuration.go | 1 + cns/logger/cnslogger.go | 27 ++++++++++++++++++++++----- cns/logger/log.go | 4 ++++ cns/service/main.go | 3 +++ 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/cns/configuration/configuration.go b/cns/configuration/configuration.go index 44da9c3e87..dab2195c2f 100644 --- a/cns/configuration/configuration.go +++ b/cns/configuration/configuration.go @@ -57,6 +57,7 @@ type CNSConfig struct { UseHTTPS bool WatchPods bool `json:"-"` WireserverIP string + EnableETWLogging bool } type TelemetrySettings struct { diff --git a/cns/logger/cnslogger.go b/cns/logger/cnslogger.go index 7d0d9f06fb..df15c3f18a 100644 --- a/cns/logger/cnslogger.go +++ b/cns/logger/cnslogger.go @@ -19,7 +19,8 @@ type CNSLogger struct { DisableMetricLogging bool DisableEventLogging bool - ETWLogger *zap.Logger + ETWLogger *zap.Logger + enableETWLogging bool m sync.RWMutex Orchestrator string @@ -43,6 +44,10 @@ func NewCNSLogger(fileName string, logLevel, logTarget int, logDir string) (*CNS }, nil } +func (c *CNSLogger) EnableETWLogging(enableETWLogging bool) { + c.enableETWLogging = enableETWLogging +} + func (c *CNSLogger) InitAI(aiConfig aitelemetry.AIConfig, disableTraceLogging, disableMetricLogging, disableEventLogging bool) { c.InitAIWithIKey(aiConfig, aiMetadata, disableTraceLogging, disableMetricLogging, disableEventLogging) } @@ -81,7 +86,10 @@ func (c *CNSLogger) SetContextDetails(orchestrator, nodeID string) { func (c *CNSLogger) Printf(format string, args ...any) { c.logger.Logf(format, args...) - c.ETWLogger.Info(fmt.Sprintf(format, args...)) + + if c.enableETWLogging { + c.ETWLogger.Info(fmt.Sprintf(format, args...)) + } if c.th == nil || c.DisableTraceLogging { return @@ -93,7 +101,10 @@ func (c *CNSLogger) Printf(format string, args ...any) { func (c *CNSLogger) Debugf(format string, args ...any) { c.logger.Debugf(format, args...) - c.ETWLogger.Debug(fmt.Sprintf(format, args...)) + + if c.enableETWLogging { + c.ETWLogger.Debug(fmt.Sprintf(format, args...)) + } if c.th == nil || c.DisableTraceLogging { return @@ -105,7 +116,10 @@ func (c *CNSLogger) Debugf(format string, args ...any) { func (c *CNSLogger) Warnf(format string, args ...any) { c.logger.Warnf(format, args...) - c.ETWLogger.Warn(fmt.Sprintf(format, args...)) + + if c.enableETWLogging { + c.ETWLogger.Warn(fmt.Sprintf(format, args...)) + } if c.th == nil || c.DisableTraceLogging { return @@ -117,7 +131,10 @@ func (c *CNSLogger) Warnf(format string, args ...any) { func (c *CNSLogger) Errorf(format string, args ...any) { c.logger.Errorf(format, args...) - c.ETWLogger.Error(fmt.Sprintf(format, args...)) + + if c.enableETWLogging { + c.ETWLogger.Error(fmt.Sprintf(format, args...)) + } if c.th == nil || c.DisableTraceLogging { return diff --git a/cns/logger/log.go b/cns/logger/log.go index 1ded6d42f1..6fab19534a 100644 --- a/cns/logger/log.go +++ b/cns/logger/log.go @@ -33,6 +33,10 @@ func SetContextDetails(orchestrator, nodeID string) { Log.SetContextDetails(orchestrator, nodeID) } +func EnableETWLogging(enableETWLogging bool) { + Log.EnableETWLogging(enableETWLogging) +} + func Printf(format string, args ...any) { Log.Printf(format, args...) } diff --git a/cns/service/main.go b/cns/service/main.go index 5a0bac7dc0..0ea48e9627 100644 --- a/cns/service/main.go +++ b/cns/service/main.go @@ -578,6 +578,9 @@ func main() { } } + if cnsconfig.EnableETWLogging { + logger.EnableETWLogging(cnsconfig.EnableETWLogging) + } logger.Printf("[Azure CNS] Using config: %+v", cnsconfig) _, envEnableConflistGeneration := os.LookupEnv(envVarEnableCNIConflistGeneration) From e73bb85a003e58b52c90df435a3c6064e16024fa Mon Sep 17 00:00:00 2001 From: "sivakami.sde@gmail.com" Date: Tue, 23 Apr 2024 10:53:32 -0700 Subject: [PATCH 04/13] Enable ETW logging in CNS through config. --- cns/azure-cns-windows.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cns/azure-cns-windows.yaml b/cns/azure-cns-windows.yaml index 1b0eba16cd..b96f76c32f 100644 --- a/cns/azure-cns-windows.yaml +++ b/cns/azure-cns-windows.yaml @@ -112,5 +112,6 @@ data: "NodeSyncIntervalInSeconds": 30 }, "ChannelMode": "CRD", - "InitializeFromCNI": true + "InitializeFromCNI": true, + "EnableETWLogging": true } From 335431754d7f2cb92f6d60caf515c0d9992bc2af Mon Sep 17 00:00:00 2001 From: "sivakami.sde@gmail.com" Date: Tue, 23 Apr 2024 12:05:30 -0700 Subject: [PATCH 05/13] Provide flexibility on zap logging format. --- cns/logger/cnslogger.go | 2 +- cns/logger/cnslogger_linux.go | 2 +- cns/logger/cnslogger_windows.go | 13 ++++--------- cns/logger/zaplogger.go | 10 ++++++++-- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/cns/logger/cnslogger.go b/cns/logger/cnslogger.go index df15c3f18a..3fb98561d6 100644 --- a/cns/logger/cnslogger.go +++ b/cns/logger/cnslogger.go @@ -33,7 +33,7 @@ func NewCNSLogger(fileName string, logLevel, logTarget int, logDir string) (*CNS return nil, errors.Wrap(err, "could not get new logger") } - etwLogger, err := initZapLogger(zapcore.DebugLevel) + etwLogger, err := initZapLogger(zapcore.DebugLevel, getJsonEncoder()) if err != nil { return nil, errors.Wrap(err, "could not get ETW logger") } diff --git a/cns/logger/cnslogger_linux.go b/cns/logger/cnslogger_linux.go index 9074bfa492..8468a1fc11 100644 --- a/cns/logger/cnslogger_linux.go +++ b/cns/logger/cnslogger_linux.go @@ -9,6 +9,6 @@ const ( LogPath = "/var/log/" ) -func GetPlatformCores(_ zapcore.Level) (zapcore.Core, error) { +func GetPlatformCores(_ zapcore.Level, _ zapcore.Encoder) (zapcore.Core, error) { return nil, nil } diff --git a/cns/logger/cnslogger_windows.go b/cns/logger/cnslogger_windows.go index 9857d0de1a..f5c06abfec 100644 --- a/cns/logger/cnslogger_windows.go +++ b/cns/logger/cnslogger_windows.go @@ -3,7 +3,6 @@ package logger import ( "github.com/Azure/azure-container-networking/zapetw" "github.com/pkg/errors" - "go.uber.org/zap" "go.uber.org/zap/zapcore" ) @@ -13,20 +12,16 @@ const ( etwCNSEventName = "AzureCNS" ) -func GetPlatformCores(loggingLevel zapcore.Level) (zapcore.Core, error) { - etwcore, err := GetETWCore(loggingLevel) +func GetPlatformCores(loggingLevel zapcore.Level, encoder zapcore.Encoder) (zapcore.Core, error) { + etwcore, err := GetETWCore(loggingLevel, encoder) if err != nil { return nil, errors.Wrap(err, "failed to get ETW core") } return etwcore, nil } -func GetETWCore(loggingLevel zapcore.Level) (zapcore.Core, error) { - encoderConfig := zap.NewProductionEncoderConfig() - encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder - jsonEncoder := zapcore.NewJSONEncoder(encoderConfig) - - etwcore, err := zapetw.NewETWCore(etwCNSEventName, jsonEncoder, loggingLevel) +func GetETWCore(loggingLevel zapcore.Level, encoder zapcore.Encoder) (zapcore.Core, error) { + etwcore, err := zapetw.NewETWCore(etwCNSEventName, encoder, loggingLevel) if err != nil { return nil, errors.Wrap(err, "failed to create ETW core") } diff --git a/cns/logger/zaplogger.go b/cns/logger/zaplogger.go index 13944e0997..4960b90b23 100644 --- a/cns/logger/zaplogger.go +++ b/cns/logger/zaplogger.go @@ -8,11 +8,17 @@ import ( "go.uber.org/zap/zapcore" ) -func initZapLogger(loggingLevel zapcore.Level) (*zap.Logger, error) { - platformCore, err := GetPlatformCores(loggingLevel) +func initZapLogger(loggingLevel zapcore.Level, encoder zapcore.Encoder) (*zap.Logger, error) { + platformCore, err := GetPlatformCores(loggingLevel, encoder) if err != nil { return nil, errors.Wrap(err, "failed to get Platform cores") } // Create a new logger with the platform core. return zap.New(platformCore, zap.AddCaller()).With(zap.Int("pid", os.Getpid())), nil } + +func getJsonEncoder() zapcore.Encoder { + encoderConfig := zap.NewProductionEncoderConfig() + encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder + return zapcore.NewJSONEncoder(encoderConfig) +} From 0a4ac6075570f517b52f61aef3b4f5f2e093a1b0 Mon Sep 17 00:00:00 2001 From: "sivakami.sde@gmail.com" Date: Tue, 23 Apr 2024 12:08:45 -0700 Subject: [PATCH 06/13] renamed zap logger instance. --- cns/logger/cnslogger.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/cns/logger/cnslogger.go b/cns/logger/cnslogger.go index 3fb98561d6..8704a86a73 100644 --- a/cns/logger/cnslogger.go +++ b/cns/logger/cnslogger.go @@ -19,7 +19,7 @@ type CNSLogger struct { DisableMetricLogging bool DisableEventLogging bool - ETWLogger *zap.Logger + zapLogger *zap.Logger enableETWLogging bool m sync.RWMutex @@ -33,14 +33,14 @@ func NewCNSLogger(fileName string, logLevel, logTarget int, logDir string) (*CNS return nil, errors.Wrap(err, "could not get new logger") } - etwLogger, err := initZapLogger(zapcore.DebugLevel, getJsonEncoder()) + zapLogger, err := initZapLogger(zapcore.DebugLevel, getJsonEncoder()) if err != nil { return nil, errors.Wrap(err, "could not get ETW logger") } return &CNSLogger{ logger: l, - ETWLogger: etwLogger, + zapLogger: zapLogger, }, nil } @@ -88,7 +88,7 @@ func (c *CNSLogger) Printf(format string, args ...any) { c.logger.Logf(format, args...) if c.enableETWLogging { - c.ETWLogger.Info(fmt.Sprintf(format, args...)) + c.zapLogger.Info(fmt.Sprintf(format, args...)) } if c.th == nil || c.DisableTraceLogging { @@ -103,7 +103,7 @@ func (c *CNSLogger) Debugf(format string, args ...any) { c.logger.Debugf(format, args...) if c.enableETWLogging { - c.ETWLogger.Debug(fmt.Sprintf(format, args...)) + c.zapLogger.Debug(fmt.Sprintf(format, args...)) } if c.th == nil || c.DisableTraceLogging { @@ -118,7 +118,7 @@ func (c *CNSLogger) Warnf(format string, args ...any) { c.logger.Warnf(format, args...) if c.enableETWLogging { - c.ETWLogger.Warn(fmt.Sprintf(format, args...)) + c.zapLogger.Warn(fmt.Sprintf(format, args...)) } if c.th == nil || c.DisableTraceLogging { @@ -133,7 +133,7 @@ func (c *CNSLogger) Errorf(format string, args ...any) { c.logger.Errorf(format, args...) if c.enableETWLogging { - c.ETWLogger.Error(fmt.Sprintf(format, args...)) + c.zapLogger.Error(fmt.Sprintf(format, args...)) } if c.th == nil || c.DisableTraceLogging { From bd4983410919a021449dd1df210869bceb97028d Mon Sep 17 00:00:00 2001 From: "sivakami.sde@gmail.com" Date: Tue, 23 Apr 2024 12:17:58 -0700 Subject: [PATCH 07/13] Renamed method. --- cns/logger/cnslogger.go | 2 +- cns/logger/zaplogger.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cns/logger/cnslogger.go b/cns/logger/cnslogger.go index 8704a86a73..09bd5ba25f 100644 --- a/cns/logger/cnslogger.go +++ b/cns/logger/cnslogger.go @@ -33,7 +33,7 @@ func NewCNSLogger(fileName string, logLevel, logTarget int, logDir string) (*CNS return nil, errors.Wrap(err, "could not get new logger") } - zapLogger, err := initZapLogger(zapcore.DebugLevel, getJsonEncoder()) + zapLogger, err := initZapLogger(zapcore.DebugLevel, getJSONEncoder()) if err != nil { return nil, errors.Wrap(err, "could not get ETW logger") } diff --git a/cns/logger/zaplogger.go b/cns/logger/zaplogger.go index 4960b90b23..77b7f9464f 100644 --- a/cns/logger/zaplogger.go +++ b/cns/logger/zaplogger.go @@ -17,7 +17,7 @@ func initZapLogger(loggingLevel zapcore.Level, encoder zapcore.Encoder) (*zap.Lo return zap.New(platformCore, zap.AddCaller()).With(zap.Int("pid", os.Getpid())), nil } -func getJsonEncoder() zapcore.Encoder { +func getJSONEncoder() zapcore.Encoder { encoderConfig := zap.NewProductionEncoderConfig() encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder return zapcore.NewJSONEncoder(encoderConfig) From 128cf3d6cb1053f182f3c0647e782c9a9c94166b Mon Sep 17 00:00:00 2001 From: sivakami-projects <126191544+sivakami-projects@users.noreply.github.com> Date: Tue, 23 Apr 2024 13:21:36 -0700 Subject: [PATCH 08/13] Update cns/logger/cnslogger_windows.go Co-authored-by: Evan Baker Signed-off-by: sivakami-projects <126191544+sivakami-projects@users.noreply.github.com> --- cns/logger/cnslogger_windows.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cns/logger/cnslogger_windows.go b/cns/logger/cnslogger_windows.go index f5c06abfec..2504bc6fb5 100644 --- a/cns/logger/cnslogger_windows.go +++ b/cns/logger/cnslogger_windows.go @@ -20,7 +20,7 @@ func GetPlatformCores(loggingLevel zapcore.Level, encoder zapcore.Encoder) (zapc return etwcore, nil } -func GetETWCore(loggingLevel zapcore.Level, encoder zapcore.Encoder) (zapcore.Core, error) { +func getETWCore(loggingLevel zapcore.Level, encoder zapcore.Encoder) (zapcore.Core, error) { etwcore, err := zapetw.NewETWCore(etwCNSEventName, encoder, loggingLevel) if err != nil { return nil, errors.Wrap(err, "failed to create ETW core") From 3e76d5e22d7d99eafe16260fca675e06015d6edd Mon Sep 17 00:00:00 2001 From: sivakami-projects <126191544+sivakami-projects@users.noreply.github.com> Date: Tue, 23 Apr 2024 13:21:47 -0700 Subject: [PATCH 09/13] Update cns/logger/cnslogger_linux.go Co-authored-by: Evan Baker Signed-off-by: sivakami-projects <126191544+sivakami-projects@users.noreply.github.com> --- cns/logger/cnslogger_linux.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cns/logger/cnslogger_linux.go b/cns/logger/cnslogger_linux.go index 8468a1fc11..f994f13963 100644 --- a/cns/logger/cnslogger_linux.go +++ b/cns/logger/cnslogger_linux.go @@ -9,6 +9,6 @@ const ( LogPath = "/var/log/" ) -func GetPlatformCores(_ zapcore.Level, _ zapcore.Encoder) (zapcore.Core, error) { +func GetPlatformCores(zapcore.Level, zapcore.Encoder) (zapcore.Core, error) { return nil, nil } From a4c30cbc8e6a8e11ad823c5358d40c04c78b5878 Mon Sep 17 00:00:00 2001 From: sivakami-projects <126191544+sivakami-projects@users.noreply.github.com> Date: Tue, 23 Apr 2024 13:22:53 -0700 Subject: [PATCH 10/13] Use Nop core until zap is implemented for all the logs in CNS. Co-authored-by: Evan Baker Signed-off-by: sivakami-projects <126191544+sivakami-projects@users.noreply.github.com> --- cns/logger/cnslogger_linux.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cns/logger/cnslogger_linux.go b/cns/logger/cnslogger_linux.go index f994f13963..192670ac93 100644 --- a/cns/logger/cnslogger_linux.go +++ b/cns/logger/cnslogger_linux.go @@ -10,5 +10,5 @@ const ( ) func GetPlatformCores(zapcore.Level, zapcore.Encoder) (zapcore.Core, error) { - return nil, nil + return zapcore.NewNopCore(), nil } From a86f69030ab593a76a54b32e05f895d228fdf0e8 Mon Sep 17 00:00:00 2001 From: "sivakami.sde@gmail.com" Date: Tue, 23 Apr 2024 13:31:57 -0700 Subject: [PATCH 11/13] return Nopcore for Linux until zap is fully implmemented. Removed flags added to ensure zap instance is not nil. --- cns/azure-cns-windows.yaml | 3 +-- cns/configuration/configuration.go | 1 - cns/logger/cnslogger.go | 33 +++++++++--------------------- cns/logger/log.go | 4 ---- cns/logger/zaplogger.go | 6 ------ cns/service/main.go | 4 ---- 6 files changed, 11 insertions(+), 40 deletions(-) diff --git a/cns/azure-cns-windows.yaml b/cns/azure-cns-windows.yaml index b96f76c32f..1b0eba16cd 100644 --- a/cns/azure-cns-windows.yaml +++ b/cns/azure-cns-windows.yaml @@ -112,6 +112,5 @@ data: "NodeSyncIntervalInSeconds": 30 }, "ChannelMode": "CRD", - "InitializeFromCNI": true, - "EnableETWLogging": true + "InitializeFromCNI": true } diff --git a/cns/configuration/configuration.go b/cns/configuration/configuration.go index dab2195c2f..44da9c3e87 100644 --- a/cns/configuration/configuration.go +++ b/cns/configuration/configuration.go @@ -57,7 +57,6 @@ type CNSConfig struct { UseHTTPS bool WatchPods bool `json:"-"` WireserverIP string - EnableETWLogging bool } type TelemetrySettings struct { diff --git a/cns/logger/cnslogger.go b/cns/logger/cnslogger.go index 09bd5ba25f..0fa84fd308 100644 --- a/cns/logger/cnslogger.go +++ b/cns/logger/cnslogger.go @@ -19,8 +19,7 @@ type CNSLogger struct { DisableMetricLogging bool DisableEventLogging bool - zapLogger *zap.Logger - enableETWLogging bool + zapLogger *zap.Logger m sync.RWMutex Orchestrator string @@ -33,7 +32,11 @@ func NewCNSLogger(fileName string, logLevel, logTarget int, logDir string) (*CNS return nil, errors.Wrap(err, "could not get new logger") } - zapLogger, err := initZapLogger(zapcore.DebugLevel, getJSONEncoder()) + encoderConfig := zap.NewProductionEncoderConfig() + encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder + jsonEncoder := zapcore.NewJSONEncoder(encoderConfig) + + zapLogger, err := initZapLogger(zapcore.DebugLevel, jsonEncoder) if err != nil { return nil, errors.Wrap(err, "could not get ETW logger") } @@ -44,10 +47,6 @@ func NewCNSLogger(fileName string, logLevel, logTarget int, logDir string) (*CNS }, nil } -func (c *CNSLogger) EnableETWLogging(enableETWLogging bool) { - c.enableETWLogging = enableETWLogging -} - func (c *CNSLogger) InitAI(aiConfig aitelemetry.AIConfig, disableTraceLogging, disableMetricLogging, disableEventLogging bool) { c.InitAIWithIKey(aiConfig, aiMetadata, disableTraceLogging, disableMetricLogging, disableEventLogging) } @@ -86,10 +85,7 @@ func (c *CNSLogger) SetContextDetails(orchestrator, nodeID string) { func (c *CNSLogger) Printf(format string, args ...any) { c.logger.Logf(format, args...) - - if c.enableETWLogging { - c.zapLogger.Info(fmt.Sprintf(format, args...)) - } + c.zapLogger.Info(fmt.Sprintf(format, args...)) if c.th == nil || c.DisableTraceLogging { return @@ -101,10 +97,7 @@ func (c *CNSLogger) Printf(format string, args ...any) { func (c *CNSLogger) Debugf(format string, args ...any) { c.logger.Debugf(format, args...) - - if c.enableETWLogging { - c.zapLogger.Debug(fmt.Sprintf(format, args...)) - } + c.zapLogger.Debug(fmt.Sprintf(format, args...)) if c.th == nil || c.DisableTraceLogging { return @@ -116,10 +109,7 @@ func (c *CNSLogger) Debugf(format string, args ...any) { func (c *CNSLogger) Warnf(format string, args ...any) { c.logger.Warnf(format, args...) - - if c.enableETWLogging { - c.zapLogger.Warn(fmt.Sprintf(format, args...)) - } + c.zapLogger.Warn(fmt.Sprintf(format, args...)) if c.th == nil || c.DisableTraceLogging { return @@ -131,10 +121,7 @@ func (c *CNSLogger) Warnf(format string, args ...any) { func (c *CNSLogger) Errorf(format string, args ...any) { c.logger.Errorf(format, args...) - - if c.enableETWLogging { - c.zapLogger.Error(fmt.Sprintf(format, args...)) - } + c.zapLogger.Error(fmt.Sprintf(format, args...)) if c.th == nil || c.DisableTraceLogging { return diff --git a/cns/logger/log.go b/cns/logger/log.go index 6fab19534a..1ded6d42f1 100644 --- a/cns/logger/log.go +++ b/cns/logger/log.go @@ -33,10 +33,6 @@ func SetContextDetails(orchestrator, nodeID string) { Log.SetContextDetails(orchestrator, nodeID) } -func EnableETWLogging(enableETWLogging bool) { - Log.EnableETWLogging(enableETWLogging) -} - func Printf(format string, args ...any) { Log.Printf(format, args...) } diff --git a/cns/logger/zaplogger.go b/cns/logger/zaplogger.go index 77b7f9464f..247c86ff23 100644 --- a/cns/logger/zaplogger.go +++ b/cns/logger/zaplogger.go @@ -16,9 +16,3 @@ func initZapLogger(loggingLevel zapcore.Level, encoder zapcore.Encoder) (*zap.Lo // Create a new logger with the platform core. return zap.New(platformCore, zap.AddCaller()).With(zap.Int("pid", os.Getpid())), nil } - -func getJSONEncoder() zapcore.Encoder { - encoderConfig := zap.NewProductionEncoderConfig() - encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder - return zapcore.NewJSONEncoder(encoderConfig) -} diff --git a/cns/service/main.go b/cns/service/main.go index 0ea48e9627..442f8d3f75 100644 --- a/cns/service/main.go +++ b/cns/service/main.go @@ -577,10 +577,6 @@ func main() { logger.InitAI(aiConfig, ts.DisableTrace, ts.DisableMetric, ts.DisableEvent) } } - - if cnsconfig.EnableETWLogging { - logger.EnableETWLogging(cnsconfig.EnableETWLogging) - } logger.Printf("[Azure CNS] Using config: %+v", cnsconfig) _, envEnableConflistGeneration := os.LookupEnv(envVarEnableCNIConflistGeneration) From 9961faa41278e069206d3e15ee28d016f4dcfcac Mon Sep 17 00:00:00 2001 From: "sivakami.sde@gmail.com" Date: Tue, 23 Apr 2024 13:44:36 -0700 Subject: [PATCH 12/13] cganged method name. --- cns/logger/cnslogger_windows.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cns/logger/cnslogger_windows.go b/cns/logger/cnslogger_windows.go index 2504bc6fb5..b343c49eb2 100644 --- a/cns/logger/cnslogger_windows.go +++ b/cns/logger/cnslogger_windows.go @@ -13,7 +13,7 @@ const ( ) func GetPlatformCores(loggingLevel zapcore.Level, encoder zapcore.Encoder) (zapcore.Core, error) { - etwcore, err := GetETWCore(loggingLevel, encoder) + etwcore, err := getETWCore(loggingLevel, encoder) if err != nil { return nil, errors.Wrap(err, "failed to get ETW core") } From 1e5d9b26f303598ad0a23e1450efbccca2694e9b Mon Sep 17 00:00:00 2001 From: "sivakami.sde@gmail.com" Date: Tue, 23 Apr 2024 15:57:58 -0700 Subject: [PATCH 13/13] Moved zap logger intilization in cnslogger file. Code polishes. --- cns/logger/cnslogger.go | 6 ++++-- cns/logger/cnslogger_linux.go | 7 +------ cns/logger/cnslogger_windows.go | 4 +--- cns/logger/zaplogger.go | 18 ------------------ 4 files changed, 6 insertions(+), 29 deletions(-) delete mode 100644 cns/logger/zaplogger.go diff --git a/cns/logger/cnslogger.go b/cns/logger/cnslogger.go index 0fa84fd308..51c93c0e0e 100644 --- a/cns/logger/cnslogger.go +++ b/cns/logger/cnslogger.go @@ -2,6 +2,7 @@ package logger import ( "fmt" + "os" "sync" "github.com/Azure/azure-container-networking/aitelemetry" @@ -36,10 +37,11 @@ func NewCNSLogger(fileName string, logLevel, logTarget int, logDir string) (*CNS encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder jsonEncoder := zapcore.NewJSONEncoder(encoderConfig) - zapLogger, err := initZapLogger(zapcore.DebugLevel, jsonEncoder) + platformCore, err := getPlatformCores(zapcore.DebugLevel, jsonEncoder) if err != nil { - return nil, errors.Wrap(err, "could not get ETW logger") + l.Errorf("Failed to get zap Platform cores: %v", err) } + zapLogger := zap.New(platformCore, zap.AddCaller()).With(zap.Int("pid", os.Getpid())) return &CNSLogger{ logger: l, diff --git a/cns/logger/cnslogger_linux.go b/cns/logger/cnslogger_linux.go index 192670ac93..8a74c61a94 100644 --- a/cns/logger/cnslogger_linux.go +++ b/cns/logger/cnslogger_linux.go @@ -4,11 +4,6 @@ import ( "go.uber.org/zap/zapcore" ) -const ( - // LogPath is the path where log files are stored. - LogPath = "/var/log/" -) - -func GetPlatformCores(zapcore.Level, zapcore.Encoder) (zapcore.Core, error) { +func getPlatformCores(zapcore.Level, zapcore.Encoder) (zapcore.Core, error) { return zapcore.NewNopCore(), nil } diff --git a/cns/logger/cnslogger_windows.go b/cns/logger/cnslogger_windows.go index b343c49eb2..b909991178 100644 --- a/cns/logger/cnslogger_windows.go +++ b/cns/logger/cnslogger_windows.go @@ -7,12 +7,10 @@ import ( ) const ( - // LogPath is the path where log files are stored. - LogPath = "" etwCNSEventName = "AzureCNS" ) -func GetPlatformCores(loggingLevel zapcore.Level, encoder zapcore.Encoder) (zapcore.Core, error) { +func getPlatformCores(loggingLevel zapcore.Level, encoder zapcore.Encoder) (zapcore.Core, error) { etwcore, err := getETWCore(loggingLevel, encoder) if err != nil { return nil, errors.Wrap(err, "failed to get ETW core") diff --git a/cns/logger/zaplogger.go b/cns/logger/zaplogger.go deleted file mode 100644 index 247c86ff23..0000000000 --- a/cns/logger/zaplogger.go +++ /dev/null @@ -1,18 +0,0 @@ -package logger - -import ( - "os" - - "github.com/pkg/errors" - "go.uber.org/zap" - "go.uber.org/zap/zapcore" -) - -func initZapLogger(loggingLevel zapcore.Level, encoder zapcore.Encoder) (*zap.Logger, error) { - platformCore, err := GetPlatformCores(loggingLevel, encoder) - if err != nil { - return nil, errors.Wrap(err, "failed to get Platform cores") - } - // Create a new logger with the platform core. - return zap.New(platformCore, zap.AddCaller()).With(zap.Int("pid", os.Getpid())), nil -}