-
Notifications
You must be signed in to change notification settings - Fork 236
/
ai-utils.go
93 lines (80 loc) · 2.27 KB
/
ai-utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package metrics
import (
"fmt"
"strconv"
"time"
"github.com/Azure/azure-container-networking/aitelemetry"
"github.com/Azure/azure-container-networking/log"
"github.com/Azure/azure-container-networking/npm/util"
)
var (
th aitelemetry.TelemetryHandle
)
// CreateTelemetryHandle creates a handler to initialize AI telemetry
func CreateTelemetryHandle(version, aiMetadata string) error {
aiConfig := aitelemetry.AIConfig{
AppName: util.AzureNpmFlag,
AppVersion: version,
BatchSize: util.BatchSizeInBytes,
BatchInterval: util.BatchIntervalInSecs,
RefreshTimeout: util.RefreshTimeoutInSecs,
DebugMode: util.DebugMode,
GetEnvRetryCount: util.GetEnvRetryCount,
GetEnvRetryWaitTimeInSecs: util.GetEnvRetryWaitTimeInSecs,
}
var err error
for i := 0; i < util.AiInitializeRetryCount; i++ {
th, err = aitelemetry.NewAITelemetry("", aiMetadata, aiConfig)
if err != nil {
log.Logf("Failed to init AppInsights with err: %+v for %d time", err, i+1)
time.Sleep(time.Minute * time.Duration(util.AiInitializeRetryInMin))
} else {
break
}
}
if err != nil {
return err
}
if th != nil {
log.Logf("Initialized AppInsights handle")
}
return nil
}
// SendErrorLogAndMetric is responsible for sending log and error metrics through AI telemetry
func SendErrorLogAndMetric(operationID int, format string, args ...interface{}) {
// Send error metrics
customDimensions := map[string]string{
util.ErrorCode: strconv.Itoa(operationID),
}
metric := aitelemetry.Metric{
Name: util.ErrorMetric,
Value: util.ErrorValue,
CustomDimensions: customDimensions,
}
SendMetric(metric)
// Send error logs
msg := fmt.Sprintf(format, args...)
report := aitelemetry.Report{
Message: msg,
Context: strconv.Itoa(operationID),
CustomDimensions: make(map[string]string),
}
log.Errorf(msg)
SendLog(report)
}
// SendMetric sends metrics
func SendMetric(metric aitelemetry.Metric) {
if th == nil {
log.Logf("AppInsights didn't initialized.")
return
}
th.TrackMetric(metric)
}
// SendLog sends log
func SendLog(report aitelemetry.Report) {
if th == nil {
log.Logf("AppInsights didn't initialized.")
return
}
th.TrackLog(report)
}