-
Notifications
You must be signed in to change notification settings - Fork 236
/
telemetrybuffer.go
416 lines (365 loc) · 10.2 KB
/
telemetrybuffer.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// Copyright 2018 Microsoft. All rights reserved.
// MIT License
package telemetry
import (
"bufio"
"context"
"encoding/json"
"net"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/Azure/azure-container-networking/common"
"github.com/Azure/azure-container-networking/log"
"github.com/Azure/azure-container-networking/platform"
"go.uber.org/zap"
)
// TelemetryConfig - telemetry config read by telemetry service
type TelemetryConfig struct {
ReportToHostIntervalInSeconds time.Duration `json:"reportToHostIntervalInSeconds"`
DisableAll bool
DisableTrace bool
DisableMetric bool
DisableMetadataThread bool
DebugMode bool
DisableTelemetryToNetAgent bool
RefreshTimeoutInSecs int
BatchIntervalInSecs int
BatchSizeInBytes int
GetEnvRetryCount int
GetEnvRetryWaitTimeInSecs int
}
// FdName - file descriptor name
// Delimiter - delimiter for socket reads/writes
// MaxPayloadSize - max buffer size in bytes
const (
FdName = "azure-vnet-telemetry"
Delimiter = '\n'
MaxPayloadSize = 4096
MaxNumReports = 1000
)
// TelemetryBuffer object
type TelemetryBuffer struct {
client net.Conn
listener net.Listener
connections []net.Conn
FdExists bool
Connected bool
data chan interface{}
cancel chan bool
mutex sync.Mutex
logger *zap.Logger
plc platform.ExecClient
}
// Buffer object holds the different types of reports
type Buffer struct {
CNIReports []CNIReport
}
// NewTelemetryBuffer - create a new TelemetryBuffer
func NewTelemetryBuffer(logger *zap.Logger) *TelemetryBuffer {
var tb TelemetryBuffer
tb.data = make(chan interface{}, MaxNumReports)
tb.cancel = make(chan bool, 1)
tb.connections = make([]net.Conn, 0)
tb.logger = logger
tb.plc = platform.NewExecClient(tb.logger)
return &tb
}
func remove(s []net.Conn, i int) []net.Conn {
if len(s) > 0 && i < len(s) {
s[i] = s[len(s)-1]
return s[:len(s)-1]
}
log.Logf("tb connections remove failed index %v len %v", i, len(s))
return s
}
// Starts Telemetry server listening on unix domain socket
func (tb *TelemetryBuffer) StartServer() error {
err := tb.Listen(FdName)
if err != nil {
tb.FdExists = strings.Contains(err.Error(), "in use") || strings.Contains(err.Error(), "Access is denied")
if tb.logger != nil {
tb.logger.Error("Listen returns", zap.Error(err))
} else {
log.Logf("Listen returns: %v", err.Error())
}
return err
}
if tb.logger != nil {
tb.logger.Info("Telemetry service started")
} else {
log.Logf("Telemetry service started")
}
// Spawn server goroutine to handle incoming connections
go func() {
for {
// Spawn worker goroutines to communicate with client
conn, err := tb.listener.Accept()
if err == nil {
tb.mutex.Lock()
tb.connections = append(tb.connections, conn)
tb.mutex.Unlock()
go func() {
for {
reportStr, err := read(conn)
if err == nil {
var tmp map[string]interface{}
err = json.Unmarshal(reportStr, &tmp)
if err != nil {
if tb.logger != nil {
tb.logger.Error("StartServer: unmarshal error", zap.Error(err))
} else {
log.Logf("StartServer: unmarshal error:%v", err)
}
return
}
if _, ok := tmp["CniSucceeded"]; ok {
var cniReport CNIReport
json.Unmarshal([]byte(reportStr), &cniReport)
tb.data <- cniReport
} else if _, ok := tmp["Metric"]; ok {
var aiMetric AIMetric
json.Unmarshal([]byte(reportStr), &aiMetric)
tb.data <- aiMetric
} else {
if tb.logger != nil {
tb.logger.Info("StartServer: default", zap.Any("case", tmp))
} else {
log.Logf("StartServer: default case:%+v...", tmp)
}
}
} else {
var index int
var value net.Conn
var found bool
tb.mutex.Lock()
defer tb.mutex.Unlock()
for index, value = range tb.connections {
if value == conn {
conn.Close()
found = true
break
}
}
if found {
tb.connections = remove(tb.connections, index)
}
return
}
}
}()
} else {
if tb.logger != nil {
tb.logger.Error("Telemetry Server accept error", zap.Error(err))
} else {
log.Logf("Telemetry Server accept error %v", err)
}
return
}
}
}()
return nil
}
func (tb *TelemetryBuffer) Connect() error {
err := tb.Dial(FdName)
if err == nil {
tb.Connected = true
} else if tb.FdExists {
tb.Cleanup(FdName)
}
return err
}
// PushData - PushData running an instance if it isn't already being run elsewhere
func (tb *TelemetryBuffer) PushData(ctx context.Context) {
defer tb.Close()
for {
select {
case report := <-tb.data:
tb.mutex.Lock()
push(report)
tb.mutex.Unlock()
case <-tb.cancel:
if tb.logger != nil {
tb.logger.Info("server cancel event")
} else {
log.Logf("[Telemetry] server cancel event")
}
return
case <-ctx.Done():
if tb.logger != nil {
tb.logger.Info("received context done event")
} else {
log.Logf("[Telemetry] received context done event")
}
return
}
}
}
// read - read from the file descriptor
func read(conn net.Conn) (b []byte, err error) {
b, err = bufio.NewReader(conn).ReadBytes(Delimiter)
if err == nil {
b = b[:len(b)-1]
}
return
}
// Write - write to the file descriptor.
func (tb *TelemetryBuffer) Write(b []byte) (c int, err error) {
buf := make([]byte, len(b))
copy(buf, b)
//nolint:makezero //keeping old code
buf = append(buf, Delimiter)
w := bufio.NewWriter(tb.client)
c, err = w.Write(buf)
if err == nil {
err = w.Flush()
}
return
}
// Cancel - signal to tear down telemetry buffer
func (tb *TelemetryBuffer) Cancel() {
tb.cancel <- true
}
// Close - close all connections
func (tb *TelemetryBuffer) Close() {
if tb.client != nil {
tb.client.Close()
tb.client = nil
}
if tb.listener != nil {
if tb.logger != nil {
tb.logger.Info("server close")
} else {
log.Logf("server close")
}
tb.listener.Close()
}
tb.mutex.Lock()
defer tb.mutex.Unlock()
for _, conn := range tb.connections {
if conn != nil {
conn.Close()
}
}
tb.connections = nil
tb.connections = make([]net.Conn, 0)
}
// push - push the report (x) to corresponding slice
func push(x interface{}) {
switch y := x.(type) {
case CNIReport:
SendAITelemetry(y)
case AIMetric:
SendAIMetric(y)
default:
log.Printf("Push fn: Default case:%+v", y)
}
}
// WaitForTelemetrySocket - Block still pipe/sock created or until max attempts retried
func WaitForTelemetrySocket(maxAttempt int, waitTimeInMillisecs time.Duration) {
for attempt := 0; attempt < maxAttempt; attempt++ {
if SockExists() {
break
}
time.Sleep(waitTimeInMillisecs * time.Millisecond)
}
}
// StartTelemetryService - Kills if any telemetry service runs and start new telemetry service
func (tb *TelemetryBuffer) StartTelemetryService(path string, args []string) error {
err := tb.plc.KillProcessByName(TelemetryServiceProcessName)
if err != nil {
if tb.logger != nil {
tb.logger.Error("Failed to kill process by", zap.String("TelemetryServiceProcessName", TelemetryServiceProcessName), zap.Error(err))
} else {
log.Logf("[Telemetry] Failed to kill process by telemetryServiceProcessName %s due to %v", TelemetryServiceProcessName, err)
}
}
if tb.logger != nil {
tb.logger.Info("Starting telemetry service process", zap.String("path", path), zap.Any("args", args))
} else {
log.Logf("[Telemetry] Starting telemetry service process :%v args:%v", path, args)
}
if err := common.StartProcess(path, args); err != nil {
if tb.logger != nil {
tb.logger.Error("Failed to start telemetry service process", zap.Error(err))
} else {
log.Logf("[Telemetry] Failed to start telemetry service process :%v", err)
}
return err
}
if tb.logger != nil {
tb.logger.Info("Telemetry service started")
} else {
log.Logf("[Telemetry] Telemetry service started")
}
return nil
}
// ReadConfigFile - Read telemetry config file and populate to structure
func ReadConfigFile(filePath string) (TelemetryConfig, error) {
config := TelemetryConfig{}
b, err := os.ReadFile(filePath)
if err != nil {
return config, err
}
if err = json.Unmarshal(b, &config); err != nil {
return config, err // nolint
}
return config, err
}
// ConnectToTelemetryService - Attempt to spawn telemetry process if it's not already running.
func (tb *TelemetryBuffer) ConnectToTelemetryService(telemetryNumRetries, telemetryWaitTimeInMilliseconds int) {
path, dir := getTelemetryServiceDirectory()
args := []string{"-d", dir}
for attempt := 0; attempt < 2; attempt++ {
if err := tb.Connect(); err != nil {
if tb.logger != nil {
tb.logger.Error("Connection to telemetry socket failed", zap.Error(err))
} else {
log.Logf("Connection to telemetry socket failed: %v", err)
}
if _, exists := os.Stat(path); exists != nil {
if tb.logger != nil {
tb.logger.Info("Skip starting telemetry service as file didn't exist")
} else {
log.Logf("Skip starting telemetry service as file didn't exist")
}
return
}
tb.Cleanup(FdName)
tb.StartTelemetryService(path, args) // nolint
WaitForTelemetrySocket(telemetryNumRetries, time.Duration(telemetryWaitTimeInMilliseconds))
} else {
tb.Connected = true
if tb.logger != nil {
tb.logger.Info("Connected to telemetry service")
} else {
log.Logf("Connected to telemetry service")
}
return
}
}
}
// ConnectToTelemetry - attempt to connect to telemetry service
func (tb *TelemetryBuffer) ConnectToTelemetry() {
if err := tb.Connect(); err != nil {
log.Logf("Connection to telemetry socket failed: %v", err)
return
}
tb.Connected = true
log.Logf("Connected to telemetry service")
}
// getTelemetryServiceDirectory - check CNI install directory and Executable location for telemetry binary
func getTelemetryServiceDirectory() (path string, dir string) {
path = filepath.Join(CniInstallDir, TelemetryServiceProcessName)
if _, exists := os.Stat(path); exists != nil {
ex, _ := os.Executable()
exDir := filepath.Dir(ex)
path = filepath.Join(exDir, TelemetryServiceProcessName)
dir = exDir
} else {
dir = CniInstallDir
}
return
}