Skip to content

Commit aa4b97e

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

File tree

5 files changed

+53
-75
lines changed

5 files changed

+53
-75
lines changed

cns/logger/cnslogger.go

Lines changed: 47 additions & 70 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

16-
type CNSLogger struct {
17-
logger *log.Logger
18-
th ai.TelemetryHandle
19-
DisableTraceLogging bool
20-
DisableMetricLogging bool
21-
DisableEventLogging bool
17+
// wait time for closing AI telemetry session.
18+
const waitTimeInSecs = 10
2219

20+
type CNSLogger struct {
21+
logger *log.Logger
2322
zapLogger *zap.Logger
23+
th ai.TelemetryHandle
24+
25+
disableTraceLogging bool
26+
disableMetricLogging bool
27+
disableEventLogging bool
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 ai.AIConfig, instrumentationKey stri
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,66 +80,62 @@ 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, ai.InfoLevel)
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, ai.DebugLevel)
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, ai.WarnLevel)
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, ai.ErrorLevel)
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
lvl := ai.InfoLevel
145141
if err == nil {
@@ -148,17 +144,14 @@ func (c *CNSLogger) Request(tag string, request any, err error) {
148144
msg = fmt.Sprintf("[%s] Failed to decode %T %+v %s.", tag, request, request, err.Error())
149145
lvl = ai.ErrorLevel
150146
}
151-
152147
c.sendTraceInternal(msg, lvl)
153148
}
154149

155150
func (c *CNSLogger) Response(tag string, response any, returnCode types.ResponseCode, err error) {
156151
c.logger.Response(tag, response, int(returnCode), returnCode.String(), err)
157-
158-
if c.th == nil || c.DisableTraceLogging {
152+
if c.th == nil || c.disableTraceLogging {
159153
return
160154
}
161-
162155
var msg string
163156
lvl := ai.InfoLevel
164157
switch {
@@ -170,17 +163,14 @@ func (c *CNSLogger) Response(tag string, response any, returnCode types.Response
170163
default:
171164
msg = fmt.Sprintf("[%s] Code:%s, %+v.", tag, returnCode.String(), response)
172165
}
173-
174166
c.sendTraceInternal(msg, lvl)
175167
}
176168

177169
func (c *CNSLogger) ResponseEx(tag string, request, response any, returnCode types.ResponseCode, err error) {
178170
c.logger.ResponseEx(tag, request, response, int(returnCode), returnCode.String(), err)
179-
180-
if c.th == nil || c.DisableTraceLogging {
171+
if c.th == nil || c.disableTraceLogging {
181172
return
182173
}
183-
184174
var msg string
185175
lvl := ai.InfoLevel
186176
switch {
@@ -192,51 +182,38 @@ func (c *CNSLogger) ResponseEx(tag string, request, response any, returnCode typ
192182
default:
193183
msg = fmt.Sprintf("[%s] Code:%s, %+v, %+v.", tag, returnCode.String(), request, response)
194184
}
195-
196185
c.sendTraceInternal(msg, lvl)
197186
}
198187

199-
func (c *CNSLogger) getOrchestratorAndNodeID() (orch, nodeID string) {
200-
c.m.RLock()
201-
orch, nodeID = c.Orchestrator, c.NodeID
202-
c.m.RUnlock()
203-
return
204-
}
205-
206188
func (c *CNSLogger) sendTraceInternal(msg string, lvl ai.Level) {
207-
orch, nodeID := c.getOrchestratorAndNodeID()
208-
209189
report := ai.Report{
210-
Message: msg,
211-
Level: lvl,
212-
Context: nodeID,
213-
CustomDimensions: map[string]string{
214-
OrchestratorTypeStr: orch,
215-
NodeIDStr: nodeID,
216-
},
190+
Message: msg,
191+
Level: lvl,
192+
Context: c.metadata[nodeIDKey],
193+
CustomDimensions: map[string]string{"Level": lvl.String()},
217194
}
218-
195+
c.m.RLock()
196+
maps.Copy(report.CustomDimensions, c.metadata)
197+
c.m.RUnlock()
219198
c.th.TrackLog(report)
220199
}
221200

222201
func (c *CNSLogger) LogEvent(event ai.Event) {
223-
if c.th == nil || c.DisableEventLogging {
202+
if c.th == nil || c.disableEventLogging {
224203
return
225204
}
226-
227-
orch, nodeID := c.getOrchestratorAndNodeID()
228-
event.Properties[OrchestratorTypeStr] = orch
229-
event.Properties[NodeIDStr] = nodeID
205+
c.m.RLock()
206+
maps.Copy(event.Properties, c.metadata)
207+
c.m.RUnlock()
230208
c.th.TrackEvent(event)
231209
}
232210

233211
func (c *CNSLogger) SendMetric(metric ai.Metric) {
234-
if c.th == nil || c.DisableMetricLogging {
212+
if c.th == nil || c.disableMetricLogging {
235213
return
236214
}
237-
238-
orch, nodeID := c.getOrchestratorAndNodeID()
239-
metric.CustomDimensions[OrchestratorTypeStr] = orch
240-
metric.CustomDimensions[NodeIDStr] = nodeID
215+
c.m.RLock()
216+
maps.Copy(metric.CustomDimensions, c.metadata)
217+
c.m.RUnlock()
241218
c.th.TrackMetric(metric)
242219
}

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)