@@ -2,6 +2,7 @@ package logger
22
33import (
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+
1620type 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-
7373func (c * CNSLogger ) Close () {
7474 c .logger .Close ()
7575 if c .th != nil {
@@ -80,83 +80,76 @@ func (c *CNSLogger) Close() {
8080func (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
8894func (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
100104func (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
112114func (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
124124func (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
136134func (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
153148func (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
173165func (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-
200182func (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
215194func (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
226204func (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}
0 commit comments