Skip to content

Commit b37bffa

Browse files
rbtrsivakami
authored andcommitted
feat: add levels to CNS AI logs (#3381)
Signed-off-by: Evan Baker <[email protected]>
1 parent da73b62 commit b37bffa

File tree

3 files changed

+38
-17
lines changed

3 files changed

+38
-17
lines changed

aitelemetry/api.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import (
55

66
"github.com/Azure/azure-container-networking/common"
77
"github.com/microsoft/ApplicationInsights-Go/appinsights"
8+
"github.com/microsoft/ApplicationInsights-Go/appinsights/contracts"
89
)
910

1011
// Application trace/log structure
1112
type Report struct {
1213
Message string
14+
Level contracts.SeverityLevel
1315
Context string
1416
AppVersion string
1517
CustomDimensions map[string]string

aitelemetry/telemetrywrapper.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/Azure/azure-container-networking/processlock"
1212
"github.com/Azure/azure-container-networking/store"
1313
"github.com/microsoft/ApplicationInsights-Go/appinsights"
14+
"github.com/microsoft/ApplicationInsights-Go/appinsights/contracts"
1415
)
1516

1617
const (
@@ -35,6 +36,17 @@ const (
3536
defaultRefreshTimeoutInSecs = 10
3637
)
3738

39+
type Level = contracts.SeverityLevel
40+
41+
const (
42+
DebugLevel Level = contracts.Verbose
43+
InfoLevel Level = contracts.Information
44+
WarnLevel Level = contracts.Warning
45+
ErrorLevel Level = contracts.Error
46+
PanicLevel Level = contracts.Critical
47+
FatalLevel Level = contracts.Critical
48+
)
49+
3850
var debugMode bool
3951

4052
func setAIConfigDefaults(config *AIConfig) {
@@ -203,7 +215,7 @@ func NewAITelemetry(
203215
// and for rest it uses custom dimesion
204216
func (th *telemetryHandle) TrackLog(report Report) {
205217
// Initialize new trace message
206-
trace := appinsights.NewTraceTelemetry(report.Message, appinsights.Warning)
218+
trace := appinsights.NewTraceTelemetry(report.Message, report.Level)
207219

208220
// will be empty if cns used as telemetry service for cni
209221
if th.appVersion == "" {

cns/logger/cnslogger.go

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"os"
66
"sync"
77

8-
"github.com/Azure/azure-container-networking/aitelemetry"
8+
ai "github.com/Azure/azure-container-networking/aitelemetry"
99
"github.com/Azure/azure-container-networking/cns/types"
1010
"github.com/Azure/azure-container-networking/log"
1111
"github.com/pkg/errors"
@@ -15,7 +15,7 @@ import (
1515

1616
type CNSLogger struct {
1717
logger *log.Logger
18-
th aitelemetry.TelemetryHandle
18+
th ai.TelemetryHandle
1919
DisableTraceLogging bool
2020
DisableMetricLogging bool
2121
DisableEventLogging bool
@@ -49,12 +49,12 @@ func NewCNSLogger(fileName string, logLevel, logTarget int, logDir string) (*CNS
4949
}, nil
5050
}
5151

52-
func (c *CNSLogger) InitAI(aiConfig aitelemetry.AIConfig, disableTraceLogging, disableMetricLogging, disableEventLogging bool) {
52+
func (c *CNSLogger) InitAI(aiConfig ai.AIConfig, disableTraceLogging, disableMetricLogging, disableEventLogging bool) {
5353
c.InitAIWithIKey(aiConfig, aiMetadata, disableTraceLogging, disableMetricLogging, disableEventLogging)
5454
}
5555

56-
func (c *CNSLogger) InitAIWithIKey(aiConfig aitelemetry.AIConfig, instrumentationKey string, disableTraceLogging, disableMetricLogging, disableEventLogging bool) {
57-
th, err := aitelemetry.NewAITelemetry("", instrumentationKey, aiConfig)
56+
func (c *CNSLogger) InitAIWithIKey(aiConfig ai.AIConfig, instrumentationKey string, disableTraceLogging, disableMetricLogging, disableEventLogging bool) {
57+
th, err := ai.NewAITelemetry("", instrumentationKey, aiConfig)
5858
if err != nil {
5959
c.logger.Errorf("Error initializing AI Telemetry:%v", err)
6060
return
@@ -94,7 +94,7 @@ func (c *CNSLogger) Printf(format string, args ...any) {
9494
}
9595

9696
msg := fmt.Sprintf(format, args...)
97-
c.sendTraceInternal(msg)
97+
c.sendTraceInternal(msg, ai.InfoLevel)
9898
}
9999

100100
func (c *CNSLogger) Debugf(format string, args ...any) {
@@ -106,7 +106,7 @@ func (c *CNSLogger) Debugf(format string, args ...any) {
106106
}
107107

108108
msg := fmt.Sprintf(format, args...)
109-
c.sendTraceInternal(msg)
109+
c.sendTraceInternal(msg, ai.DebugLevel)
110110
}
111111

112112
func (c *CNSLogger) Warnf(format string, args ...any) {
@@ -118,7 +118,7 @@ func (c *CNSLogger) Warnf(format string, args ...any) {
118118
}
119119

120120
msg := fmt.Sprintf(format, args...)
121-
c.sendTraceInternal(msg)
121+
c.sendTraceInternal(msg, ai.WarnLevel)
122122
}
123123

124124
func (c *CNSLogger) Errorf(format string, args ...any) {
@@ -130,7 +130,7 @@ func (c *CNSLogger) Errorf(format string, args ...any) {
130130
}
131131

132132
msg := fmt.Sprintf(format, args...)
133-
c.sendTraceInternal(msg)
133+
c.sendTraceInternal(msg, ai.ErrorLevel)
134134
}
135135

136136
func (c *CNSLogger) Request(tag string, request any, err error) {
@@ -141,13 +141,15 @@ func (c *CNSLogger) Request(tag string, request any, err error) {
141141
}
142142

143143
var msg string
144+
lvl := ai.InfoLevel
144145
if err == nil {
145146
msg = fmt.Sprintf("[%s] Received %T %+v.", tag, request, request)
146147
} else {
147148
msg = fmt.Sprintf("[%s] Failed to decode %T %+v %s.", tag, request, request, err.Error())
149+
lvl = ai.ErrorLevel
148150
}
149151

150-
c.sendTraceInternal(msg)
152+
c.sendTraceInternal(msg, lvl)
151153
}
152154

153155
func (c *CNSLogger) Response(tag string, response any, returnCode types.ResponseCode, err error) {
@@ -158,16 +160,18 @@ func (c *CNSLogger) Response(tag string, response any, returnCode types.Response
158160
}
159161

160162
var msg string
163+
lvl := ai.InfoLevel
161164
switch {
162165
case err == nil && returnCode == 0:
163166
msg = fmt.Sprintf("[%s] Sent %T %+v.", tag, response, response)
164167
case err != nil:
165168
msg = fmt.Sprintf("[%s] Code:%s, %+v %s.", tag, returnCode.String(), response, err.Error())
169+
lvl = ai.ErrorLevel
166170
default:
167171
msg = fmt.Sprintf("[%s] Code:%s, %+v.", tag, returnCode.String(), response)
168172
}
169173

170-
c.sendTraceInternal(msg)
174+
c.sendTraceInternal(msg, lvl)
171175
}
172176

173177
func (c *CNSLogger) ResponseEx(tag string, request, response any, returnCode types.ResponseCode, err error) {
@@ -178,16 +182,18 @@ func (c *CNSLogger) ResponseEx(tag string, request, response any, returnCode typ
178182
}
179183

180184
var msg string
185+
lvl := ai.InfoLevel
181186
switch {
182187
case err == nil && returnCode == 0:
183188
msg = fmt.Sprintf("[%s] Sent %T %+v %T %+v.", tag, request, request, response, response)
184189
case err != nil:
185190
msg = fmt.Sprintf("[%s] Code:%s, %+v, %+v, %s.", tag, returnCode.String(), request, response, err.Error())
191+
lvl = ai.ErrorLevel
186192
default:
187193
msg = fmt.Sprintf("[%s] Code:%s, %+v, %+v.", tag, returnCode.String(), request, response)
188194
}
189195

190-
c.sendTraceInternal(msg)
196+
c.sendTraceInternal(msg, lvl)
191197
}
192198

193199
func (c *CNSLogger) getOrchestratorAndNodeID() (orch, nodeID string) {
@@ -197,11 +203,12 @@ func (c *CNSLogger) getOrchestratorAndNodeID() (orch, nodeID string) {
197203
return
198204
}
199205

200-
func (c *CNSLogger) sendTraceInternal(msg string) {
206+
func (c *CNSLogger) sendTraceInternal(msg string, lvl ai.Level) {
201207
orch, nodeID := c.getOrchestratorAndNodeID()
202208

203-
report := aitelemetry.Report{
209+
report := ai.Report{
204210
Message: msg,
211+
Level: lvl,
205212
Context: nodeID,
206213
CustomDimensions: map[string]string{
207214
OrchestratorTypeStr: orch,
@@ -212,7 +219,7 @@ func (c *CNSLogger) sendTraceInternal(msg string) {
212219
c.th.TrackLog(report)
213220
}
214221

215-
func (c *CNSLogger) LogEvent(event aitelemetry.Event) {
222+
func (c *CNSLogger) LogEvent(event ai.Event) {
216223
if c.th == nil || c.DisableEventLogging {
217224
return
218225
}
@@ -223,7 +230,7 @@ func (c *CNSLogger) LogEvent(event aitelemetry.Event) {
223230
c.th.TrackEvent(event)
224231
}
225232

226-
func (c *CNSLogger) SendMetric(metric aitelemetry.Metric) {
233+
func (c *CNSLogger) SendMetric(metric ai.Metric) {
227234
if c.th == nil || c.DisableMetricLogging {
228235
return
229236
}

0 commit comments

Comments
 (0)