Skip to content

Commit 4ae9842

Browse files
sivakami-projectssivakami.sde@gmail.comrbtr
authored
Feat: Added ETW logging support for CNS. (#2700)
* 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 <[email protected]> Signed-off-by: sivakami-projects <[email protected]> * Update cns/logger/cnslogger_linux.go Co-authored-by: Evan Baker <[email protected]> Signed-off-by: sivakami-projects <[email protected]> * Use Nop core until zap is implemented for all the logs in CNS. Co-authored-by: Evan Baker <[email protected]> Signed-off-by: sivakami-projects <[email protected]> * 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 <[email protected]> Co-authored-by: [email protected] <[email protected]> Co-authored-by: Evan Baker <[email protected]>
1 parent 008dadc commit 4ae9842

File tree

4 files changed

+59
-2
lines changed

4 files changed

+59
-2
lines changed

cns/logger/cnslogger.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ package logger
22

33
import (
44
"fmt"
5+
"os"
56
"sync"
67

78
"github.com/Azure/azure-container-networking/aitelemetry"
89
"github.com/Azure/azure-container-networking/cns/types"
910
"github.com/Azure/azure-container-networking/log"
1011
"github.com/pkg/errors"
12+
"go.uber.org/zap"
13+
"go.uber.org/zap/zapcore"
1114
)
1215

1316
type CNSLogger struct {
@@ -17,6 +20,8 @@ type CNSLogger struct {
1720
DisableMetricLogging bool
1821
DisableEventLogging bool
1922

23+
zapLogger *zap.Logger
24+
2025
m sync.RWMutex
2126
Orchestrator string
2227
NodeID string
@@ -28,7 +33,20 @@ func NewCNSLogger(fileName string, logLevel, logTarget int, logDir string) (*CNS
2833
return nil, errors.Wrap(err, "could not get new logger")
2934
}
3035

31-
return &CNSLogger{logger: l}, nil
36+
encoderConfig := zap.NewProductionEncoderConfig()
37+
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
38+
jsonEncoder := zapcore.NewJSONEncoder(encoderConfig)
39+
40+
platformCore, err := getPlatformCores(zapcore.DebugLevel, jsonEncoder)
41+
if err != nil {
42+
l.Errorf("Failed to get zap Platform cores: %v", err)
43+
}
44+
zapLogger := zap.New(platformCore, zap.AddCaller()).With(zap.Int("pid", os.Getpid()))
45+
46+
return &CNSLogger{
47+
logger: l,
48+
zapLogger: zapLogger,
49+
}, nil
3250
}
3351

3452
func (c *CNSLogger) InitAI(aiConfig aitelemetry.AIConfig, disableTraceLogging, disableMetricLogging, disableEventLogging bool) {
@@ -69,6 +87,7 @@ func (c *CNSLogger) SetContextDetails(orchestrator, nodeID string) {
6987

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

7392
if c.th == nil || c.DisableTraceLogging {
7493
return
@@ -80,6 +99,7 @@ func (c *CNSLogger) Printf(format string, args ...any) {
8099

81100
func (c *CNSLogger) Debugf(format string, args ...any) {
82101
c.logger.Debugf(format, args...)
102+
c.zapLogger.Debug(fmt.Sprintf(format, args...))
83103

84104
if c.th == nil || c.DisableTraceLogging {
85105
return
@@ -91,6 +111,7 @@ func (c *CNSLogger) Debugf(format string, args ...any) {
91111

92112
func (c *CNSLogger) Warnf(format string, args ...any) {
93113
c.logger.Warnf(format, args...)
114+
c.zapLogger.Warn(fmt.Sprintf(format, args...))
94115

95116
if c.th == nil || c.DisableTraceLogging {
96117
return
@@ -102,6 +123,7 @@ func (c *CNSLogger) Warnf(format string, args ...any) {
102123

103124
func (c *CNSLogger) Errorf(format string, args ...any) {
104125
c.logger.Errorf(format, args...)
126+
c.zapLogger.Error(fmt.Sprintf(format, args...))
105127

106128
if c.th == nil || c.DisableTraceLogging {
107129
return

cns/logger/cnslogger_linux.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package logger
2+
3+
import (
4+
"go.uber.org/zap/zapcore"
5+
)
6+
7+
func getPlatformCores(zapcore.Level, zapcore.Encoder) (zapcore.Core, error) {
8+
return zapcore.NewNopCore(), nil
9+
}

cns/logger/cnslogger_windows.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package logger
2+
3+
import (
4+
"github.com/Azure/azure-container-networking/zapetw"
5+
"github.com/pkg/errors"
6+
"go.uber.org/zap/zapcore"
7+
)
8+
9+
const (
10+
etwCNSEventName = "AzureCNS"
11+
)
12+
13+
func getPlatformCores(loggingLevel zapcore.Level, encoder zapcore.Encoder) (zapcore.Core, error) {
14+
etwcore, err := getETWCore(loggingLevel, encoder)
15+
if err != nil {
16+
return nil, errors.Wrap(err, "failed to get ETW core")
17+
}
18+
return etwcore, nil
19+
}
20+
21+
func getETWCore(loggingLevel zapcore.Level, encoder zapcore.Encoder) (zapcore.Core, error) {
22+
etwcore, err := zapetw.NewETWCore(etwCNSEventName, encoder, loggingLevel)
23+
if err != nil {
24+
return nil, errors.Wrap(err, "failed to create ETW core")
25+
}
26+
return etwcore, nil
27+
}

cns/service/main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,6 @@ func main() {
577577
logger.InitAI(aiConfig, ts.DisableTrace, ts.DisableMetric, ts.DisableEvent)
578578
}
579579
}
580-
581580
logger.Printf("[Azure CNS] Using config: %+v", cnsconfig)
582581

583582
_, envEnableConflistGeneration := os.LookupEnv(envVarEnableCNIConflistGeneration)

0 commit comments

Comments
 (0)