44package logger
55
66import (
7- "reflect"
8- "regexp"
97 "time"
108
119 "github.com/Azure/azure-container-networking/aitelemetry"
12- "github.com/Azure/azure-container-networking/log"
13- "github.com/Azure/azure-container-networking/platform"
14- "github.com/Azure/azure-container-networking/telemetry"
15- "github.com/google/uuid"
1610)
1711
18- const (
19- // CNSTelemetryFile - telemetry file path.
20- cnsTelemetryFile = platform .CNSRuntimePath + "AzureCNSTelemetry.json"
21- errorcodePrefix = 5
22- heartbeatIntervalInMinutes = 30
23- retryWaitTimeInSeconds = 60
24- telemetryNumRetries = 5
25- telemetryWaitTimeInMilliseconds = 200
26- )
27-
28- var codeRegex = regexp .MustCompile (`Code:(\w*)` )
29-
3012func SendHeartBeat (heartbeatIntervalInMins int , stopheartbeat chan bool ) {
3113 heartbeat := time .NewTicker (time .Minute * time .Duration (heartbeatIntervalInMins )).C
3214 metric := aitelemetry.Metric {
@@ -44,55 +26,3 @@ func SendHeartBeat(heartbeatIntervalInMins int, stopheartbeat chan bool) {
4426 }
4527 }
4628}
47-
48- // SendCnsTelemetry - handles cns telemetry reports
49- func SendToTelemetryService (reports chan interface {}, telemetryStopProcessing chan bool ) {
50-
51- CONNECT:
52- tb := telemetry .NewTelemetryBuffer ("" )
53- tb .ConnectToTelemetryService (telemetryNumRetries , telemetryWaitTimeInMilliseconds )
54- if tb .Connected {
55-
56- reportMgr := telemetry.ReportManager {
57- ContentType : telemetry .ContentType ,
58- Report : & telemetry.CNSReport {},
59- }
60-
61- reportMgr .GetReportState (cnsTelemetryFile )
62- reportMgr .GetKernelVersion ()
63-
64- for {
65- select {
66- case msg := <- reports :
67- codeStr := codeRegex .FindString (msg .(string ))
68- if len (codeStr ) > errorcodePrefix {
69- reflect .ValueOf (reportMgr .Report ).Elem ().FieldByName ("Errorcode" ).SetString (codeStr [errorcodePrefix :])
70- }
71-
72- reflect .ValueOf (reportMgr .Report ).Elem ().FieldByName ("EventMessage" ).SetString (msg .(string ))
73- case <- telemetryStopProcessing :
74- tb .Close ()
75- return
76- }
77-
78- reflect .ValueOf (reportMgr .Report ).Elem ().FieldByName ("Timestamp" ).SetString (time .Now ().UTC ().String ())
79- if id , err := uuid .NewUUID (); err == nil {
80- reflect .ValueOf (reportMgr .Report ).Elem ().FieldByName ("UUID" ).SetString (id .String ())
81- }
82-
83- if ! reportMgr .GetReportState (cnsTelemetryFile ) {
84- reportMgr .SetReportState (cnsTelemetryFile )
85- }
86-
87- report , err := reportMgr .ReportToBytes ()
88- if err == nil {
89- // If write fails, try to re-establish connections as server/client
90- if _ , err = tb .Write (report ); err != nil {
91- log .Logf ("[CNS-Telemetry] Telemetry write failed: %v" , err )
92- tb .Close ()
93- goto CONNECT
94- }
95- }
96- }
97- }
98- }
0 commit comments