-
Notifications
You must be signed in to change notification settings - Fork 244
/
Copy pathtelemetry.go
170 lines (148 loc) · 4.02 KB
/
telemetry.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
// Copyright 2018 Microsoft. All rights reserved.
// MIT License
package telemetry
import (
"encoding/json"
"github.com/Azure/azure-container-networking/aitelemetry"
"github.com/Azure/azure-container-networking/common"
"github.com/Azure/azure-container-networking/log"
"github.com/Azure/azure-container-networking/platform"
"github.com/pkg/errors"
"go.uber.org/zap"
)
const (
// CNITelemetryFile Path.
CNITelemetryFile = platform.CNIRuntimePath + "AzureCNITelemetry.json"
// ContentType of JSON
ContentType = "application/json"
)
// OS Details structure.
type OSInfo struct {
OSType string
OSVersion string
KernelVersion string
OSDistribution string
ErrorMessage string
}
// System Details structure.
type SystemInfo struct {
MemVMTotal uint64
MemVMFree uint64
MemUsedByProcess uint64
DiskVMTotal uint64
DiskVMFree uint64
CPUCount int
ErrorMessage string
}
// Interface Details structure.
type InterfaceInfo struct {
InterfaceType string
Subnet string
PrimaryCA string
MAC string
Name string
SecondaryCATotalCount int
SecondaryCAUsedCount int
ErrorMessage string
}
// CNI Bridge Details structure.
type BridgeInfo struct {
NetworkMode string
BridgeName string
ErrorMessage string
}
// Azure CNI Telemetry Report structure.
type CNIReport struct {
IsNewInstance bool
CniSucceeded bool
Name string
Version string
ErrorMessage string
EventMessage string
OperationType string
OperationDuration int
Context string
SubContext string
VMUptime string
Timestamp string
ContainerName string
InfraVnetID string
VnetAddressSpace []string
OSDetails OSInfo
SystemDetails SystemInfo
InterfaceDetails InterfaceInfo
BridgeDetails BridgeInfo
Metadata common.Metadata `json:"compute"`
Logger *zap.Logger
}
type AIMetric struct {
Metric aitelemetry.Metric
}
// ReportManager structure.
type ReportManager struct {
HostNetAgentURL string
ContentType string
Report interface{}
}
// GetReport retrieves orchestrator, system, OS and Interface details and create a report structure.
func (report *CNIReport) GetReport(name string, version string, ipamQueryURL string) {
report.Name = name
report.Version = version
report.GetSystemDetails()
report.GetOSDetails()
}
// SendReport will send telemetry report to HostNetAgent.
func (reportMgr *ReportManager) SendReport(tb *TelemetryBuffer) error {
var err error
var report []byte
if tb != nil && tb.Connected {
report, err = reportMgr.ReportToBytes()
if err == nil {
if _, err = tb.Write(report); err != nil {
if tb.logger != nil {
tb.logger.Error("telemetry write failed", zap.Error(err))
} else {
log.Printf("telemetry write failed:%v", err)
}
}
}
}
return err
}
// ReportToBytes - returns the report bytes
func (reportMgr *ReportManager) ReportToBytes() ([]byte, error) {
switch reportMgr.Report.(type) {
case *CNIReport:
case *AIMetric:
default:
return []byte{}, errors.Errorf("Invalid report type: %T", reportMgr.Report)
}
report, err := json.Marshal(reportMgr.Report)
return report, err
}
// This function for sending CNI metrics to telemetry service
func SendCNIMetric(cniMetric *AIMetric, tb *TelemetryBuffer) error {
var err error
var report []byte
if tb != nil && tb.Connected {
reportMgr := &ReportManager{Report: cniMetric}
report, err = reportMgr.ReportToBytes()
if err == nil {
if _, err = tb.Write(report); err != nil {
tb.logger.Error("Error writing to telemetry socket", zap.Error(err))
}
}
}
return err
}
func SendCNIEvent(tb *TelemetryBuffer, report *CNIReport) {
if tb != nil && tb.Connected {
reportMgr := &ReportManager{Report: report}
reportBytes, err := reportMgr.ReportToBytes()
if err == nil {
if _, err = tb.Write(reportBytes); err != nil {
tb.logger.Error("Error writing to telemetry socket", zap.Error(err))
}
}
}
}