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
22 changes: 21 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,8 @@ type CNSLogger struct {
DisableMetricLogging bool
DisableEventLogging bool

zapLogger *zap.Logger

m sync.RWMutex
Orchestrator string
NodeID string
Expand All @@ -28,7 +32,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
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")
rbtr marked this conversation as resolved.
Show resolved Hide resolved
}

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

func (c *CNSLogger) InitAI(aiConfig aitelemetry.AIConfig, disableTraceLogging, disableMetricLogging, disableEventLogging bool) {
Expand Down Expand Up @@ -69,6 +85,7 @@ func (c *CNSLogger) SetContextDetails(orchestrator, nodeID string) {

func (c *CNSLogger) Printf(format string, args ...any) {
c.logger.Logf(format, args...)
c.zapLogger.Info(fmt.Sprintf(format, args...))

if c.th == nil || c.DisableTraceLogging {
return
Expand All @@ -80,6 +97,7 @@ func (c *CNSLogger) Printf(format string, args ...any) {

func (c *CNSLogger) Debugf(format string, args ...any) {
c.logger.Debugf(format, args...)
c.zapLogger.Debug(fmt.Sprintf(format, args...))

if c.th == nil || c.DisableTraceLogging {
return
Expand All @@ -91,6 +109,7 @@ func (c *CNSLogger) Debugf(format string, args ...any) {

func (c *CNSLogger) Warnf(format string, args ...any) {
c.logger.Warnf(format, args...)
c.zapLogger.Warn(fmt.Sprintf(format, args...))

if c.th == nil || c.DisableTraceLogging {
return
Expand All @@ -102,6 +121,7 @@ func (c *CNSLogger) Warnf(format string, args ...any) {

func (c *CNSLogger) Errorf(format string, args ...any) {
c.logger.Errorf(format, args...)
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) {
return zapcore.NewNopCore(), nil
}
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) {
etwcore, err := zapetw.NewETWCore(etwCNSEventName, encoder, loggingLevel)
if err != nil {
return nil, errors.Wrap(err, "failed to create ETW core")
}
return etwcore, nil
}
18 changes: 18 additions & 0 deletions cns/logger/zaplogger.go
Original file line number Diff line number Diff line change
@@ -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, 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
1 change: 0 additions & 1 deletion cns/service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,6 @@ func main() {
logger.InitAI(aiConfig, ts.DisableTrace, ts.DisableMetric, ts.DisableEvent)
}
}

logger.Printf("[Azure CNS] Using config: %+v", cnsconfig)

_, envEnableConflistGeneration := os.LookupEnv(envVarEnableCNIConflistGeneration)
Expand Down
Loading