Skip to content

Commit 47f6d8f

Browse files
authored
Adding telemetry to network monitor (#533)
* Adding telemetry to network monitor * Removed newlines per Tamilmanis request * Triggering tests * Triggering tests * Triggering tests * Removed telemetry from unit tests
1 parent a4e9e99 commit 47f6d8f

File tree

4 files changed

+55
-8
lines changed

4 files changed

+55
-8
lines changed

cnms/cnmspackage/api.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package cnms
22

3+
import "github.com/Azure/azure-container-networking/telemetry"
4+
35
type NetworkMonitor struct {
46
AddRulesToBeValidated map[string]int
57
DeleteRulesToBeValidated map[string]int
8+
CNIReport *telemetry.CNIReport
69
}

cnms/cnmspackage/monitor2rules_linux.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package cnms
22

33
import (
4+
"fmt"
5+
46
"github.com/Azure/azure-container-networking/ebtables"
57
"github.com/Azure/azure-container-networking/log"
68
)
@@ -14,11 +16,14 @@ func (networkMonitor *NetworkMonitor) deleteRulesNotExistInMap(chainRules map[st
1416
for rule, chain := range chainRules {
1517
if _, ok := stateRules[rule]; !ok {
1618
if itr, ok := networkMonitor.DeleteRulesToBeValidated[rule]; ok && itr > 0 {
17-
log.Printf("[monitor] Deleting Ebtable rule as it didn't exist in state for %d iterations chain %v rule %v", itr, chain, rule)
19+
buf := fmt.Sprintf("[monitor] Deleting Ebtable rule as it didn't exist in state for %d iterations chain %v rule %v", itr, chain, rule)
1820
if err := ebtables.SetEbRule(table, action, chain, rule); err != nil {
19-
log.Printf("[monitor] Error while deleting ebtable rule %v", err)
21+
buf = fmt.Sprintf("[monitor] Error while deleting ebtable rule %v", err)
2022
}
2123

24+
log.Printf(buf)
25+
networkMonitor.CNIReport.ErrorMessage = buf
26+
networkMonitor.CNIReport.OperationType = "EBTableDelete"
2227
delete(networkMonitor.DeleteRulesToBeValidated, rule)
2328
} else {
2429
log.Printf("[DELETE] Found unmatched rule chain %v rule %v itr %d. Giving one more iteration.", chain, rule, itr)
@@ -39,14 +44,17 @@ func (networkMonitor *NetworkMonitor) addRulesNotExistInMap(
3944
for rule, chain := range stateRules {
4045
if _, ok := chainRules[rule]; !ok {
4146
if itr, ok := networkMonitor.AddRulesToBeValidated[rule]; ok && itr > 0 {
42-
log.Printf("[monitor] Adding Ebtable rule as it existed in state rules but not in current chain rules for %d iterations chain %v rule %v", itr, chain, rule)
47+
buf := fmt.Sprintf("[monitor] Adding Ebtable rule as it existed in state rules but not in current chain rules for %d iterations chain %v rule %v", itr, chain, rule)
4348
if err := ebtables.SetEbRule(table, action, chain, rule); err != nil {
44-
log.Printf("[monitor] Error while adding ebtable rule %v", err)
49+
buf = fmt.Sprintf("[monitor] Error while adding ebtable rule %v", err)
4550
}
4651

52+
log.Printf(buf)
53+
networkMonitor.CNIReport.ErrorMessage = buf
54+
networkMonitor.CNIReport.OperationType = "EBTableAdd"
4755
delete(networkMonitor.AddRulesToBeValidated, rule)
4856
} else {
49-
log.Printf("[ADD] Found unmatched rule chain %v rule %v itr %d. Giving one more iteration", chain, rule, itr)
57+
log.Printf("[ADD] Found unmatched rule chain %v rule %v itr %d. Giving one more iteration.", chain, rule, itr)
5058
networkMonitor.AddRulesToBeValidated[rule] = itr + 1
5159
}
5260
}

cnms/service/networkmonitor.go

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@ import (
1414
"github.com/Azure/azure-container-networking/network"
1515
"github.com/Azure/azure-container-networking/platform"
1616
"github.com/Azure/azure-container-networking/store"
17+
"github.com/Azure/azure-container-networking/telemetry"
1718
)
1819

1920
const (
2021
// Service name.
21-
name = "azure-cnimonitor"
22-
pluginName = "azure-vnet"
23-
DEFAULT_TIMEOUT_IN_SECS = "10"
22+
name = "azure-cnimonitor"
23+
pluginName = "azure-vnet"
24+
DEFAULT_TIMEOUT_IN_SECS = "10"
25+
telemetryNumRetries = 5
26+
telemetryWaitTimeInMilliseconds = 200
2427
)
2528

2629
// Version is populated by make during build.
@@ -113,11 +116,29 @@ func main() {
113116
// Log platform information.
114117
log.Printf("[monitor] Running on %v", platform.GetOSInfo())
115118

119+
reportManager := &telemetry.ReportManager{
120+
ContentType: telemetry.ContentType,
121+
Report: &telemetry.CNIReport{
122+
Context: "AzureCNINetworkMonitor",
123+
Version: version,
124+
SystemDetails: telemetry.SystemInfo{},
125+
InterfaceDetails: telemetry.InterfaceInfo{},
126+
BridgeDetails: telemetry.BridgeInfo{},
127+
},
128+
}
129+
130+
reportManager.Report.(*telemetry.CNIReport).GetOSDetails()
131+
116132
netMonitor := &cnms.NetworkMonitor{
117133
AddRulesToBeValidated: make(map[string]int),
118134
DeleteRulesToBeValidated: make(map[string]int),
135+
CNIReport: reportManager.Report.(*telemetry.CNIReport),
119136
}
120137

138+
tb := telemetry.NewTelemetryBuffer("")
139+
tb.ConnectToTelemetryService(telemetryNumRetries, telemetryWaitTimeInMilliseconds)
140+
defer tb.Close()
141+
121142
for true {
122143
config.Store, err = store.NewJsonFileStore(platform.CNIRuntimePath + pluginName + ".json")
123144
if err != nil {
@@ -141,6 +162,18 @@ func main() {
141162
log.Printf("[monitor] Failed while calling SetupNetworkUsingState with error %v", err)
142163
}
143164

165+
if netMonitor.CNIReport.ErrorMessage != "" {
166+
log.Printf("[monitor] Reporting discrepancy in rules")
167+
netMonitor.CNIReport.Timestamp = time.Now().Format("2006-01-02 15:04:05")
168+
if err := reportManager.SendReport(tb); err != nil {
169+
log.Errorf("[monitor] SendReport failed due to %v", err)
170+
} else {
171+
log.Printf("[monitor] Reported successfully")
172+
}
173+
174+
netMonitor.CNIReport.ErrorMessage = ""
175+
}
176+
144177
log.Printf("[monitor] Going to sleep for %v seconds", timeout)
145178
time.Sleep(time.Duration(timeout) * time.Second)
146179
nm = nil

cnms/service/networkmonitor_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
cnms "github.com/Azure/azure-container-networking/cnms/cnmspackage"
88
"github.com/Azure/azure-container-networking/ebtables"
9+
"github.com/Azure/azure-container-networking/telemetry"
910
)
1011

1112
const (
@@ -38,6 +39,7 @@ func TestAddMissingRule(t *testing.T) {
3839
netMonitor := &cnms.NetworkMonitor{
3940
AddRulesToBeValidated: make(map[string]int),
4041
DeleteRulesToBeValidated: make(map[string]int),
42+
CNIReport: &telemetry.CNIReport{},
4143
}
4244

4345
currentStateRulesMap := addStateRulesToMap()
@@ -78,6 +80,7 @@ func TestDeleteInvalidRule(t *testing.T) {
7880
netMonitor := &cnms.NetworkMonitor{
7981
AddRulesToBeValidated: make(map[string]int),
8082
DeleteRulesToBeValidated: make(map[string]int),
83+
CNIReport: &telemetry.CNIReport{},
8184
}
8285

8386
currentStateRulesMap := addStateRulesToMap()

0 commit comments

Comments
 (0)