-
Notifications
You must be signed in to change notification settings - Fork 242
/
Copy pathai-utils.go
123 lines (107 loc) · 3.1 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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"
"k8s.io/klog"
)
const telemetryCloseWaitTimeSeconds = 10
var (
th aitelemetry.TelemetryHandle
npmVersion int
PrintLog = true
DonotPrint = false
)
// CreateTelemetryHandle creates a handler to initialize AI telemetry
func CreateTelemetryHandle(npmVersionNum int, imageVersion, aiMetadata string) error {
npmVersion = npmVersionNum
aiConfig := aitelemetry.AIConfig{
AppName: util.AzureNpmFlag,
AppVersion: imageVersion,
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
}
// Close cleans up the telemetry handle, which effectively waits for all telemetry data to be sent
func Close() {
if th == nil {
return
}
th.Close(telemetryCloseWaitTimeSeconds)
}
// SendErrorLogAndMetric sends a metric through AI telemetry and sends a log to the Kusto Messages table
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 prints out a log visible in kubectl logs
SendMetric(metric)
// Send error logs
msg := fmt.Sprintf(format, args...)
log.Errorf(msg)
SendLog(operationID, msg, DonotPrint)
}
// SendMetric sends metrics
func SendMetric(metric aitelemetry.Metric) {
if th == nil {
return
}
th.TrackMetric(metric)
}
// SendLog sends log
func SendLog(operationID int, msg string, printLog bool) {
msg = fmt.Sprintf("%s - (NPM v%d)", msg, npmVersion)
report := aitelemetry.Report{
Message: msg,
Context: strconv.Itoa(operationID),
CustomDimensions: make(map[string]string),
}
if printLog {
klog.Infof(msg)
}
if th == nil {
return
}
th.TrackLog(report)
}
func SendHeartbeatWithNumPolicies() {
var message string
numPolicies, err := GetNumPolicies()
if err == nil {
message = fmt.Sprintf("info: NPM heartbeat. Current num policies: %d", numPolicies)
} else {
message = fmt.Sprintf("warn: NPM hearbeat. Couldn't get number of policies for telemetry log: %v", err)
klog.Warning(message)
}
SendLog(util.NpmID, message, DonotPrint)
}