Skip to content

Commit 8046c63

Browse files
committed
move telemetry to dedicated package and create package variable
1 parent b7190c8 commit 8046c63

File tree

13 files changed

+128
-213
lines changed

13 files changed

+128
-213
lines changed

cni/network/common.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,14 @@ import (
44
"encoding/json"
55
"io"
66
"os"
7-
"reflect"
87

98
"github.com/Azure/azure-container-networking/cni"
10-
"github.com/Azure/azure-container-networking/telemetry"
119
"github.com/containernetworking/cni/pkg/skel"
1210
cniTypes "github.com/containernetworking/cni/pkg/types"
1311
"github.com/pkg/errors"
1412
"go.uber.org/zap"
1513
)
1614

17-
// send error report to hostnetagent if CNI encounters any error.
18-
func ReportPluginError(reportManager *telemetry.ReportManager, tb *telemetry.TelemetryBuffer, err error) {
19-
logger.Error("Report plugin error")
20-
reflect.ValueOf(reportManager.Report).Elem().FieldByName("ErrorMessage").SetString(err.Error())
21-
22-
if err := reportManager.SendReport(tb); err != nil {
23-
logger.Error("SendReport failed", zap.Error(err))
24-
}
25-
}
26-
2715
func validateConfig(jsonBytes []byte) error {
2816
var conf struct {
2917
Name string `json:"name"`

cni/network/network.go

Lines changed: 22 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ import (
88
"encoding/json"
99
"fmt"
1010
"net"
11-
"os"
1211
"regexp"
1312
"strconv"
1413
"time"
1514

16-
"github.com/Azure/azure-container-networking/aitelemetry"
1715
"github.com/Azure/azure-container-networking/cni"
1816
"github.com/Azure/azure-container-networking/cni/api"
1917
"github.com/Azure/azure-container-networking/cni/log"
18+
"github.com/Azure/azure-container-networking/cni/telemetry/client"
2019
"github.com/Azure/azure-container-networking/cni/util"
2120
"github.com/Azure/azure-container-networking/cns"
2221
cnscli "github.com/Azure/azure-container-networking/cns/client"
@@ -31,7 +30,6 @@ import (
3130
"github.com/Azure/azure-container-networking/platform"
3231
nnscontracts "github.com/Azure/azure-container-networking/proto/nodenetworkservice/3.302.0.744"
3332
"github.com/Azure/azure-container-networking/store"
34-
"github.com/Azure/azure-container-networking/telemetry"
3533
cniSkel "github.com/containernetworking/cni/pkg/skel"
3634
cniTypes "github.com/containernetworking/cni/pkg/types"
3735
cniTypesCurr "github.com/containernetworking/cni/pkg/types/100"
@@ -40,7 +38,10 @@ import (
4038
)
4139

4240
// matches if the string fully consists of zero or more alphanumeric, dots, dashes, parentheses, spaces, or underscores
43-
var allowedInput = regexp.MustCompile(`^[a-zA-Z0-9._\-\(\) ]*$`)
41+
var (
42+
allowedInput = regexp.MustCompile(`^[a-zA-Z0-9._\-\(\) ]*$`)
43+
telemetry = telemetryclient.Telemetry
44+
)
4445

4546
const (
4647
dockerNetworkOption = "com.docker.network.generic"
@@ -80,8 +81,6 @@ type NetPlugin struct {
8081
*cni.Plugin
8182
nm network.NetworkManager
8283
ipamInvoker IPAMInvoker
83-
report *telemetry.CNIReport
84-
tb *telemetry.TelemetryBuffer
8584
nnsClient NnsClient
8685
multitenancyClient MultitenancyClient
8786
netClient InterfaceGetter
@@ -148,11 +147,6 @@ func NewPlugin(name string,
148147
}, nil
149148
}
150149

151-
func (plugin *NetPlugin) SetCNIReport(report *telemetry.CNIReport, tb *telemetry.TelemetryBuffer) {
152-
plugin.report = report
153-
plugin.tb = tb
154-
}
155-
156150
// Starts the plugin.
157151
func (plugin *NetPlugin) Start(config *common.PluginConfig) error {
158152
// Initialize base plugin.
@@ -179,13 +173,6 @@ func (plugin *NetPlugin) Start(config *common.PluginConfig) error {
179173
return nil
180174
}
181175

182-
func sendEvent(plugin *NetPlugin, msg string) {
183-
eventMsg := fmt.Sprintf("[%d] %s", os.Getpid(), msg)
184-
plugin.report.Version = plugin.Version
185-
plugin.report.EventMessage = eventMsg
186-
telemetry.SendCNIEvent(plugin.tb, plugin.report)
187-
}
188-
189176
func (plugin *NetPlugin) GetAllEndpointState(networkid string) (*api.AzureCNIState, error) {
190177
st := api.AzureCNIState{
191178
ContainerInterfaces: make(map[string]api.PodNetworkInterfaceInfo),
@@ -307,35 +294,13 @@ func (plugin *NetPlugin) getPodInfo(args string) (name, ns string, err error) {
307294
return k8sPodName, k8sNamespace, nil
308295
}
309296

310-
func SetCustomDimensions(cniMetric *telemetry.AIMetric, nwCfg *cni.NetworkConfig, err error) {
311-
if cniMetric == nil {
312-
logger.Error("Unable to set custom dimension. Report is nil")
313-
return
314-
}
315-
316-
if err != nil {
317-
cniMetric.Metric.CustomDimensions[telemetry.StatusStr] = telemetry.FailedStr
318-
} else {
319-
cniMetric.Metric.CustomDimensions[telemetry.StatusStr] = telemetry.SucceededStr
320-
}
321-
322-
if nwCfg != nil {
323-
if nwCfg.MultiTenancy {
324-
cniMetric.Metric.CustomDimensions[telemetry.CNIModeStr] = telemetry.MultiTenancyStr
325-
} else {
326-
cniMetric.Metric.CustomDimensions[telemetry.CNIModeStr] = telemetry.SingleTenancyStr
327-
}
328-
329-
cniMetric.Metric.CustomDimensions[telemetry.CNINetworkModeStr] = nwCfg.Mode
330-
}
331-
}
332-
333297
func (plugin *NetPlugin) setCNIReportDetails(nwCfg *cni.NetworkConfig, opType, msg string) {
334-
plugin.report.OperationType = opType
335-
plugin.report.SubContext = fmt.Sprintf("%+v", nwCfg)
336-
plugin.report.EventMessage = msg
337-
plugin.report.BridgeDetails.NetworkMode = nwCfg.Mode
338-
plugin.report.InterfaceDetails.SecondaryCAUsedCount = plugin.nm.GetNumberOfEndpoints("", nwCfg.Name)
298+
telemetry.CNIReportSettings.OperationType = opType
299+
telemetry.CNIReportSettings.SubContext = fmt.Sprintf("%+v", nwCfg)
300+
telemetry.CNIReportSettings.EventMessage = msg
301+
telemetry.CNIReportSettings.BridgeDetails.NetworkMode = nwCfg.Mode
302+
telemetry.CNIReportSettings.InterfaceDetails.SecondaryCAUsedCount = plugin.nm.GetNumberOfEndpoints("", nwCfg.Name)
303+
telemetry.CNIReportSettings.Version = plugin.Version
339304
}
340305

341306
func addNatIPV6SubnetInfo(nwCfg *cni.NetworkConfig,
@@ -361,7 +326,7 @@ func (plugin *NetPlugin) addIpamInvoker(ipamAddConfig IPAMAddConfig) (IPAMAddRes
361326
if err != nil {
362327
return IPAMAddResult{}, errors.Wrap(err, "failed to add ipam invoker")
363328
}
364-
sendEvent(plugin, fmt.Sprintf("Allocated IPAddress from ipam interface: %+v", ipamAddResult.PrettyString()))
329+
telemetry.SendEvent(fmt.Sprintf("Allocated IPAddress from ipam interface: %+v", ipamAddResult.PrettyString()))
365330
return ipamAddResult, nil
366331
}
367332

@@ -393,20 +358,17 @@ func (plugin *NetPlugin) Add(args *cniSkel.CmdArgs) error {
393358
enableInfraVnet bool
394359
enableSnatForDNS bool
395360
k8sPodName string
396-
cniMetric telemetry.AIMetric
397361
epInfos []*network.EndpointInfo
398362
)
399363

400-
startTime := time.Now()
401-
402364
logger.Info("Processing ADD command",
403365
zap.String("containerId", args.ContainerID),
404366
zap.String("netNS", args.Netns),
405367
zap.String("ifName", args.IfName),
406368
zap.Any("args", args.Args),
407369
zap.String("path", args.Path),
408370
zap.ByteString("stdinData", args.StdinData))
409-
sendEvent(plugin, fmt.Sprintf("[cni-net] Processing ADD command with args {ContainerID:%v Netns:%v IfName:%v Args:%v Path:%v StdinData:%s}.",
371+
telemetry.SendEvent(fmt.Sprintf("[cni-net] Processing ADD command with args {ContainerID:%v Netns:%v IfName:%v Args:%v Path:%v StdinData:%s}.",
410372
args.ContainerID, args.Netns, args.IfName, args.Args, args.Path, args.StdinData))
411373

412374
// Parse network configuration from stdin.
@@ -425,16 +387,6 @@ func (plugin *NetPlugin) Add(args *cniSkel.CmdArgs) error {
425387
plugin.setCNIReportDetails(nwCfg, CNI_ADD, "")
426388

427389
defer func() {
428-
operationTimeMs := time.Since(startTime).Milliseconds()
429-
cniMetric.Metric = aitelemetry.Metric{
430-
Name: telemetry.CNIAddTimeMetricStr,
431-
Value: float64(operationTimeMs),
432-
AppVersion: plugin.Version,
433-
CustomDimensions: make(map[string]string),
434-
}
435-
SetCustomDimensions(&cniMetric, nwCfg, err)
436-
telemetry.SendCNIMetric(&cniMetric, plugin.tb)
437-
438390
// Add Interfaces to result.
439391
// previously we had a default interface info to select which interface info was the one to be returned from cni add
440392
cniResult := &cniTypesCurr.Result{}
@@ -489,7 +441,7 @@ func (plugin *NetPlugin) Add(args *cniSkel.CmdArgs) error {
489441
return err
490442
}
491443

492-
plugin.report.ContainerName = k8sPodName + ":" + k8sNamespace
444+
telemetry.CNIReportSettings.ContainerName = k8sPodName + ":" + k8sNamespace
493445

494446
k8sContainerID := args.ContainerID
495447
if len(k8sContainerID) == 0 {
@@ -542,7 +494,7 @@ func (plugin *NetPlugin) Add(args *cniSkel.CmdArgs) error {
542494
// triggered only in swift v1 multitenancy
543495
// dual nic multitenancy -> two interface infos
544496
// multitenancy (swift v1) -> one interface info
545-
plugin.report.Context = "AzureCNIMultitenancy"
497+
telemetry.CNIReportSettings.Context = "AzureCNIMultitenancy"
546498
plugin.multitenancyClient.Init(cnsClient, AzureNetIOShim{})
547499

548500
// Temporary if block to determining whether we disable SNAT on host (for multi-tenant scenario only)
@@ -680,7 +632,7 @@ func (plugin *NetPlugin) Add(args *cniSkel.CmdArgs) error {
680632
return errors.Wrap(err, "failed to create endpoint") // behavior can change if you don't assign to err prior to returning
681633
}
682634
// telemetry added
683-
sendEvent(plugin, fmt.Sprintf("CNI ADD Process succeeded for interfaces: %v", ipamAddResult.PrettyString()))
635+
telemetry.SendEvent(fmt.Sprintf("CNI ADD Process succeeded for interfaces: %v", ipamAddResult.PrettyString()))
684636
return nil
685637
}
686638

@@ -1011,19 +963,16 @@ func (plugin *NetPlugin) Delete(args *cniSkel.CmdArgs) error {
1011963
k8sNamespace string
1012964
networkID string
1013965
nwInfo network.EndpointInfo
1014-
cniMetric telemetry.AIMetric
1015966
)
1016967

1017-
startTime := time.Now()
1018-
1019968
logger.Info("Processing DEL command",
1020969
zap.String("containerId", args.ContainerID),
1021970
zap.String("netNS", args.Netns),
1022971
zap.String("ifName", args.IfName),
1023972
zap.Any("args", args.Args),
1024973
zap.String("path", args.Path),
1025974
zap.ByteString("stdinData", args.StdinData))
1026-
sendEvent(plugin, fmt.Sprintf("[cni-net] Processing DEL command with args {ContainerID:%v Netns:%v IfName:%v Args:%v Path:%v, StdinData:%s}.",
975+
telemetry.SendEvent(fmt.Sprintf("[cni-net] Processing DEL command with args {ContainerID:%v Netns:%v IfName:%v Args:%v Path:%v, StdinData:%s}.",
1027976
args.ContainerID, args.Netns, args.IfName, args.Args, args.Path, args.StdinData))
1028977

1029978
defer func() {
@@ -1049,28 +998,14 @@ func (plugin *NetPlugin) Delete(args *cniSkel.CmdArgs) error {
1049998
}
1050999

10511000
plugin.setCNIReportDetails(nwCfg, CNI_DEL, "")
1052-
plugin.report.ContainerName = k8sPodName + ":" + k8sNamespace
1001+
telemetry.CNIReportSettings.ContainerName = k8sPodName + ":" + k8sNamespace
10531002

10541003
iptables.DisableIPTableLock = nwCfg.DisableIPTableLock
10551004

1056-
sendMetricFunc := func() {
1057-
operationTimeMs := time.Since(startTime).Milliseconds()
1058-
cniMetric.Metric = aitelemetry.Metric{
1059-
Name: telemetry.CNIDelTimeMetricStr,
1060-
Value: float64(operationTimeMs),
1061-
AppVersion: plugin.Version,
1062-
CustomDimensions: make(map[string]string),
1063-
}
1064-
SetCustomDimensions(&cniMetric, nwCfg, err)
1065-
telemetry.SendCNIMetric(&cniMetric, plugin.tb)
1066-
}
1067-
10681005
platformInit(nwCfg)
10691006

10701007
logger.Info("Execution mode", zap.String("mode", nwCfg.ExecutionMode))
10711008
if nwCfg.ExecutionMode == string(util.Baremetal) {
1072-
// schedule send metric before attempting delete
1073-
defer sendMetricFunc()
10741009
_, err = plugin.nnsClient.DeleteContainerNetworking(context.Background(), k8sPodName, args.Netns)
10751010
if err != nil {
10761011
return fmt.Errorf("nnsClient.DeleteContainerNetworking failed with err %w", err)
@@ -1166,7 +1101,7 @@ func (plugin *NetPlugin) Delete(args *cniSkel.CmdArgs) error {
11661101

11671102
logger.Error("Release ip by ContainerID (endpoint not found)",
11681103
zap.String("containerID", args.ContainerID))
1169-
sendEvent(plugin, fmt.Sprintf("Release ip by ContainerID (endpoint not found):%v", args.ContainerID))
1104+
telemetry.SendEvent(fmt.Sprintf("Release ip by ContainerID (endpoint not found):%v", args.ContainerID))
11701105
if err = plugin.ipamInvoker.Delete(nil, nwCfg, args, nwInfo.Options); err != nil {
11711106
return plugin.RetriableError(fmt.Errorf("failed to release address(no endpoint): %w", err))
11721107
}
@@ -1191,18 +1126,16 @@ func (plugin *NetPlugin) Delete(args *cniSkel.CmdArgs) error {
11911126
logger.Info("Deleting the endpoints from the ipam")
11921127
// delete endpoint state in cns and in statefile
11931128
for _, epInfo := range epInfos {
1194-
// schedule send metric before attempting delete
1195-
defer sendMetricFunc() //nolint:gocritic
11961129
logger.Info("Deleting endpoint",
11971130
zap.String("endpointID", epInfo.EndpointID))
1198-
sendEvent(plugin, fmt.Sprintf("Deleting endpoint:%v", epInfo.EndpointID))
1131+
telemetry.SendEvent(fmt.Sprintf("Deleting endpoint:%v", epInfo.EndpointID))
11991132

12001133
if !nwCfg.MultiTenancy && (epInfo.NICType == cns.InfraNIC || epInfo.NICType == "") {
12011134
// Delegated/secondary nic ips are statically allocated so we don't need to release
12021135
// Call into IPAM plugin to release the endpoint's addresses.
12031136
for i := range epInfo.IPAddresses {
12041137
logger.Info("Release ip", zap.String("ip", epInfo.IPAddresses[i].IP.String()))
1205-
sendEvent(plugin, fmt.Sprintf("Release ip:%s", epInfo.IPAddresses[i].IP.String()))
1138+
telemetry.SendEvent(fmt.Sprintf("Release ip:%s", epInfo.IPAddresses[i].IP.String()))
12061139
err = plugin.ipamInvoker.Delete(&epInfo.IPAddresses[i], nwCfg, args, nwInfo.Options)
12071140
if err != nil {
12081141
return plugin.RetriableError(fmt.Errorf("failed to release address: %w", err))
@@ -1222,7 +1155,7 @@ func (plugin *NetPlugin) Delete(args *cniSkel.CmdArgs) error {
12221155
if err != nil {
12231156
return plugin.RetriableError(fmt.Errorf("failed to save state: %w", err))
12241157
}
1225-
sendEvent(plugin, fmt.Sprintf("CNI DEL succeeded : Released ip %+v podname %v namespace %v", nwCfg.IPAM.Address, k8sPodName, k8sNamespace))
1158+
telemetry.SendEvent(fmt.Sprintf("CNI DEL succeeded : Released ip %+v podname %v namespace %v", nwCfg.IPAM.Address, k8sPodName, k8sNamespace))
12261159

12271160
return err
12281161
}
@@ -1238,11 +1171,8 @@ func (plugin *NetPlugin) Update(args *cniSkel.CmdArgs) error {
12381171
podCfg *cni.K8SPodEnvArgs
12391172
orchestratorContext []byte
12401173
targetNetworkConfig *cns.GetNetworkContainerResponse
1241-
cniMetric telemetry.AIMetric
12421174
)
12431175

1244-
startTime := time.Now()
1245-
12461176
logger.Info("Processing UPDATE command",
12471177
zap.String("netns", args.Netns),
12481178
zap.String("args", args.Args),
@@ -1265,16 +1195,6 @@ func (plugin *NetPlugin) Update(args *cniSkel.CmdArgs) error {
12651195
plugin.setCNIReportDetails(nwCfg, CNI_UPDATE, "")
12661196

12671197
defer func() {
1268-
operationTimeMs := time.Since(startTime).Milliseconds()
1269-
cniMetric.Metric = aitelemetry.Metric{
1270-
Name: telemetry.CNIUpdateTimeMetricStr,
1271-
Value: float64(operationTimeMs),
1272-
AppVersion: plugin.Version,
1273-
CustomDimensions: make(map[string]string),
1274-
}
1275-
SetCustomDimensions(&cniMetric, nwCfg, err)
1276-
telemetry.SendCNIMetric(&cniMetric, plugin.tb)
1277-
12781198
if result == nil {
12791199
result = &cniTypesCurr.Result{}
12801200
}

cni/network/network_linux_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/Azure/azure-container-networking/cns"
1414
"github.com/Azure/azure-container-networking/network"
1515
"github.com/Azure/azure-container-networking/platform"
16-
"github.com/Azure/azure-container-networking/telemetry"
1716
cniSkel "github.com/containernetworking/cni/pkg/skel"
1817
"github.com/containernetworking/cni/pkg/types"
1918
"github.com/stretchr/testify/assert"
@@ -261,8 +260,6 @@ func TestPluginLinuxAdd(t *testing.T) {
261260
plugin: &NetPlugin{
262261
Plugin: resources.Plugin,
263262
nm: network.NewMockNetworkmanager(network.NewMockEndpointClient(nil)),
264-
tb: &telemetry.TelemetryBuffer{},
265-
report: &telemetry.CNIReport{},
266263
multitenancyClient: NewMockMultitenancy(false, []*cns.GetNetworkContainerResponse{GetTestCNSResponse3()}),
267264
},
268265
args: &cniSkel.CmdArgs{
@@ -341,8 +338,6 @@ func TestPluginLinuxAdd(t *testing.T) {
341338
plugin: &NetPlugin{
342339
Plugin: resources.Plugin,
343340
nm: network.NewMockNetworkmanager(network.NewMockEndpointClient(nil)),
344-
tb: &telemetry.TelemetryBuffer{},
345-
report: &telemetry.CNIReport{},
346341
ipamInvoker: &MockIpamInvoker{
347342
add: func(opt IPAMAddConfig) (ipamAddResult IPAMAddResult, err error) {
348343
ipamAddResult = IPAMAddResult{interfaceInfo: make(map[string]network.InterfaceInfo)}

0 commit comments

Comments
 (0)