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: cns logger v2 [2/2] #3438

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions aitelemetry/telemetrywrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aitelemetry
import (
"fmt"
"os"
"path/filepath"
"runtime"
"time"

Expand Down Expand Up @@ -36,6 +37,8 @@ const (
defaultRefreshTimeoutInSecs = 10
)

var MetadataFile = filepath.Join(os.TempDir(), "azuremetadata.json")

type Level = contracts.SeverityLevel

const (
Expand Down Expand Up @@ -98,7 +101,7 @@ func getMetadata(th *telemetryHandle) {

// check if metadata in memory otherwise initiate wireserver request
for {
metadata, err = common.GetHostMetadata(metadataFile)
metadata, err = common.GetHostMetadata(MetadataFile)
if err == nil || th.disableMetadataRefreshThread {
break
}
Expand All @@ -117,14 +120,14 @@ func getMetadata(th *telemetryHandle) {
th.metadata = metadata
th.rwmutex.Unlock()

lockclient, err := processlock.NewFileLock(metadataFile + store.LockExtension)
lockclient, err := processlock.NewFileLock(MetadataFile + store.LockExtension)
if err != nil {
log.Printf("Error initializing file lock:%v", err)
return
}

// Save metadata retrieved from wireserver to a file
kvs, err := store.NewJsonFileStore(metadataFile, lockclient, nil)
kvs, err := store.NewJsonFileStore(MetadataFile, lockclient, nil)
if err != nil {
debugLog("[AppInsights] Error initializing kvs store: %v", err)
return
Expand All @@ -134,7 +137,7 @@ func getMetadata(th *telemetryHandle) {
log.Errorf("getMetadata: Not able to acquire lock:%v", err)
return
}
metadataErr := common.SaveHostMetadata(th.metadata, metadataFile)
metadataErr := common.SaveHostMetadata(th.metadata, MetadataFile)
err = kvs.Unlock()
if err != nil {
log.Errorf("getMetadata: Not able to release lock:%v", err)
Expand Down
5 changes: 0 additions & 5 deletions aitelemetry/telemetrywrapper_linux.go

This file was deleted.

8 changes: 0 additions & 8 deletions aitelemetry/telemetrywrapper_windows.go

This file was deleted.

3 changes: 3 additions & 0 deletions cns/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/Azure/azure-container-networking/cns"
"github.com/Azure/azure-container-networking/cns/logger"
loggerv2 "github.com/Azure/azure-container-networking/cns/logger/v2"
"github.com/Azure/azure-container-networking/common"
"github.com/pkg/errors"
)
Expand All @@ -30,12 +31,14 @@ type CNSConfig struct {
EnableCNIConflistGeneration bool
EnableIPAMv2 bool
EnableK8sDevicePlugin bool
EnableLoggerV2 bool
EnablePprof bool
EnableStateMigration bool
EnableSubnetScarcity bool
EnableSwiftV2 bool
InitializeFromCNI bool
KeyVaultSettings KeyVaultSettings
Logger loggerv2.Config
MSISettings MSISettings
ManageEndpointState bool
ManagedSettings ManagedSettings
Expand Down
37 changes: 19 additions & 18 deletions cns/logger/cnslogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
// wait time for closing AI telemetry session.
const waitTimeInSecs = 10

type CNSLogger struct {
type logger struct {
logger *log.Logger
zapLogger *zap.Logger
th ai.TelemetryHandle
Expand All @@ -30,7 +30,8 @@ type CNSLogger struct {
metadata map[string]string
}

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

return &CNSLogger{
return &logger{
logger: l,
zapLogger: zapLogger,
metadata: map[string]string{},
}, nil
}

func (c *CNSLogger) InitAI(aiConfig ai.AIConfig, disableTraceLogging, disableMetricLogging, disableEventLogging bool) {
func (c *logger) InitAI(aiConfig ai.AIConfig, disableTraceLogging, disableMetricLogging, disableEventLogging bool) {
c.InitAIWithIKey(aiConfig, aiMetadata, disableTraceLogging, disableMetricLogging, disableEventLogging)
}

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

func (c *CNSLogger) Close() {
func (c *logger) Close() {
c.logger.Close()
if c.th != nil {
c.th.Close(waitTimeInSecs)
}
}

func (c *CNSLogger) SetContextDetails(orchestrator, nodeID string) {
func (c *logger) SetContextDetails(orchestrator, nodeID string) {
c.logger.Logf("SetContext details called with: %v orchestrator nodeID %v", orchestrator, nodeID)
c.m.Lock()
c.metadata[orchestratorTypeKey] = orchestrator
c.metadata[nodeIDKey] = nodeID
c.m.Unlock()
}

func (c *CNSLogger) SetAPIServer(apiserver string) {
func (c *logger) SetAPIServer(apiserver string) {
c.m.Lock()
c.metadata[apiServerKey] = apiserver
c.m.Unlock()
}

func (c *CNSLogger) Printf(format string, args ...any) {
func (c *logger) Printf(format string, args ...any) {
c.logger.Logf(format, args...)
c.zapLogger.Info(fmt.Sprintf(format, args...))
if c.th == nil || c.disableTraceLogging {
Expand All @@ -101,7 +102,7 @@ func (c *CNSLogger) Printf(format string, args ...any) {
c.sendTraceInternal(msg, ai.InfoLevel)
}

func (c *CNSLogger) Debugf(format string, args ...any) {
func (c *logger) Debugf(format string, args ...any) {
c.logger.Debugf(format, args...)
c.zapLogger.Debug(fmt.Sprintf(format, args...))
if c.th == nil || c.disableTraceLogging {
Expand All @@ -111,7 +112,7 @@ func (c *CNSLogger) Debugf(format string, args ...any) {
c.sendTraceInternal(msg, ai.DebugLevel)
}

func (c *CNSLogger) Warnf(format string, args ...any) {
func (c *logger) Warnf(format string, args ...any) {
c.logger.Warnf(format, args...)
c.zapLogger.Warn(fmt.Sprintf(format, args...))
if c.th == nil || c.disableTraceLogging {
Expand All @@ -121,7 +122,7 @@ func (c *CNSLogger) Warnf(format string, args ...any) {
c.sendTraceInternal(msg, ai.WarnLevel)
}

func (c *CNSLogger) Errorf(format string, args ...any) {
func (c *logger) Errorf(format string, args ...any) {
c.logger.Errorf(format, args...)
c.zapLogger.Error(fmt.Sprintf(format, args...))
if c.th == nil || c.disableTraceLogging {
Expand All @@ -131,7 +132,7 @@ func (c *CNSLogger) Errorf(format string, args ...any) {
c.sendTraceInternal(msg, ai.ErrorLevel)
}

func (c *CNSLogger) Request(tag string, request any, err error) {
func (c *logger) Request(tag string, request any, err error) {
c.logger.Request(tag, request, err)
if c.th == nil || c.disableTraceLogging {
return
Expand All @@ -147,7 +148,7 @@ func (c *CNSLogger) Request(tag string, request any, err error) {
c.sendTraceInternal(msg, lvl)
}

func (c *CNSLogger) Response(tag string, response any, returnCode types.ResponseCode, err error) {
func (c *logger) Response(tag string, response any, returnCode types.ResponseCode, err error) {
c.logger.Response(tag, response, int(returnCode), returnCode.String(), err)
if c.th == nil || c.disableTraceLogging {
return
Expand All @@ -166,7 +167,7 @@ func (c *CNSLogger) Response(tag string, response any, returnCode types.Response
c.sendTraceInternal(msg, lvl)
}

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

func (c *CNSLogger) sendTraceInternal(msg string, lvl ai.Level) {
func (c *logger) sendTraceInternal(msg string, lvl ai.Level) {
report := ai.Report{
Message: msg,
Level: lvl,
Expand All @@ -198,7 +199,7 @@ func (c *CNSLogger) sendTraceInternal(msg string, lvl ai.Level) {
c.th.TrackLog(report)
}

func (c *CNSLogger) LogEvent(event ai.Event) {
func (c *logger) LogEvent(event ai.Event) {
if c.th == nil || c.disableEventLogging {
return
}
Expand All @@ -208,7 +209,7 @@ func (c *CNSLogger) LogEvent(event ai.Event) {
c.th.TrackEvent(event)
}

func (c *CNSLogger) SendMetric(metric ai.Metric) {
func (c *logger) SendMetric(metric ai.Metric) {
if c.th == nil || c.disableMetricLogging {
return
}
Expand Down
37 changes: 33 additions & 4 deletions cns/logger/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,66 +6,95 @@ import (
"github.com/Azure/azure-container-networking/cns/types"
)

type loggershim interface {
Close()
InitAI(aitelemetry.AIConfig, bool, bool, bool)
InitAIWithIKey(aitelemetry.AIConfig, string, bool, bool, bool)
SetContextDetails(string, string)
SetAPIServer(string)
Printf(string, ...any)
Debugf(string, ...any)
Warnf(string, ...any)
LogEvent(aitelemetry.Event)
Errorf(string, ...any)
Request(string, any, error)
Response(string, any, types.ResponseCode, error)
ResponseEx(string, any, any, types.ResponseCode, error)
SendMetric(aitelemetry.Metric)
}

var (
Log *CNSLogger
aiMetadata string // this var is set at build time.
Log loggershim
AppInsightsIKey = aiMetadata
aiMetadata string // this var is set at build time.
)

// todo: the functions below should be removed. CNSLogger should be injected where needed and not used from package level scope.

// Deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead.
func Close() {
Log.Close()
}

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

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

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

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

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

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

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

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

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

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

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

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

// Deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead.
func SendMetric(metric aitelemetry.Metric) {
Log.SendMetric(metric)
}
4 changes: 2 additions & 2 deletions cns/logger/v2/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func (c *Config) UnmarshalJSON(data []byte) error {
return nil
}

// Normalize checks the Config for missing or illegal values and sets them
// to defaults if appropriate.
// Normalize checks the Config for missing/default values and sets them
// if appropriate.
func (c *Config) Normalize() {
if c.File != nil {
if c.File.Filepath == "" {
Expand Down
2 changes: 1 addition & 1 deletion cns/logger/v2/cores/ai.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ func ApplicationInsightsCore(cfg *AppInsightsConfig) (zapcore.Core, func(), erro
core := zapai.NewCore(cfg.level, sink)
core = core.WithFieldMappers(zapai.DefaultMappers)
// add normalized fields for the built-in AI Tags
// TODO(rbtr): move to the caller

return core.With(cfg.Fields), aiclose, nil
}
Loading
Loading