Skip to content

Commit

Permalink
feat: add levels to CNS AI logs
Browse files Browse the repository at this point in the history
Signed-off-by: Evan Baker <rbtr@users.noreply.github.com>
  • Loading branch information
rbtr authored Jan 31, 2025
1 parent c670d53 commit 29a2b3b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
2 changes: 2 additions & 0 deletions aitelemetry/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (

"github.com/Azure/azure-container-networking/common"
"github.com/microsoft/ApplicationInsights-Go/appinsights"
"github.com/microsoft/ApplicationInsights-Go/appinsights/contracts"
)

// Application trace/log structure
type Report struct {
Message string
Level contracts.SeverityLevel
Context string
AppVersion string
CustomDimensions map[string]string
Expand Down
14 changes: 13 additions & 1 deletion aitelemetry/telemetrywrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/Azure/azure-container-networking/processlock"
"github.com/Azure/azure-container-networking/store"
"github.com/microsoft/ApplicationInsights-Go/appinsights"
"github.com/microsoft/ApplicationInsights-Go/appinsights/contracts"
)

const (
Expand All @@ -35,6 +36,17 @@ const (
defaultRefreshTimeoutInSecs = 10
)

type Level = contracts.SeverityLevel

const (
DebugLevel Level = contracts.Verbose
InfoLevel Level = contracts.Information
WarnLevel Level = contracts.Warning
ErrorLevel Level = contracts.Error
PanicLevel Level = contracts.Critical
FatalLevel Level = contracts.Critical
)

var debugMode bool

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

// will be empty if cns used as telemetry service for cni
if th.appVersion == "" {
Expand Down
39 changes: 23 additions & 16 deletions cns/logger/cnslogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"sync"

"github.com/Azure/azure-container-networking/aitelemetry"
ai "github.com/Azure/azure-container-networking/aitelemetry"
"github.com/Azure/azure-container-networking/cns/types"
"github.com/Azure/azure-container-networking/log"
"github.com/pkg/errors"
Expand All @@ -15,7 +15,7 @@ import (

type CNSLogger struct {
logger *log.Logger
th aitelemetry.TelemetryHandle
th ai.TelemetryHandle
DisableTraceLogging bool
DisableMetricLogging bool
DisableEventLogging bool
Expand Down Expand Up @@ -49,12 +49,12 @@ func NewCNSLogger(fileName string, logLevel, logTarget int, logDir string) (*CNS
}, nil
}

func (c *CNSLogger) InitAI(aiConfig aitelemetry.AIConfig, disableTraceLogging, disableMetricLogging, disableEventLogging bool) {
func (c *CNSLogger) InitAI(aiConfig ai.AIConfig, disableTraceLogging, disableMetricLogging, disableEventLogging bool) {
c.InitAIWithIKey(aiConfig, aiMetadata, disableTraceLogging, disableMetricLogging, disableEventLogging)
}

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

msg := fmt.Sprintf(format, args...)
c.sendTraceInternal(msg)
c.sendTraceInternal(msg, ai.InfoLevel)
}

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

msg := fmt.Sprintf(format, args...)
c.sendTraceInternal(msg)
c.sendTraceInternal(msg, ai.DebugLevel)
}

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

msg := fmt.Sprintf(format, args...)
c.sendTraceInternal(msg)
c.sendTraceInternal(msg, ai.WarnLevel)
}

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

msg := fmt.Sprintf(format, args...)
c.sendTraceInternal(msg)
c.sendTraceInternal(msg, ai.ErrorLevel)
}

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

var msg string
lvl := ai.InfoLevel
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())
lvl = ai.ErrorLevel
}

c.sendTraceInternal(msg)
c.sendTraceInternal(msg, lvl)
}

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

var msg string
lvl := ai.InfoLevel
switch {
case err == nil && returnCode == 0:
msg = fmt.Sprintf("[%s] Sent %T %+v.", tag, response, response)
case err != nil:
msg = fmt.Sprintf("[%s] Code:%s, %+v %s.", tag, returnCode.String(), response, err.Error())
lvl = ai.ErrorLevel
default:
msg = fmt.Sprintf("[%s] Code:%s, %+v.", tag, returnCode.String(), response)
}

c.sendTraceInternal(msg)
c.sendTraceInternal(msg, lvl)
}

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

var msg string
lvl := ai.InfoLevel
switch {
case err == nil && returnCode == 0:
msg = fmt.Sprintf("[%s] Sent %T %+v %T %+v.", tag, request, request, response, response)
case err != nil:
msg = fmt.Sprintf("[%s] Code:%s, %+v, %+v, %s.", tag, returnCode.String(), request, response, err.Error())
lvl = ai.ErrorLevel
default:
msg = fmt.Sprintf("[%s] Code:%s, %+v, %+v.", tag, returnCode.String(), request, response)
}

c.sendTraceInternal(msg)
c.sendTraceInternal(msg, lvl)
}

func (c *CNSLogger) getOrchestratorAndNodeID() (orch, nodeID string) {
Expand All @@ -197,11 +203,12 @@ func (c *CNSLogger) getOrchestratorAndNodeID() (orch, nodeID string) {
return
}

func (c *CNSLogger) sendTraceInternal(msg string) {
func (c *CNSLogger) sendTraceInternal(msg string, lvl ai.Level) {
orch, nodeID := c.getOrchestratorAndNodeID()

report := aitelemetry.Report{
report := ai.Report{
Message: msg,
Level: lvl,
Context: nodeID,
CustomDimensions: map[string]string{
OrchestratorTypeStr: orch,
Expand All @@ -212,7 +219,7 @@ func (c *CNSLogger) sendTraceInternal(msg string) {
c.th.TrackLog(report)
}

func (c *CNSLogger) LogEvent(event aitelemetry.Event) {
func (c *CNSLogger) LogEvent(event ai.Event) {
if c.th == nil || c.DisableEventLogging {
return
}
Expand All @@ -223,7 +230,7 @@ func (c *CNSLogger) LogEvent(event aitelemetry.Event) {
c.th.TrackEvent(event)
}

func (c *CNSLogger) SendMetric(metric aitelemetry.Metric) {
func (c *CNSLogger) SendMetric(metric ai.Metric) {
if c.th == nil || c.DisableMetricLogging {
return
}
Expand Down

0 comments on commit 29a2b3b

Please sign in to comment.