88 "encoding/json"
99 "fmt"
1010 "net"
11+ "os"
1112 "time"
1213
1314 "github.com/Azure/azure-container-networking/aitelemetry"
@@ -161,6 +162,19 @@ func (plugin *NetPlugin) Start(config *common.PluginConfig) error {
161162 return nil
162163}
163164
165+ // This function for sending CNI metrics to telemetry service
166+ func logAndSendEvent (plugin * NetPlugin , msg string ) {
167+ log .Printf (msg )
168+ sendEvent (plugin , msg )
169+ }
170+
171+ func sendEvent (plugin * NetPlugin , msg string ) {
172+ eventMsg := fmt .Sprintf ("[%d] %s" , os .Getpid (), msg )
173+ plugin .report .Version = plugin .Version
174+ plugin .report .EventMessage = eventMsg
175+ telemetry .SendCNIEvent (plugin .tb , plugin .report )
176+ }
177+
164178func (plugin * NetPlugin ) GetAllEndpointState (networkid string ) (* api.AzureCNIState , error ) {
165179 st := api.AzureCNIState {
166180 ContainerInterfaces : make (map [string ]api.PodNetworkInterfaceInfo ),
@@ -321,7 +335,7 @@ func (plugin *NetPlugin) Add(args *cniSkel.CmdArgs) error {
321335
322336 startTime := time .Now ()
323337
324- telemetry . LogAndSendEvent (plugin . tb , fmt .Sprintf ("[cni-net] Processing ADD command with args {ContainerID:%v Netns:%v IfName:%v Args:%v Path:%v StdinData:%s}." ,
338+ logAndSendEvent (plugin , fmt .Sprintf ("[cni-net] Processing ADD command with args {ContainerID:%v Netns:%v IfName:%v Args:%v Path:%v StdinData:%s}." ,
325339 args .ContainerID , args .Netns , args .IfName , args .Args , args .Path , args .StdinData ))
326340
327341 // Parse network configuration from stdin.
@@ -339,6 +353,7 @@ func (plugin *NetPlugin) Add(args *cniSkel.CmdArgs) error {
339353 cniMetric .Metric = aitelemetry.Metric {
340354 Name : telemetry .CNIAddTimeMetricStr ,
341355 Value : float64 (operationTimeMs ),
356+ AppVersion : plugin .Version ,
342357 CustomDimensions : make (map [string ]string ),
343358 }
344359 SetCustomDimensions (& cniMetric , nwCfg , err )
@@ -501,7 +516,7 @@ func (plugin *NetPlugin) Add(args *cniSkel.CmdArgs) error {
501516 }
502517 }
503518
504- telemetry . SendCNIEvent (plugin . tb , fmt .Sprintf ("Allocated IPAddress from ipam:%+v v6:%+v" , ipamAddResult .ipv4Result , ipamAddResult .ipv6Result ))
519+ sendEvent (plugin , fmt .Sprintf ("Allocated IPAddress from ipam:%+v v6:%+v" , ipamAddResult .ipv4Result , ipamAddResult .ipv6Result ))
505520
506521 defer func () {
507522 if err != nil {
@@ -512,14 +527,14 @@ func (plugin *NetPlugin) Add(args *cniSkel.CmdArgs) error {
512527 // Create network
513528 if nwInfoErr != nil {
514529 // Network does not exist.
515- telemetry . LogAndSendEvent (plugin . tb , fmt .Sprintf ("[cni-net] Creating network %v." , networkID ))
530+ logAndSendEvent (plugin , fmt .Sprintf ("[cni-net] Creating network %v." , networkID ))
516531 // opts map needs to get passed in here
517532 if nwInfo , err = plugin .createNetworkInternal (networkID , policies , ipamAddConfig , ipamAddResult ); err != nil {
518533 log .Errorf ("Create network failed: %w" , err )
519534 return err
520535 }
521536
522- telemetry . LogAndSendEvent (plugin . tb , fmt .Sprintf ("[cni-net] Created network %v with subnet %v." , networkID , ipamAddResult .hostSubnetPrefix .String ()))
537+ logAndSendEvent (plugin , fmt .Sprintf ("[cni-net] Created network %v with subnet %v." , networkID , ipamAddResult .hostSubnetPrefix .String ()))
523538 }
524539
525540 natInfo := getNATInfo (nwCfg .ExecutionMode , options [network .SNATIPKey ], nwCfg .MultiTenancy , enableSnatForDNS )
@@ -546,7 +561,7 @@ func (plugin *NetPlugin) Add(args *cniSkel.CmdArgs) error {
546561 return err
547562 }
548563
549- telemetry . LogAndSendEvent (plugin . tb , fmt .Sprintf ("CNI ADD succeeded : IP:%+v, VlanID: %v, podname %v, namespace %v numendpoints:%d" ,
564+ sendEvent (plugin , fmt .Sprintf ("CNI ADD succeeded : IP:%+v, VlanID: %v, podname %v, namespace %v numendpoints:%d" ,
550565 ipamAddResult .ipv4Result .IPs , epInfo .Data [network .VlanIDKey ], k8sPodName , k8sNamespace , plugin .nm .GetNumberOfEndpoints ("" , nwCfg .Name )))
551566
552567 return nil
@@ -749,7 +764,7 @@ func (plugin *NetPlugin) createEndpointInternal(opt *createEndpointInternalOpt)
749764 }
750765
751766 // Create the endpoint.
752- telemetry . LogAndSendEvent (plugin . tb , fmt .Sprintf ("[cni-net] Creating endpoint %s." , epInfo .PrettyString ()))
767+ logAndSendEvent (plugin , fmt .Sprintf ("[cni-net] Creating endpoint %s." , epInfo .PrettyString ()))
753768 err = plugin .nm .CreateEndpoint (cnsclient , opt .nwInfo .Id , & epInfo )
754769 if err != nil {
755770 err = plugin .Errorf ("Failed to create endpoint: %v" , err )
@@ -859,12 +874,11 @@ func (plugin *NetPlugin) Delete(args *cniSkel.CmdArgs) error {
859874 nwInfo network.NetworkInfo
860875 epInfo * network.EndpointInfo
861876 cniMetric telemetry.AIMetric
862- msg string
863877 )
864878
865879 startTime := time .Now ()
866880
867- telemetry . LogAndSendEvent (plugin . tb , fmt .Sprintf ("[cni-net] Processing DEL command with args {ContainerID:%v Netns:%v IfName:%v Args:%v Path:%v, StdinData:%s}." ,
881+ logAndSendEvent (plugin , fmt .Sprintf ("[cni-net] Processing DEL command with args {ContainerID:%v Netns:%v IfName:%v Args:%v Path:%v, StdinData:%s}." ,
868882 args .ContainerID , args .Netns , args .IfName , args .Args , args .Path , args .StdinData ))
869883
870884 defer func () {
@@ -883,13 +897,16 @@ func (plugin *NetPlugin) Delete(args *cniSkel.CmdArgs) error {
883897 }
884898
885899 plugin .setCNIReportDetails (nwCfg , CNI_DEL , "" )
900+ plugin .report .ContainerName = k8sPodName + ":" + k8sNamespace
901+
886902 iptables .DisableIPTableLock = nwCfg .DisableIPTableLock
887903
888904 sendMetricFunc := func () {
889905 operationTimeMs := time .Since (startTime ).Milliseconds ()
890906 cniMetric .Metric = aitelemetry.Metric {
891907 Name : telemetry .CNIDelTimeMetricStr ,
892908 Value : float64 (operationTimeMs ),
909+ AppVersion : plugin .Version ,
893910 CustomDimensions : make (map [string ]string ),
894911 }
895912 SetCustomDimensions (& cniMetric , nwCfg , err )
@@ -957,7 +974,7 @@ func (plugin *NetPlugin) Delete(args *cniSkel.CmdArgs) error {
957974 // attempt to release address associated with this Endpoint id
958975 // This is to ensure clean up is done even in failure cases
959976 log .Printf ("[cni-net] Failed to query endpoint %s: %v" , endpointID , err )
960- telemetry . LogAndSendEvent (plugin . tb , fmt .Sprintf ("Release ip by ContainerID (endpoint not found):%v" , args .ContainerID ))
977+ logAndSendEvent (plugin , fmt .Sprintf ("Release ip by ContainerID (endpoint not found):%v" , args .ContainerID ))
961978 if err = plugin .ipamInvoker .Delete (nil , nwCfg , args , nwInfo .Options ); err != nil {
962979 return plugin .RetriableError (fmt .Errorf ("failed to release address(no endpoint): %w" , err ))
963980 }
@@ -970,7 +987,7 @@ func (plugin *NetPlugin) Delete(args *cniSkel.CmdArgs) error {
970987
971988 // schedule send metric before attempting delete
972989 defer sendMetricFunc ()
973- telemetry . LogAndSendEvent (plugin . tb , fmt .Sprintf ("Deleting endpoint:%v" , endpointID ))
990+ logAndSendEvent (plugin , fmt .Sprintf ("Deleting endpoint:%v" , endpointID ))
974991 // Delete the endpoint.
975992 if err = plugin .nm .DeleteEndpoint (networkID , endpointID ); err != nil {
976993 // return a retriable error so the container runtime will retry this DEL later
@@ -982,7 +999,7 @@ func (plugin *NetPlugin) Delete(args *cniSkel.CmdArgs) error {
982999 if ! nwCfg .MultiTenancy {
9831000 // Call into IPAM plugin to release the endpoint's addresses.
9841001 for _ , address := range epInfo .IPAddresses {
985- telemetry . LogAndSendEvent (plugin . tb , fmt .Sprintf ("Release ip:%s" , address .IP .String ()))
1002+ logAndSendEvent (plugin , fmt .Sprintf ("Release ip:%s" , address .IP .String ()))
9861003 err = plugin .ipamInvoker .Delete (& address , nwCfg , args , nwInfo .Options )
9871004 if err != nil {
9881005 return plugin .RetriableError (fmt .Errorf ("failed to release address: %w" , err ))
@@ -997,8 +1014,7 @@ func (plugin *NetPlugin) Delete(args *cniSkel.CmdArgs) error {
9971014 }
9981015 }
9991016
1000- plugin .setCNIReportDetails (nwCfg , CNI_DEL , msg )
1001- telemetry .SendCNIEvent (plugin .tb , fmt .Sprintf ("CNI DEL succeeded : Released ip %+v podname %v namespace %v" , nwCfg .Ipam .Address , k8sPodName , k8sNamespace ))
1017+ sendEvent (plugin , fmt .Sprintf ("CNI DEL succeeded : Released ip %+v podname %v namespace %v" , nwCfg .Ipam .Address , k8sPodName , k8sNamespace ))
10021018
10031019 return err
10041020}
@@ -1038,6 +1054,7 @@ func (plugin *NetPlugin) Update(args *cniSkel.CmdArgs) error {
10381054 cniMetric .Metric = aitelemetry.Metric {
10391055 Name : telemetry .CNIUpdateTimeMetricStr ,
10401056 Value : float64 (operationTimeMs ),
1057+ AppVersion : plugin .Version ,
10411058 CustomDimensions : make (map [string ]string ),
10421059 }
10431060 SetCustomDimensions (& cniMetric , nwCfg , err )
0 commit comments