Skip to content

Commit 6eafd37

Browse files
authored
feat: add apiserver FQDN to CNS log metadata in AKS
Signed-off-by: Evan Baker <[email protected]>
1 parent c670d53 commit 6eafd37

File tree

5 files changed

+49
-71
lines changed

5 files changed

+49
-71
lines changed

cns/logger/cnslogger.go

Lines changed: 43 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package logger
22

33
import (
44
"fmt"
5+
"maps"
56
"os"
67
"sync"
78

@@ -13,21 +14,23 @@ import (
1314
"go.uber.org/zap/zapcore"
1415
)
1516

17+
// wait time for closing AI telemetry session.
18+
const waitTimeInSecs = 10
19+
1620
type CNSLogger struct {
1721
logger *log.Logger
1822
th aitelemetry.TelemetryHandle
19-
DisableTraceLogging bool
20-
DisableMetricLogging bool
21-
DisableEventLogging bool
23+
disableTraceLogging bool
24+
disableMetricLogging bool
25+
disableEventLogging bool
2226

2327
zapLogger *zap.Logger
2428

25-
m sync.RWMutex
26-
Orchestrator string
27-
NodeID string
29+
m sync.RWMutex
30+
metadata map[string]string
2831
}
2932

30-
func NewCNSLogger(fileName string, logLevel, logTarget int, logDir string) (*CNSLogger, error) {
33+
func New(fileName string, logLevel, logTarget int, logDir string) (*CNSLogger, error) {
3134
l, err := log.NewLoggerE(fileName, logLevel, logTarget, logDir)
3235
if err != nil {
3336
return nil, errors.Wrap(err, "could not get new logger")
@@ -46,6 +49,7 @@ func NewCNSLogger(fileName string, logLevel, logTarget int, logDir string) (*CNS
4649
return &CNSLogger{
4750
logger: l,
4851
zapLogger: zapLogger,
52+
metadata: map[string]string{},
4953
}, nil
5054
}
5155

@@ -59,17 +63,13 @@ func (c *CNSLogger) InitAIWithIKey(aiConfig aitelemetry.AIConfig, instrumentatio
5963
c.logger.Errorf("Error initializing AI Telemetry:%v", err)
6064
return
6165
}
62-
6366
c.th = th
6467
c.logger.Printf("AI Telemetry Handle created")
65-
c.DisableMetricLogging = disableMetricLogging
66-
c.DisableTraceLogging = disableTraceLogging
67-
c.DisableEventLogging = disableEventLogging
68+
c.disableMetricLogging = disableMetricLogging
69+
c.disableTraceLogging = disableTraceLogging
70+
c.disableEventLogging = disableEventLogging
6871
}
6972

70-
// wait time for closing AI telemetry session.
71-
const waitTimeInSecs = 10
72-
7373
func (c *CNSLogger) Close() {
7474
c.logger.Close()
7575
if c.th != nil {
@@ -80,83 +80,76 @@ func (c *CNSLogger) Close() {
8080
func (c *CNSLogger) SetContextDetails(orchestrator, nodeID string) {
8181
c.logger.Logf("SetContext details called with: %v orchestrator nodeID %v", orchestrator, nodeID)
8282
c.m.Lock()
83-
c.Orchestrator = orchestrator
84-
c.NodeID = nodeID
83+
c.metadata[orchestratorTypeKey] = orchestrator
84+
c.metadata[nodeIDKey] = nodeID
85+
c.m.Unlock()
86+
}
87+
88+
func (c *CNSLogger) SetAPIServer(apiserver string) {
89+
c.m.Lock()
90+
c.metadata[apiServerKey] = apiserver
8591
c.m.Unlock()
8692
}
8793

8894
func (c *CNSLogger) Printf(format string, args ...any) {
8995
c.logger.Logf(format, args...)
9096
c.zapLogger.Info(fmt.Sprintf(format, args...))
91-
92-
if c.th == nil || c.DisableTraceLogging {
97+
if c.th == nil || c.disableTraceLogging {
9398
return
9499
}
95-
96100
msg := fmt.Sprintf(format, args...)
97101
c.sendTraceInternal(msg)
98102
}
99103

100104
func (c *CNSLogger) Debugf(format string, args ...any) {
101105
c.logger.Debugf(format, args...)
102106
c.zapLogger.Debug(fmt.Sprintf(format, args...))
103-
104-
if c.th == nil || c.DisableTraceLogging {
107+
if c.th == nil || c.disableTraceLogging {
105108
return
106109
}
107-
108110
msg := fmt.Sprintf(format, args...)
109111
c.sendTraceInternal(msg)
110112
}
111113

112114
func (c *CNSLogger) Warnf(format string, args ...any) {
113115
c.logger.Warnf(format, args...)
114116
c.zapLogger.Warn(fmt.Sprintf(format, args...))
115-
116-
if c.th == nil || c.DisableTraceLogging {
117+
if c.th == nil || c.disableTraceLogging {
117118
return
118119
}
119-
120120
msg := fmt.Sprintf(format, args...)
121121
c.sendTraceInternal(msg)
122122
}
123123

124124
func (c *CNSLogger) Errorf(format string, args ...any) {
125125
c.logger.Errorf(format, args...)
126126
c.zapLogger.Error(fmt.Sprintf(format, args...))
127-
128-
if c.th == nil || c.DisableTraceLogging {
127+
if c.th == nil || c.disableTraceLogging {
129128
return
130129
}
131-
132130
msg := fmt.Sprintf(format, args...)
133131
c.sendTraceInternal(msg)
134132
}
135133

136134
func (c *CNSLogger) Request(tag string, request any, err error) {
137135
c.logger.Request(tag, request, err)
138-
139-
if c.th == nil || c.DisableTraceLogging {
136+
if c.th == nil || c.disableTraceLogging {
140137
return
141138
}
142-
143139
var msg string
144140
if err == nil {
145141
msg = fmt.Sprintf("[%s] Received %T %+v.", tag, request, request)
146142
} else {
147143
msg = fmt.Sprintf("[%s] Failed to decode %T %+v %s.", tag, request, request, err.Error())
148144
}
149-
150145
c.sendTraceInternal(msg)
151146
}
152147

153148
func (c *CNSLogger) Response(tag string, response any, returnCode types.ResponseCode, err error) {
154149
c.logger.Response(tag, response, int(returnCode), returnCode.String(), err)
155-
156-
if c.th == nil || c.DisableTraceLogging {
150+
if c.th == nil || c.disableTraceLogging {
157151
return
158152
}
159-
160153
var msg string
161154
switch {
162155
case err == nil && returnCode == 0:
@@ -166,17 +159,14 @@ func (c *CNSLogger) Response(tag string, response any, returnCode types.Response
166159
default:
167160
msg = fmt.Sprintf("[%s] Code:%s, %+v.", tag, returnCode.String(), response)
168161
}
169-
170162
c.sendTraceInternal(msg)
171163
}
172164

173165
func (c *CNSLogger) ResponseEx(tag string, request, response any, returnCode types.ResponseCode, err error) {
174166
c.logger.ResponseEx(tag, request, response, int(returnCode), returnCode.String(), err)
175-
176-
if c.th == nil || c.DisableTraceLogging {
167+
if c.th == nil || c.disableTraceLogging {
177168
return
178169
}
179-
180170
var msg string
181171
switch {
182172
case err == nil && returnCode == 0:
@@ -186,50 +176,37 @@ func (c *CNSLogger) ResponseEx(tag string, request, response any, returnCode typ
186176
default:
187177
msg = fmt.Sprintf("[%s] Code:%s, %+v, %+v.", tag, returnCode.String(), request, response)
188178
}
189-
190179
c.sendTraceInternal(msg)
191180
}
192181

193-
func (c *CNSLogger) getOrchestratorAndNodeID() (orch, nodeID string) {
194-
c.m.RLock()
195-
orch, nodeID = c.Orchestrator, c.NodeID
196-
c.m.RUnlock()
197-
return
198-
}
199-
200182
func (c *CNSLogger) sendTraceInternal(msg string) {
201-
orch, nodeID := c.getOrchestratorAndNodeID()
202-
203183
report := aitelemetry.Report{
204-
Message: msg,
205-
Context: nodeID,
206-
CustomDimensions: map[string]string{
207-
OrchestratorTypeStr: orch,
208-
NodeIDStr: nodeID,
209-
},
184+
Message: msg,
185+
Context: c.metadata[nodeIDKey],
186+
CustomDimensions: map[string]string{},
210187
}
211-
188+
c.m.RLock()
189+
maps.Copy(report.CustomDimensions, c.metadata)
190+
c.m.RUnlock()
212191
c.th.TrackLog(report)
213192
}
214193

215194
func (c *CNSLogger) LogEvent(event aitelemetry.Event) {
216-
if c.th == nil || c.DisableEventLogging {
195+
if c.th == nil || c.disableEventLogging {
217196
return
218197
}
219-
220-
orch, nodeID := c.getOrchestratorAndNodeID()
221-
event.Properties[OrchestratorTypeStr] = orch
222-
event.Properties[NodeIDStr] = nodeID
198+
c.m.RLock()
199+
maps.Copy(event.Properties, c.metadata)
200+
c.m.RUnlock()
223201
c.th.TrackEvent(event)
224202
}
225203

226204
func (c *CNSLogger) SendMetric(metric aitelemetry.Metric) {
227-
if c.th == nil || c.DisableMetricLogging {
205+
if c.th == nil || c.disableMetricLogging {
228206
return
229207
}
230-
231-
orch, nodeID := c.getOrchestratorAndNodeID()
232-
metric.CustomDimensions[OrchestratorTypeStr] = orch
233-
metric.CustomDimensions[NodeIDStr] = nodeID
208+
c.m.RLock()
209+
maps.Copy(metric.CustomDimensions, c.metadata)
210+
c.m.RUnlock()
234211
c.th.TrackMetric(metric)
235212
}

cns/logger/constants.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ const (
77
ConfigSnapshotMetricsStr = "ConfigSnapshot"
88

99
// Dimensions
10-
OrchestratorTypeStr = "OrchestratorType"
11-
NodeIDStr = "NodeID"
10+
orchestratorTypeKey = "OrchestratorType"
11+
nodeIDKey = "NodeID"
1212
HomeAZStr = "HomeAZ"
1313
IsAZRSupportedStr = "IsAZRSupported"
1414
HomeAZErrorCodeStr = "HomeAZErrorCode"
1515
HomeAZErrorMsgStr = "HomeAZErrorMsg"
1616
CNSConfigPropertyStr = "CNSConfiguration"
1717
CNSConfigMD5CheckSumPropertyStr = "CNSConfigurationMD5Checksum"
18+
apiServerKey = "APIServer"
1819

1920
// CNS NC Snspshot properties
2021
CnsNCSnapshotEventStr = "CNSNCSnapshot"

cns/logger/log.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func Close() {
1818
}
1919

2020
func InitLogger(fileName string, logLevel, logTarget int, logDir string) {
21-
Log, _ = NewCNSLogger(fileName, logLevel, logTarget, logDir)
21+
Log, _ = New(fileName, logLevel, logTarget, logDir)
2222
}
2323

2424
func InitAI(aiConfig aitelemetry.AIConfig, disableTraceLogging, disableMetricLogging, disableEventLogging bool) {

cns/metric/heartbeat.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ func SendHeartBeat(ctx context.Context, heartbeatInterval time.Duration, homeAzM
3030
Value: 1.0,
3131
CustomDimensions: make(map[string]string),
3232
}
33-
3433
// add azr metrics when channel mode is direct
3534
if channelMode == cns.Direct {
3635
getHomeAzResp := homeAzMonitor.GetHomeAz(ctx)
@@ -41,7 +40,6 @@ func SendHeartBeat(ctx context.Context, heartbeatInterval time.Duration, homeAzM
4140
default:
4241
metric.CustomDimensions[logger.HomeAZErrorCodeStr] = getHomeAzResp.Response.ReturnCode.String()
4342
metric.CustomDimensions[logger.HomeAZErrorMsgStr] = getHomeAzResp.Response.Message
44-
4543
}
4644
}
4745
logger.SendMetric(metric)

cns/service/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,8 @@ func main() {
828828
// Initialze state in if CNS is running in CRD mode
829829
// State must be initialized before we start HTTPRestService
830830
if config.ChannelMode == cns.CRD {
831+
// Add APIServer FQDN to Log metadata
832+
logger.Log.SetAPIServer(os.Getenv("KUBERNETES_SERVICE_HOST"))
831833

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

0 commit comments

Comments
 (0)