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 Jan 31, 2025
1 parent c670d53 commit 6605979
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 66 deletions.
103 changes: 41 additions & 62 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 @@ -16,15 +17,17 @@ import (
type CNSLogger struct {
logger *log.Logger
th aitelemetry.TelemetryHandle
DisableTraceLogging bool
DisableMetricLogging bool
DisableEventLogging bool
disableTraceLogging bool
disableMetricLogging bool
disableEventLogging bool

zapLogger *zap.Logger

m sync.RWMutex
Orchestrator string
NodeID string
m sync.RWMutex
metadata map[string]string
// orchestrator string
// nodeID string
// apiserver string
}

func NewCNSLogger(fileName string, logLevel, logTarget int, logDir string) (*CNSLogger, error) {
Expand Down Expand Up @@ -59,12 +62,11 @@ func (c *CNSLogger) InitAIWithIKey(aiConfig aitelemetry.AIConfig, instrumentatio
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.
Expand All @@ -80,83 +82,76 @@ 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)
}

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)
}

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)
}

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)
}

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
if err == nil {
msg = fmt.Sprintf("[%s] Received %T %+v.", tag, request, request)
} else {
msg = fmt.Sprintf("[%s] Failed to decode %T %+v %s.", tag, request, request, err.Error())
}

c.sendTraceInternal(msg)
}

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
switch {
case err == nil && returnCode == 0:
Expand All @@ -166,17 +161,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)
}

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
switch {
case err == nil && returnCode == 0:
Expand All @@ -186,50 +178,37 @@ 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)
}

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) {
orch, nodeID := c.getOrchestratorAndNodeID()

report := aitelemetry.Report{
Message: msg,
Context: nodeID,
CustomDimensions: map[string]string{
OrchestratorTypeStr: orch,
NodeIDStr: nodeID,
},
Message: msg,
Context: c.metadata[nodeIDKey],
CustomDimensions: map[string]string{},
}

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

func (c *CNSLogger) LogEvent(event aitelemetry.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 aitelemetry.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: 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 6605979

Please sign in to comment.