Skip to content

Commit

Permalink
Feat: Added ETW logging support for CNS. (#2700)
Browse files Browse the repository at this point in the history
* Added ETW logging support for CNS.

* Renamed unused parameter. Removed punctuation mark from error message.

* Added flag to write logs to ETW. Zap logger writes only to ETW in windows. And is NIL in Linux.

* Enable ETW logging in CNS through config.

* Provide flexibility on zap logging format.

* renamed zap logger instance.

* Renamed method.

* Update cns/logger/cnslogger_windows.go

Co-authored-by: Evan Baker <rbtr@users.noreply.github.com>
Signed-off-by: sivakami-projects <126191544+sivakami-projects@users.noreply.github.com>

* Update cns/logger/cnslogger_linux.go

Co-authored-by: Evan Baker <rbtr@users.noreply.github.com>
Signed-off-by: sivakami-projects <126191544+sivakami-projects@users.noreply.github.com>

* Use Nop core until zap is implemented for all the logs in CNS.

Co-authored-by: Evan Baker <rbtr@users.noreply.github.com>
Signed-off-by: sivakami-projects <126191544+sivakami-projects@users.noreply.github.com>

* return Nopcore for Linux until zap is fully implmemented. Removed flags added to ensure zap instance is not nil.

* cganged method name.

* Moved zap logger intilization in cnslogger file. Code polishes.

---------

Signed-off-by: sivakami-projects <126191544+sivakami-projects@users.noreply.github.com>
Co-authored-by: sivakami.sde@gmail.com <t-sivakamis@microsoft.com>
Co-authored-by: Evan Baker <rbtr@users.noreply.github.com>
  • Loading branch information
3 people authored Apr 26, 2024
1 parent 008dadc commit 4ae9842
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
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

0 comments on commit 4ae9842

Please sign in to comment.