Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Added ETW logging support for CNS. #2700

Merged
merged 13 commits into from
Apr 26, 2024
3 changes: 2 additions & 1 deletion cns/azure-cns-windows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,6 @@ data:
"NodeSyncIntervalInSeconds": 30
},
"ChannelMode": "CRD",
"InitializeFromCNI": true
"InitializeFromCNI": true,
"EnableETWLogging": true
}
1 change: 1 addition & 0 deletions cns/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type CNSConfig struct {
UseHTTPS bool
WatchPods bool `json:"-"`
WireserverIP string
EnableETWLogging bool
rbtr marked this conversation as resolved.
Show resolved Hide resolved
}

type TelemetrySettings struct {
Expand Down
35 changes: 34 additions & 1 deletion cns/logger/cnslogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -17,6 +19,9 @@ type CNSLogger struct {
DisableMetricLogging bool
DisableEventLogging bool

zapLogger *zap.Logger
enableETWLogging bool
rbtr marked this conversation as resolved.
Show resolved Hide resolved

m sync.RWMutex
Orchestrator string
NodeID string
Expand All @@ -28,7 +33,19 @@ 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
zapLogger, err := initZapLogger(zapcore.DebugLevel, getJSONEncoder())
if err != nil {
return nil, errors.Wrap(err, "could not get ETW logger")
rbtr marked this conversation as resolved.
Show resolved Hide resolved
}

return &CNSLogger{
logger: l,
zapLogger: zapLogger,
}, nil
}

func (c *CNSLogger) EnableETWLogging(enableETWLogging bool) {
c.enableETWLogging = enableETWLogging
}

func (c *CNSLogger) InitAI(aiConfig aitelemetry.AIConfig, disableTraceLogging, disableMetricLogging, disableEventLogging bool) {
Expand Down Expand Up @@ -70,6 +87,10 @@ 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...))
}
rbtr marked this conversation as resolved.
Show resolved Hide resolved

if c.th == nil || c.DisableTraceLogging {
return
}
Expand All @@ -81,6 +102,10 @@ 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...))
}

if c.th == nil || c.DisableTraceLogging {
return
}
Expand All @@ -92,6 +117,10 @@ 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...))
}

if c.th == nil || c.DisableTraceLogging {
return
}
Expand All @@ -103,6 +132,10 @@ 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...))
}

if c.th == nil || c.DisableTraceLogging {
return
}
Expand Down
14 changes: 14 additions & 0 deletions cns/logger/cnslogger_linux.go
Original file line number Diff line number Diff line change
@@ -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(_ zapcore.Level, _ zapcore.Encoder) (zapcore.Core, error) {
sivakami-projects marked this conversation as resolved.
Show resolved Hide resolved
return nil, nil
sivakami-projects marked this conversation as resolved.
Show resolved Hide resolved
}
29 changes: 29 additions & 0 deletions cns/logger/cnslogger_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package logger

import (
"github.com/Azure/azure-container-networking/zapetw"
"github.com/pkg/errors"
"go.uber.org/zap/zapcore"
)

const (
// LogPath is the path where log files are stored.
LogPath = ""
rbtr marked this conversation as resolved.
Show resolved Hide resolved
etwCNSEventName = "AzureCNS"
)

func GetPlatformCores(loggingLevel zapcore.Level, encoder zapcore.Encoder) (zapcore.Core, error) {
rbtr marked this conversation as resolved.
Show resolved Hide resolved
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, encoder zapcore.Encoder) (zapcore.Core, error) {
sivakami-projects marked this conversation as resolved.
Show resolved Hide resolved
etwcore, err := zapetw.NewETWCore(etwCNSEventName, encoder, loggingLevel)
if err != nil {
return nil, errors.Wrap(err, "failed to create ETW core")
}
return etwcore, nil
}
4 changes: 4 additions & 0 deletions cns/logger/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}
Expand Down
24 changes: 24 additions & 0 deletions cns/logger/zaplogger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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
}
rbtr marked this conversation as resolved.
Show resolved Hide resolved

func getJSONEncoder() zapcore.Encoder {
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
return zapcore.NewJSONEncoder(encoderConfig)
}
rbtr marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 3 additions & 0 deletions cns/service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading