Skip to content

Commit

Permalink
feat: add apiserver FQDN to CNS log metadata in AKS
Browse files Browse the repository at this point in the history
Signed-off-by: Evan Baker <rbtr@users.noreply.github.com>
  • Loading branch information
rbtr authored Feb 4, 2025
1 parent 40b6c44 commit aa4b97e
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 75 deletions.
117 changes: 47 additions & 70 deletions cns/logger/cnslogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package logger

import (
"fmt"
"maps"
"os"
"sync"

Expand All @@ -13,21 +14,23 @@ import (
"go.uber.org/zap/zapcore"
)

type CNSLogger struct {
logger *log.Logger
th ai.TelemetryHandle
DisableTraceLogging bool
DisableMetricLogging bool
DisableEventLogging bool
// wait time for closing AI telemetry session.
const waitTimeInSecs = 10

type CNSLogger struct {
logger *log.Logger
zapLogger *zap.Logger
th ai.TelemetryHandle

disableTraceLogging bool
disableMetricLogging bool
disableEventLogging bool

m sync.RWMutex
Orchestrator string
NodeID string
m sync.RWMutex
metadata map[string]string
}

func NewCNSLogger(fileName string, logLevel, logTarget int, logDir string) (*CNSLogger, error) {
func New(fileName string, logLevel, logTarget int, logDir string) (*CNSLogger, 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,6 +49,7 @@ func NewCNSLogger(fileName string, logLevel, logTarget int, logDir string) (*CNS
return &CNSLogger{
logger: l,
zapLogger: zapLogger,
metadata: map[string]string{},
}, nil
}

Expand All @@ -59,17 +63,13 @@ func (c *CNSLogger) InitAIWithIKey(aiConfig ai.AIConfig, instrumentationKey stri
c.logger.Errorf("Error initializing AI Telemetry:%v", err)
return
}

c.th = th
c.logger.Printf("AI Telemetry Handle created")
c.DisableMetricLogging = disableMetricLogging
c.DisableTraceLogging = disableTraceLogging
c.DisableEventLogging = disableEventLogging
c.disableMetricLogging = disableMetricLogging
c.disableTraceLogging = disableTraceLogging
c.disableEventLogging = disableEventLogging
}

// wait time for closing AI telemetry session.
const waitTimeInSecs = 10

func (c *CNSLogger) Close() {
c.logger.Close()
if c.th != nil {
Expand All @@ -80,66 +80,62 @@ func (c *CNSLogger) Close() {
func (c *CNSLogger) SetContextDetails(orchestrator, nodeID string) {
c.logger.Logf("SetContext details called with: %v orchestrator nodeID %v", orchestrator, nodeID)
c.m.Lock()
c.Orchestrator = orchestrator
c.NodeID = nodeID
c.metadata[orchestratorTypeKey] = orchestrator
c.metadata[nodeIDKey] = nodeID
c.m.Unlock()
}

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

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 {
if c.th == nil || c.disableTraceLogging {
return
}

msg := fmt.Sprintf(format, args...)
c.sendTraceInternal(msg, ai.InfoLevel)
}

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 {
if c.th == nil || c.disableTraceLogging {
return
}

msg := fmt.Sprintf(format, args...)
c.sendTraceInternal(msg, ai.DebugLevel)
}

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 {
if c.th == nil || c.disableTraceLogging {
return
}

msg := fmt.Sprintf(format, args...)
c.sendTraceInternal(msg, ai.WarnLevel)
}

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 {
if c.th == nil || c.disableTraceLogging {
return
}

msg := fmt.Sprintf(format, args...)
c.sendTraceInternal(msg, ai.ErrorLevel)
}

func (c *CNSLogger) Request(tag string, request any, err error) {
c.logger.Request(tag, request, err)

if c.th == nil || c.DisableTraceLogging {
if c.th == nil || c.disableTraceLogging {
return
}

var msg string
lvl := ai.InfoLevel
if err == nil {
Expand All @@ -148,17 +144,14 @@ func (c *CNSLogger) Request(tag string, request any, err error) {
msg = fmt.Sprintf("[%s] Failed to decode %T %+v %s.", tag, request, request, err.Error())
lvl = ai.ErrorLevel
}

c.sendTraceInternal(msg, lvl)
}

func (c *CNSLogger) 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 {
if c.th == nil || c.disableTraceLogging {
return
}

var msg string
lvl := ai.InfoLevel
switch {
Expand All @@ -170,17 +163,14 @@ func (c *CNSLogger) Response(tag string, response any, returnCode types.Response
default:
msg = fmt.Sprintf("[%s] Code:%s, %+v.", tag, returnCode.String(), response)
}

c.sendTraceInternal(msg, lvl)
}

func (c *CNSLogger) 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 {
if c.th == nil || c.disableTraceLogging {
return
}

var msg string
lvl := ai.InfoLevel
switch {
Expand All @@ -192,51 +182,38 @@ func (c *CNSLogger) ResponseEx(tag string, request, response any, returnCode typ
default:
msg = fmt.Sprintf("[%s] Code:%s, %+v, %+v.", tag, returnCode.String(), request, response)
}

c.sendTraceInternal(msg, lvl)
}

func (c *CNSLogger) getOrchestratorAndNodeID() (orch, nodeID string) {
c.m.RLock()
orch, nodeID = c.Orchestrator, c.NodeID
c.m.RUnlock()
return
}

func (c *CNSLogger) sendTraceInternal(msg string, lvl ai.Level) {
orch, nodeID := c.getOrchestratorAndNodeID()

report := ai.Report{
Message: msg,
Level: lvl,
Context: nodeID,
CustomDimensions: map[string]string{
OrchestratorTypeStr: orch,
NodeIDStr: nodeID,
},
Message: msg,
Level: lvl,
Context: c.metadata[nodeIDKey],
CustomDimensions: map[string]string{"Level": lvl.String()},
}

c.m.RLock()
maps.Copy(report.CustomDimensions, c.metadata)
c.m.RUnlock()
c.th.TrackLog(report)
}

func (c *CNSLogger) LogEvent(event ai.Event) {
if c.th == nil || c.DisableEventLogging {
if c.th == nil || c.disableEventLogging {
return
}

orch, nodeID := c.getOrchestratorAndNodeID()
event.Properties[OrchestratorTypeStr] = orch
event.Properties[NodeIDStr] = nodeID
c.m.RLock()
maps.Copy(event.Properties, c.metadata)
c.m.RUnlock()
c.th.TrackEvent(event)
}

func (c *CNSLogger) SendMetric(metric ai.Metric) {
if c.th == nil || c.DisableMetricLogging {
if c.th == nil || c.disableMetricLogging {
return
}

orch, nodeID := c.getOrchestratorAndNodeID()
metric.CustomDimensions[OrchestratorTypeStr] = orch
metric.CustomDimensions[NodeIDStr] = nodeID
c.m.RLock()
maps.Copy(metric.CustomDimensions, c.metadata)
c.m.RUnlock()
c.th.TrackMetric(metric)
}
5 changes: 3 additions & 2 deletions cns/logger/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ const (
ConfigSnapshotMetricsStr = "ConfigSnapshot"

// Dimensions
OrchestratorTypeStr = "OrchestratorType"
NodeIDStr = "NodeID"
orchestratorTypeKey = "OrchestratorType"
nodeIDKey = "NodeID"
HomeAZStr = "HomeAZ"
IsAZRSupportedStr = "IsAZRSupported"
HomeAZErrorCodeStr = "HomeAZErrorCode"
HomeAZErrorMsgStr = "HomeAZErrorMsg"
CNSConfigPropertyStr = "CNSConfiguration"
CNSConfigMD5CheckSumPropertyStr = "CNSConfigurationMD5Checksum"
apiServerKey = "APIServer"

// CNS NC Snspshot properties
CnsNCSnapshotEventStr = "CNSNCSnapshot"
Expand Down
2 changes: 1 addition & 1 deletion cns/logger/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func Close() {
}

func InitLogger(fileName string, logLevel, logTarget int, logDir string) {
Log, _ = NewCNSLogger(fileName, logLevel, logTarget, logDir)
Log, _ = New(fileName, logLevel, logTarget, logDir)
}

func InitAI(aiConfig aitelemetry.AIConfig, disableTraceLogging, disableMetricLogging, disableEventLogging bool) {
Expand Down
2 changes: 0 additions & 2 deletions cns/metric/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ func SendHeartBeat(ctx context.Context, heartbeatInterval time.Duration, homeAzM
Value: 1.0,
CustomDimensions: make(map[string]string),
}

// add azr metrics when channel mode is direct
if channelMode == cns.Direct {
getHomeAzResp := homeAzMonitor.GetHomeAz(ctx)
Expand All @@ -41,7 +40,6 @@ func SendHeartBeat(ctx context.Context, heartbeatInterval time.Duration, homeAzM
default:
metric.CustomDimensions[logger.HomeAZErrorCodeStr] = getHomeAzResp.Response.ReturnCode.String()
metric.CustomDimensions[logger.HomeAZErrorMsgStr] = getHomeAzResp.Response.Message

}
}
logger.SendMetric(metric)
Expand Down
2 changes: 2 additions & 0 deletions cns/service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,8 @@ func main() {
// Initialze state in if CNS is running in CRD mode
// State must be initialized before we start HTTPRestService
if config.ChannelMode == cns.CRD {
// Add APIServer FQDN to Log metadata
logger.Log.SetAPIServer(os.Getenv("KUBERNETES_SERVICE_HOST"))

// Check the CNI statefile mount, and if the file is empty
// stub an empty JSON object
Expand Down

0 comments on commit aa4b97e

Please sign in to comment.