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
24 changes: 23 additions & 1 deletion cns/logger/cnslogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package logger

import (
"fmt"
"os"
"sync"

"github.com/Azure/azure-container-networking/aitelemetry"
"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 +20,8 @@ type CNSLogger struct {
DisableMetricLogging bool
DisableEventLogging bool

zapLogger *zap.Logger

m sync.RWMutex
Orchestrator string
NodeID string
Expand All @@ -28,7 +33,20 @@ 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)

platformCore, err := getPlatformCores(zapcore.DebugLevel, jsonEncoder)
if err != nil {
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,
zapLogger: zapLogger,
}, nil
}

func (c *CNSLogger) InitAI(aiConfig aitelemetry.AIConfig, disableTraceLogging, disableMetricLogging, disableEventLogging bool) {
Expand Down Expand Up @@ -69,6 +87,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 +99,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 +111,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 +123,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
9 changes: 9 additions & 0 deletions cns/logger/cnslogger_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package logger

import (
"go.uber.org/zap/zapcore"
)

func getPlatformCores(zapcore.Level, zapcore.Encoder) (zapcore.Core, error) {
return zapcore.NewNopCore(), nil
}
27 changes: 27 additions & 0 deletions cns/logger/cnslogger_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package logger

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

const (
etwCNSEventName = "AzureCNS"
)

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, 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
}
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