Skip to content

Commit f9ce354

Browse files
authored
backport: feat: cns logger v2 [2/2] (#3438) (#3897)
feat: cns logger v2 [2/2] (#3438) * feat: add migration shim and config for v2 logger Signed-off-by: Evan Baker <rbtr@users.noreply.github.com * add logger config to CNS config * map telemetry metadata fields and compose logger --------- Signed-off-by: Evan Baker <rbtr@users.noreply.github.com Signed-off-by: Evan Baker <rbtr@users.noreply.github.com Signed-off-by: Evan Baker <rbtr@users.noreply.github.com>
1 parent 39b2e99 commit f9ce354

File tree

17 files changed

+248
-63
lines changed

17 files changed

+248
-63
lines changed

aitelemetry/telemetrywrapper.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package aitelemetry
33
import (
44
"fmt"
55
"os"
6+
"path/filepath"
67
"runtime"
78
"time"
89

@@ -36,6 +37,8 @@ const (
3637
defaultRefreshTimeoutInSecs = 10
3738
)
3839

40+
var MetadataFile = filepath.Join(os.TempDir(), "azuremetadata.json")
41+
3942
type Level = contracts.SeverityLevel
4043

4144
const (
@@ -98,7 +101,7 @@ func getMetadata(th *telemetryHandle) {
98101

99102
// check if metadata in memory otherwise initiate wireserver request
100103
for {
101-
metadata, err = common.GetHostMetadata(metadataFile)
104+
metadata, err = common.GetHostMetadata(MetadataFile)
102105
if err == nil || th.disableMetadataRefreshThread {
103106
break
104107
}
@@ -117,14 +120,14 @@ func getMetadata(th *telemetryHandle) {
117120
th.metadata = metadata
118121
th.rwmutex.Unlock()
119122

120-
lockclient, err := processlock.NewFileLock(metadataFile + store.LockExtension)
123+
lockclient, err := processlock.NewFileLock(MetadataFile + store.LockExtension)
121124
if err != nil {
122125
log.Printf("Error initializing file lock:%v", err)
123126
return
124127
}
125128

126129
// Save metadata retrieved from wireserver to a file
127-
kvs, err := store.NewJsonFileStore(metadataFile, lockclient, nil)
130+
kvs, err := store.NewJsonFileStore(MetadataFile, lockclient, nil)
128131
if err != nil {
129132
debugLog("[AppInsights] Error initializing kvs store: %v", err)
130133
return
@@ -134,7 +137,7 @@ func getMetadata(th *telemetryHandle) {
134137
log.Errorf("getMetadata: Not able to acquire lock:%v", err)
135138
return
136139
}
137-
metadataErr := common.SaveHostMetadata(th.metadata, metadataFile)
140+
metadataErr := common.SaveHostMetadata(th.metadata, MetadataFile)
138141
err = kvs.Unlock()
139142
if err != nil {
140143
log.Errorf("getMetadata: Not able to release lock:%v", err)

aitelemetry/telemetrywrapper_linux.go

Lines changed: 0 additions & 5 deletions
This file was deleted.

aitelemetry/telemetrywrapper_windows.go

Lines changed: 0 additions & 8 deletions
This file was deleted.

cns/configuration/configuration.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/Azure/azure-container-networking/cns"
1212
"github.com/Azure/azure-container-networking/cns/logger"
13+
loggerv2 "github.com/Azure/azure-container-networking/cns/logger/v2"
1314
"github.com/Azure/azure-container-networking/common"
1415
"github.com/pkg/errors"
1516
)
@@ -31,12 +32,14 @@ type CNSConfig struct {
3132
EnableCNIConflistGeneration bool
3233
EnableIPAMv2 bool
3334
EnableK8sDevicePlugin bool
35+
EnableLoggerV2 bool
3436
EnablePprof bool
3537
EnableStateMigration bool
3638
EnableSubnetScarcity bool
3739
EnableSwiftV2 bool
3840
InitializeFromCNI bool
3941
KeyVaultSettings KeyVaultSettings
42+
Logger loggerv2.Config
4043
MSISettings MSISettings
4144
ManageEndpointState bool
4245
ManagedSettings ManagedSettings

cns/logger/cnslogger.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
// wait time for closing AI telemetry session.
1818
const waitTimeInSecs = 10
1919

20-
type CNSLogger struct {
20+
type logger struct {
2121
logger *log.Logger
2222
zapLogger *zap.Logger
2323
th ai.TelemetryHandle
@@ -30,7 +30,8 @@ type CNSLogger struct {
3030
metadata map[string]string
3131
}
3232

33-
func New(fileName string, logLevel, logTarget int, logDir string) (*CNSLogger, error) {
33+
// Deprecated: The v1 logger is deprecated. Migrate to zap using the cns/logger/v2 package.
34+
func New(fileName string, logLevel, logTarget int, logDir string) (loggershim, error) {
3435
l, err := log.NewLoggerE(fileName, logLevel, logTarget, logDir)
3536
if err != nil {
3637
return nil, errors.Wrap(err, "could not get new logger")
@@ -46,18 +47,18 @@ func New(fileName string, logLevel, logTarget int, logDir string) (*CNSLogger, e
4647
}
4748
zapLogger := zap.New(platformCore, zap.AddCaller()).With(zap.Int("pid", os.Getpid()))
4849

49-
return &CNSLogger{
50+
return &logger{
5051
logger: l,
5152
zapLogger: zapLogger,
5253
metadata: map[string]string{},
5354
}, nil
5455
}
5556

56-
func (c *CNSLogger) InitAI(aiConfig ai.AIConfig, disableTraceLogging, disableMetricLogging, disableEventLogging bool) {
57+
func (c *logger) InitAI(aiConfig ai.AIConfig, disableTraceLogging, disableMetricLogging, disableEventLogging bool) {
5758
c.InitAIWithIKey(aiConfig, aiMetadata, disableTraceLogging, disableMetricLogging, disableEventLogging)
5859
}
5960

60-
func (c *CNSLogger) InitAIWithIKey(aiConfig ai.AIConfig, instrumentationKey string, disableTraceLogging, disableMetricLogging, disableEventLogging bool) {
61+
func (c *logger) InitAIWithIKey(aiConfig ai.AIConfig, instrumentationKey string, disableTraceLogging, disableMetricLogging, disableEventLogging bool) {
6162
th, err := ai.NewAITelemetry("", instrumentationKey, aiConfig)
6263
if err != nil {
6364
c.logger.Errorf("Error initializing AI Telemetry:%v", err)
@@ -70,28 +71,28 @@ func (c *CNSLogger) InitAIWithIKey(aiConfig ai.AIConfig, instrumentationKey stri
7071
c.disableEventLogging = disableEventLogging
7172
}
7273

73-
func (c *CNSLogger) Close() {
74+
func (c *logger) Close() {
7475
c.logger.Close()
7576
if c.th != nil {
7677
c.th.Close(waitTimeInSecs)
7778
}
7879
}
7980

80-
func (c *CNSLogger) SetContextDetails(orchestrator, nodeID string) {
81+
func (c *logger) SetContextDetails(orchestrator, nodeID string) {
8182
c.logger.Logf("SetContext details called with: %v orchestrator nodeID %v", orchestrator, nodeID)
8283
c.m.Lock()
8384
c.metadata[orchestratorTypeKey] = orchestrator
8485
c.metadata[nodeIDKey] = nodeID
8586
c.m.Unlock()
8687
}
8788

88-
func (c *CNSLogger) SetAPIServer(apiserver string) {
89+
func (c *logger) SetAPIServer(apiserver string) {
8990
c.m.Lock()
9091
c.metadata[apiServerKey] = apiserver
9192
c.m.Unlock()
9293
}
9394

94-
func (c *CNSLogger) Printf(format string, args ...any) {
95+
func (c *logger) Printf(format string, args ...any) {
9596
c.logger.Logf(format, args...)
9697
c.zapLogger.Info(fmt.Sprintf(format, args...))
9798
if c.th == nil || c.disableTraceLogging {
@@ -101,7 +102,7 @@ func (c *CNSLogger) Printf(format string, args ...any) {
101102
c.sendTraceInternal(msg, ai.InfoLevel)
102103
}
103104

104-
func (c *CNSLogger) Debugf(format string, args ...any) {
105+
func (c *logger) Debugf(format string, args ...any) {
105106
c.logger.Debugf(format, args...)
106107
c.zapLogger.Debug(fmt.Sprintf(format, args...))
107108
if c.th == nil || c.disableTraceLogging {
@@ -111,7 +112,7 @@ func (c *CNSLogger) Debugf(format string, args ...any) {
111112
c.sendTraceInternal(msg, ai.DebugLevel)
112113
}
113114

114-
func (c *CNSLogger) Warnf(format string, args ...any) {
115+
func (c *logger) Warnf(format string, args ...any) {
115116
c.logger.Warnf(format, args...)
116117
c.zapLogger.Warn(fmt.Sprintf(format, args...))
117118
if c.th == nil || c.disableTraceLogging {
@@ -121,7 +122,7 @@ func (c *CNSLogger) Warnf(format string, args ...any) {
121122
c.sendTraceInternal(msg, ai.WarnLevel)
122123
}
123124

124-
func (c *CNSLogger) Errorf(format string, args ...any) {
125+
func (c *logger) Errorf(format string, args ...any) {
125126
c.logger.Errorf(format, args...)
126127
c.zapLogger.Error(fmt.Sprintf(format, args...))
127128
if c.th == nil || c.disableTraceLogging {
@@ -131,7 +132,7 @@ func (c *CNSLogger) Errorf(format string, args ...any) {
131132
c.sendTraceInternal(msg, ai.ErrorLevel)
132133
}
133134

134-
func (c *CNSLogger) Request(tag string, request any, err error) {
135+
func (c *logger) Request(tag string, request any, err error) {
135136
c.logger.Request(tag, request, err)
136137
if c.th == nil || c.disableTraceLogging {
137138
return
@@ -147,7 +148,7 @@ func (c *CNSLogger) Request(tag string, request any, err error) {
147148
c.sendTraceInternal(msg, lvl)
148149
}
149150

150-
func (c *CNSLogger) Response(tag string, response any, returnCode types.ResponseCode, err error) {
151+
func (c *logger) Response(tag string, response any, returnCode types.ResponseCode, err error) {
151152
c.logger.Response(tag, response, int(returnCode), returnCode.String(), err)
152153
if c.th == nil || c.disableTraceLogging {
153154
return
@@ -166,7 +167,7 @@ func (c *CNSLogger) Response(tag string, response any, returnCode types.Response
166167
c.sendTraceInternal(msg, lvl)
167168
}
168169

169-
func (c *CNSLogger) ResponseEx(tag string, request, response any, returnCode types.ResponseCode, err error) {
170+
func (c *logger) ResponseEx(tag string, request, response any, returnCode types.ResponseCode, err error) {
170171
c.logger.ResponseEx(tag, request, response, int(returnCode), returnCode.String(), err)
171172
if c.th == nil || c.disableTraceLogging {
172173
return
@@ -185,7 +186,7 @@ func (c *CNSLogger) ResponseEx(tag string, request, response any, returnCode typ
185186
c.sendTraceInternal(msg, lvl)
186187
}
187188

188-
func (c *CNSLogger) sendTraceInternal(msg string, lvl ai.Level) {
189+
func (c *logger) sendTraceInternal(msg string, lvl ai.Level) {
189190
report := ai.Report{
190191
Message: msg,
191192
Level: lvl,
@@ -198,7 +199,7 @@ func (c *CNSLogger) sendTraceInternal(msg string, lvl ai.Level) {
198199
c.th.TrackLog(report)
199200
}
200201

201-
func (c *CNSLogger) LogEvent(event ai.Event) {
202+
func (c *logger) LogEvent(event ai.Event) {
202203
if c.th == nil || c.disableEventLogging {
203204
return
204205
}
@@ -208,7 +209,7 @@ func (c *CNSLogger) LogEvent(event ai.Event) {
208209
c.th.TrackEvent(event)
209210
}
210211

211-
func (c *CNSLogger) SendMetric(metric ai.Metric) {
212+
func (c *logger) SendMetric(metric ai.Metric) {
212213
if c.th == nil || c.disableMetricLogging {
213214
return
214215
}

cns/logger/log.go

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,66 +6,95 @@ import (
66
"github.com/Azure/azure-container-networking/cns/types"
77
)
88

9+
type loggershim interface {
10+
Close()
11+
InitAI(aitelemetry.AIConfig, bool, bool, bool)
12+
InitAIWithIKey(aitelemetry.AIConfig, string, bool, bool, bool)
13+
SetContextDetails(string, string)
14+
SetAPIServer(string)
15+
Printf(string, ...any)
16+
Debugf(string, ...any)
17+
Warnf(string, ...any)
18+
LogEvent(aitelemetry.Event)
19+
Errorf(string, ...any)
20+
Request(string, any, error)
21+
Response(string, any, types.ResponseCode, error)
22+
ResponseEx(string, any, any, types.ResponseCode, error)
23+
SendMetric(aitelemetry.Metric)
24+
}
25+
926
var (
10-
Log *CNSLogger
11-
aiMetadata string // this var is set at build time.
27+
Log loggershim
1228
AppInsightsIKey = aiMetadata
29+
aiMetadata string // this var is set at build time.
1330
)
1431

15-
// todo: the functions below should be removed. CNSLogger should be injected where needed and not used from package level scope.
16-
32+
// Deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead.
1733
func Close() {
1834
Log.Close()
1935
}
2036

37+
// Deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead.
2138
func InitLogger(fileName string, logLevel, logTarget int, logDir string) {
2239
Log, _ = New(fileName, logLevel, logTarget, logDir)
2340
}
2441

42+
// Deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead.
2543
func InitAI(aiConfig aitelemetry.AIConfig, disableTraceLogging, disableMetricLogging, disableEventLogging bool) {
2644
Log.InitAI(aiConfig, disableTraceLogging, disableMetricLogging, disableEventLogging)
2745
}
2846

47+
// Deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead.
2948
func InitAIWithIKey(aiConfig aitelemetry.AIConfig, instrumentationKey string, disableTraceLogging, disableMetricLogging, disableEventLogging bool) {
3049
Log.InitAIWithIKey(aiConfig, instrumentationKey, disableTraceLogging, disableMetricLogging, disableEventLogging)
3150
}
3251

52+
// Deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead.
3353
func SetContextDetails(orchestrator, nodeID string) {
3454
Log.SetContextDetails(orchestrator, nodeID)
3555
}
3656

57+
// Deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead.
3758
func Printf(format string, args ...any) {
3859
Log.Printf(format, args...)
3960
}
4061

62+
// Deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead.
4163
func Debugf(format string, args ...any) {
4264
Log.Debugf(format, args...)
4365
}
4466

67+
// Deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead.
4568
func Warnf(format string, args ...any) {
4669
Log.Warnf(format, args...)
4770
}
4871

72+
// Deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead.
4973
func LogEvent(event aitelemetry.Event) {
5074
Log.LogEvent(event)
5175
}
5276

77+
// Deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead.
5378
func Errorf(format string, args ...any) {
5479
Log.Errorf(format, args...)
5580
}
5681

82+
// Deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead.
5783
func Request(tag string, request any, err error) {
5884
Log.Request(tag, request, err)
5985
}
6086

87+
// Deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead.
6188
func Response(tag string, response any, returnCode types.ResponseCode, err error) {
6289
Log.Response(tag, response, returnCode, err)
6390
}
6491

92+
// Deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead.
6593
func ResponseEx(tag string, request, response any, returnCode types.ResponseCode, err error) {
6694
Log.ResponseEx(tag, request, response, returnCode, err)
6795
}
6896

97+
// Deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead.
6998
func SendMetric(metric aitelemetry.Metric) {
7099
Log.SendMetric(metric)
71100
}

cns/logger/v2/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ func (c *Config) UnmarshalJSON(data []byte) error {
4242
return nil
4343
}
4444

45-
// Normalize checks the Config for missing or illegal values and sets them
46-
// to defaults if appropriate.
45+
// Normalize checks the Config for missing/default values and sets them
46+
// if appropriate.
4747
func (c *Config) Normalize() {
4848
if c.File != nil {
4949
if c.File.Filepath == "" {

cns/logger/v2/cores/ai.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,6 @@ func ApplicationInsightsCore(cfg *AppInsightsConfig) (zapcore.Core, func(), erro
6262
core := zapai.NewCore(cfg.level, sink)
6363
core = core.WithFieldMappers(zapai.DefaultMappers)
6464
// add normalized fields for the built-in AI Tags
65-
// TODO(rbtr): move to the caller
65+
6666
return core.With(cfg.Fields), aiclose, nil
6767
}

0 commit comments

Comments
 (0)