diff --git a/cns/logger/cnslogger.go b/cns/logger/cnslogger.go index 51c93c0e0e..679b819699 100644 --- a/cns/logger/cnslogger.go +++ b/cns/logger/cnslogger.go @@ -2,6 +2,7 @@ package logger import ( "fmt" + "maps" "os" "sync" @@ -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) { @@ -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. @@ -80,19 +82,23 @@ 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) } @@ -100,11 +106,9 @@ func (c *CNSLogger) Printf(format string, args ...any) { 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) } @@ -112,11 +116,9 @@ func (c *CNSLogger) Debugf(format string, args ...any) { 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) } @@ -124,39 +126,32 @@ func (c *CNSLogger) Warnf(format string, args ...any) { 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: @@ -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: @@ -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) } diff --git a/cns/logger/constants.go b/cns/logger/constants.go index ce4c0c80c0..db9f03b304 100644 --- a/cns/logger/constants.go +++ b/cns/logger/constants.go @@ -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" diff --git a/cns/metric/heartbeat.go b/cns/metric/heartbeat.go index de65e4e5ea..8a20943a17 100644 --- a/cns/metric/heartbeat.go +++ b/cns/metric/heartbeat.go @@ -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) @@ -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) diff --git a/cns/service/main.go b/cns/service/main.go index f8235bba9e..7a6a64dccb 100644 --- a/cns/service/main.go +++ b/cns/service/main.go @@ -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