diff --git a/npm/ipsm/ipsm.go b/npm/ipsm/ipsm.go index b2564958ce..902cf35f35 100644 --- a/npm/ipsm/ipsm.go +++ b/npm/ipsm/ipsm.go @@ -97,7 +97,7 @@ func (ipsMgr *IpsetManager) CreateList(listName string) error { } log.Logf("Creating List: %+v", entry) if errCode, err := ipsMgr.Run(entry); err != nil && errCode != 1 { - metrics.SendErrorMetric(util.IpsmID, "Error: failed to create ipset list %s.", listName) + metrics.SendErrorLogAndMetric(util.IpsmID, "Error: failed to create ipset list %s.", listName) return err } @@ -118,7 +118,7 @@ func (ipsMgr *IpsetManager) DeleteList(listName string) error { return nil } - metrics.SendErrorMetric(util.IpsmID, "Error: failed to delete ipset %s %+v", listName, entry) + metrics.SendErrorLogAndMetric(util.IpsmID, "Error: failed to delete ipset %s %+v", listName, entry) return err } @@ -148,7 +148,7 @@ func (ipsMgr *IpsetManager) AddToList(listName string, setName string) error { } if errCode, err := ipsMgr.Run(entry); err != nil && errCode != 1 { - metrics.SendErrorMetric(util.IpsmID, "Error: failed to create ipset rules. rule: %+v", entry) + metrics.SendErrorLogAndMetric(util.IpsmID, "Error: failed to create ipset rules. rule: %+v", entry) return err } @@ -160,7 +160,7 @@ func (ipsMgr *IpsetManager) AddToList(listName string, setName string) error { // DeleteFromList removes an ipset to an ipset list. func (ipsMgr *IpsetManager) DeleteFromList(listName string, setName string) error { if _, exists := ipsMgr.listMap[listName]; !exists { - metrics.SendErrorMetric(util.IpsmID, "ipset list with name %s not found", listName) + metrics.SendErrorLogAndMetric(util.IpsmID, "ipset list with name %s not found", listName) return nil } @@ -172,7 +172,7 @@ func (ipsMgr *IpsetManager) DeleteFromList(listName string, setName string) erro } if _, err := ipsMgr.Run(entry); err != nil { - metrics.SendErrorMetric(util.IpsmID, "Error: failed to delete ipset entry. %+v", entry) + metrics.SendErrorLogAndMetric(util.IpsmID, "Error: failed to delete ipset entry. %+v", entry) return err } @@ -183,7 +183,7 @@ func (ipsMgr *IpsetManager) DeleteFromList(listName string, setName string) erro if len(ipsMgr.listMap[listName].elements) == 0 { if err := ipsMgr.DeleteList(listName); err != nil { - metrics.SendErrorMetric(util.IpsmID, "Error: failed to delete ipset list %s.", listName) + metrics.SendErrorLogAndMetric(util.IpsmID, "Error: failed to delete ipset list %s.", listName) return err } } @@ -208,7 +208,7 @@ func (ipsMgr *IpsetManager) CreateSet(setName string, spec []string) error { } log.Logf("Creating Set: %+v", entry) if errCode, err := ipsMgr.Run(entry); err != nil && errCode != 1 { - metrics.SendErrorMetric(util.IpsmID, "Error: failed to create ipset.") + metrics.SendErrorLogAndMetric(util.IpsmID, "Error: failed to create ipset.") return err } @@ -224,7 +224,7 @@ func (ipsMgr *IpsetManager) CreateSet(setName string, spec []string) error { // DeleteSet removes a set from ipset. func (ipsMgr *IpsetManager) DeleteSet(setName string) error { if _, exists := ipsMgr.setMap[setName]; !exists { - metrics.SendErrorMetric(util.IpsmID, "ipset with name %s not found", setName) + metrics.SendErrorLogAndMetric(util.IpsmID, "ipset with name %s not found", setName) return nil } @@ -238,7 +238,7 @@ func (ipsMgr *IpsetManager) DeleteSet(setName string) error { return nil } - metrics.SendErrorMetric(util.IpsmID, "Error: failed to delete ipset %s. Entry: %+v", setName, entry) + metrics.SendErrorLogAndMetric(util.IpsmID, "Error: failed to delete ipset %s. Entry: %+v", setName, entry) return err } @@ -287,7 +287,7 @@ func (ipsMgr *IpsetManager) AddToSet(setName, ip, spec, podUid string) error { } if errCode, err := ipsMgr.Run(entry); err != nil && errCode != 1 { - metrics.SendErrorMetric(util.IpsmID, "Error: failed to create ipset rules. %+v", entry) + metrics.SendErrorLogAndMetric(util.IpsmID, "Error: failed to create ipset rules. %+v", entry) return err } @@ -331,7 +331,7 @@ func (ipsMgr *IpsetManager) DeleteFromSet(setName, ip, podUid string) error { return nil } - metrics.SendErrorMetric(util.IpsmID, "Error: failed to delete ipset entry. Entry: %+v", entry) + metrics.SendErrorLogAndMetric(util.IpsmID, "Error: failed to delete ipset entry. Entry: %+v", entry) return err } @@ -356,7 +356,7 @@ func (ipsMgr *IpsetManager) Clean() error { } if err := ipsMgr.DeleteSet(setName); err != nil { - metrics.SendErrorMetric(util.IpsmID, "Error: failed to clean ipset") + metrics.SendErrorLogAndMetric(util.IpsmID, "Error: failed to clean ipset") return err } } @@ -367,7 +367,7 @@ func (ipsMgr *IpsetManager) Clean() error { } if err := ipsMgr.DeleteList(listName); err != nil { - metrics.SendErrorMetric(util.IpsmID, "Error: failed to clean ipset list") + metrics.SendErrorLogAndMetric(util.IpsmID, "Error: failed to clean ipset list") return err } } @@ -381,13 +381,13 @@ func (ipsMgr *IpsetManager) Destroy() error { operationFlag: util.IpsetFlushFlag, } if _, err := ipsMgr.Run(entry); err != nil { - metrics.SendErrorMetric(util.IpsmID, "Error: failed to flush ipset") + metrics.SendErrorLogAndMetric(util.IpsmID, "Error: failed to flush ipset") return err } entry.operationFlag = util.IpsetDestroyFlag if _, err := ipsMgr.Run(entry); err != nil { - metrics.SendErrorMetric(util.IpsmID, "Error: failed to destroy ipset") + metrics.SendErrorLogAndMetric(util.IpsmID, "Error: failed to destroy ipset") return err } @@ -407,7 +407,7 @@ func (ipsMgr *IpsetManager) Run(entry *ipsEntry) (int, error) { if msg, failed := err.(*exec.ExitError); failed { errCode := msg.Sys().(syscall.WaitStatus).ExitStatus() if errCode > 0 { - metrics.SendErrorMetric(util.IpsmID, "Error: There was an error running command: [%s %v] Stderr: [%v, %s]", cmdName, strings.Join(cmdArgs, " "), err, strings.TrimSuffix(string(msg.Stderr), "\n")) + metrics.SendErrorLogAndMetric(util.IpsmID, "Error: There was an error running command: [%s %v] Stderr: [%v, %s]", cmdName, strings.Join(cmdArgs, " "), err, strings.TrimSuffix(string(msg.Stderr), "\n")) } return errCode, err @@ -424,7 +424,7 @@ func (ipsMgr *IpsetManager) Save(configFile string) error { cmd := exec.Command(util.Ipset, util.IpsetSaveFlag, util.IpsetFileFlag, configFile) if err := cmd.Start(); err != nil { - metrics.SendErrorMetric(util.IpsmID, "Error: failed to save ipset to file.") + metrics.SendErrorLogAndMetric(util.IpsmID, "Error: failed to save ipset to file.") return err } cmd.Wait() @@ -440,7 +440,7 @@ func (ipsMgr *IpsetManager) Restore(configFile string) error { f, err := os.Stat(configFile) if err != nil { - metrics.SendErrorMetric(util.IpsmID, "Error: failed to get file %s stat from ipsm.Restore", configFile) + metrics.SendErrorLogAndMetric(util.IpsmID, "Error: failed to get file %s stat from ipsm.Restore", configFile) return err } @@ -452,7 +452,7 @@ func (ipsMgr *IpsetManager) Restore(configFile string) error { cmd := exec.Command(util.Ipset, util.IpsetRestoreFlag, util.IpsetFileFlag, configFile) if err := cmd.Start(); err != nil { - metrics.SendErrorMetric(util.IpsmID, "Error: failed to to restore ipset from file.") + metrics.SendErrorLogAndMetric(util.IpsmID, "Error: failed to to restore ipset from file.") return err } cmd.Wait() @@ -472,13 +472,13 @@ func (ipsMgr *IpsetManager) DestroyNpmIpsets() error { if msg, failed := err.(*exec.ExitError); failed { errCode := msg.Sys().(syscall.WaitStatus).ExitStatus() if errCode > 0 { - metrics.SendErrorMetric(util.IpsmID, "{DestroyNpmIpsets} Error: There was an error running command: [%s] Stderr: [%v, %s]", cmdName, err, strings.TrimSuffix(string(msg.Stderr), "\n")) + metrics.SendErrorLogAndMetric(util.IpsmID, "{DestroyNpmIpsets} Error: There was an error running command: [%s] Stderr: [%v, %s]", cmdName, err, strings.TrimSuffix(string(msg.Stderr), "\n")) } return err } if reply == nil { - metrics.SendErrorMetric(util.IpsmID, "{DestroyNpmIpsets} Received empty string from ipset list while destroying azure-npm ipsets") + metrics.SendErrorLogAndMetric(util.IpsmID, "{DestroyNpmIpsets} Received empty string from ipset list while destroying azure-npm ipsets") return nil } @@ -516,7 +516,7 @@ func (ipsMgr *IpsetManager) DestroyNpmIpsets() error { } if _, err := ipsMgr.Run(entry); err != nil { - metrics.SendErrorMetric(util.IpsmID, "{DestroyNpmIpsets} Error: failed to flush ipset %s", ipsetName) + metrics.SendErrorLogAndMetric(util.IpsmID, "{DestroyNpmIpsets} Error: failed to flush ipset %s", ipsetName) } } @@ -524,7 +524,7 @@ func (ipsMgr *IpsetManager) DestroyNpmIpsets() error { entry.operationFlag = util.IpsetDestroyFlag entry.set = ipsetName if _, err := ipsMgr.Run(entry); err != nil { - metrics.SendErrorMetric(util.IpsmID, "{DestroyNpmIpsets} Error: failed to destroy ipset %s", ipsetName) + metrics.SendErrorLogAndMetric(util.IpsmID, "{DestroyNpmIpsets} Error: failed to destroy ipset %s", ipsetName) } } diff --git a/npm/iptm/iptm.go b/npm/iptm/iptm.go index 6935dd7473..757a4efa2d 100644 --- a/npm/iptm/iptm.go +++ b/npm/iptm/iptm.go @@ -11,10 +11,10 @@ import ( "syscall" "time" - "golang.org/x/sys/unix" "github.com/Azure/azure-container-networking/log" "github.com/Azure/azure-container-networking/npm/metrics" "github.com/Azure/azure-container-networking/npm/util" + "golang.org/x/sys/unix" "k8s.io/apimachinery/pkg/util/wait" // utiliptables "k8s.io/kubernetes/pkg/util/iptables" ) @@ -88,7 +88,7 @@ func (iptMgr *IptablesManager) InitNpmChains() error { iptMgr.OperationFlag = util.IptablesInsertionFlag entry.Specs = append([]string{index}, entry.Specs...) if _, err = iptMgr.Run(entry); err != nil { - metrics.SendErrorMetric(util.IptmID, "Error: failed to add AZURE-NPM chain to FORWARD chain.") + metrics.SendErrorLogAndMetric(util.IptmID, "Error: failed to add AZURE-NPM chain to FORWARD chain.") return err } } @@ -109,7 +109,7 @@ func (iptMgr *IptablesManager) InitNpmChains() error { if !exists { iptMgr.OperationFlag = util.IptablesAppendFlag if _, err := iptMgr.Run(entry); err != nil { - metrics.SendErrorMetric(util.IptmID, "Error: failed to add AZURE-NPM-INGRESS-PORT chain to AZURE-NPM chain.") + metrics.SendErrorLogAndMetric(util.IptmID, "Error: failed to add AZURE-NPM-INGRESS-PORT chain to AZURE-NPM chain.") return err } } @@ -135,7 +135,7 @@ func (iptMgr *IptablesManager) InitNpmChains() error { if !exists { iptMgr.OperationFlag = util.IptablesAppendFlag if _, err := iptMgr.Run(entry); err != nil { - metrics.SendErrorMetric(util.IptmID, "Error: failed to add AZURE-NPM-INGRESS-PORT chain to AZURE-NPM chain.") + metrics.SendErrorLogAndMetric(util.IptmID, "Error: failed to add AZURE-NPM-INGRESS-PORT chain to AZURE-NPM chain.") return err } } @@ -161,7 +161,7 @@ func (iptMgr *IptablesManager) InitNpmChains() error { if !exists { iptMgr.OperationFlag = util.IptablesAppendFlag if _, err := iptMgr.Run(entry); err != nil { - metrics.SendErrorMetric(util.IptmID, "Error: failed to add AZURE-NPM-TARGET-SETS chain to AZURE-NPM chain.") + metrics.SendErrorLogAndMetric(util.IptmID, "Error: failed to add AZURE-NPM-TARGET-SETS chain to AZURE-NPM chain.") return err } } @@ -184,7 +184,7 @@ func (iptMgr *IptablesManager) InitNpmChains() error { if !exists { iptMgr.OperationFlag = util.IptablesAppendFlag if _, err = iptMgr.Run(entry); err != nil { - metrics.SendErrorMetric(util.IptmID, "Error: failed to add default allow CONNECTED/RELATED rule to AZURE-NPM chain.") + metrics.SendErrorLogAndMetric(util.IptmID, "Error: failed to add default allow CONNECTED/RELATED rule to AZURE-NPM chain.") return err } } @@ -214,7 +214,7 @@ func (iptMgr *IptablesManager) UninitNpmChains() error { iptMgr.OperationFlag = util.IptablesDeletionFlag errCode, err := iptMgr.Run(entry) if errCode != iptablesErrDoesNotExist && err != nil { - metrics.SendErrorMetric(util.IptmID, "Error: failed to add default allow CONNECTED/RELATED rule to AZURE-NPM chain.") + metrics.SendErrorLogAndMetric(util.IptmID, "Error: failed to add default allow CONNECTED/RELATED rule to AZURE-NPM chain.") return err } @@ -225,7 +225,7 @@ func (iptMgr *IptablesManager) UninitNpmChains() error { } errCode, err := iptMgr.Run(entry) if errCode != iptablesErrDoesNotExist && err != nil { - metrics.SendErrorMetric(util.IptmID, "Error: failed to flush iptables chain %s.", chain) + metrics.SendErrorLogAndMetric(util.IptmID, "Error: failed to flush iptables chain %s.", chain) } } @@ -266,7 +266,7 @@ func (iptMgr *IptablesManager) AddChain(chain string) error { return nil } - metrics.SendErrorMetric(util.IptmID, "Error: failed to create iptables chain %s.", entry.Chain) + metrics.SendErrorLogAndMetric(util.IptmID, "Error: failed to create iptables chain %s.", entry.Chain) return err } @@ -286,7 +286,7 @@ func (iptMgr *IptablesManager) DeleteChain(chain string) error { return nil } - metrics.SendErrorMetric(util.IptmID, "Error: failed to delete iptables chain %s.", entry.Chain) + metrics.SendErrorLogAndMetric(util.IptmID, "Error: failed to delete iptables chain %s.", entry.Chain) return err } @@ -305,7 +305,7 @@ func (iptMgr *IptablesManager) Add(entry *IptEntry) error { iptMgr.OperationFlag = util.IptablesInsertionFlag } if _, err := iptMgr.Run(entry); err != nil { - metrics.SendErrorMetric(util.IptmID, "Error: failed to create iptables rules.") + metrics.SendErrorLogAndMetric(util.IptmID, "Error: failed to create iptables rules.") return err } @@ -330,7 +330,7 @@ func (iptMgr *IptablesManager) Delete(entry *IptEntry) error { iptMgr.OperationFlag = util.IptablesDeletionFlag if _, err := iptMgr.Run(entry); err != nil { - metrics.SendErrorMetric(util.IptmID, "Error: failed to delete iptables rules.") + metrics.SendErrorLogAndMetric(util.IptmID, "Error: failed to delete iptables rules.") return err } @@ -360,7 +360,7 @@ func (iptMgr *IptablesManager) Run(entry *IptEntry) (int, error) { if msg, failed := err.(*exec.ExitError); failed { errCode := msg.Sys().(syscall.WaitStatus).ExitStatus() if errCode > 0 && iptMgr.OperationFlag != util.IptablesCheckFlag { - metrics.SendErrorMetric(util.IptmID, "Error: There was an error running command: [%s %v] Stderr: [%v, %s]", cmdName, strings.Join(cmdArgs, " "), err, strings.TrimSuffix(string(msg.Stderr), "\n")) + metrics.SendErrorLogAndMetric(util.IptmID, "Error: There was an error running command: [%s %v] Stderr: [%v, %s]", cmdName, strings.Join(cmdArgs, " "), err, strings.TrimSuffix(string(msg.Stderr), "\n")) } return errCode, err @@ -389,7 +389,7 @@ func (iptMgr *IptablesManager) Save(configFile string) error { // create the config file for writing f, err := os.Create(configFile) if err != nil { - metrics.SendErrorMetric(util.IptmID, "Error: failed to open file: %s.", configFile) + metrics.SendErrorLogAndMetric(util.IptmID, "Error: failed to open file: %s.", configFile) return err } defer f.Close() @@ -397,7 +397,7 @@ func (iptMgr *IptablesManager) Save(configFile string) error { cmd := exec.Command(util.IptablesSave) cmd.Stdout = f if err := cmd.Start(); err != nil { - metrics.SendErrorMetric(util.IptmID, "Error: failed to run iptables-save.") + metrics.SendErrorLogAndMetric(util.IptmID, "Error: failed to run iptables-save.") return err } cmd.Wait() @@ -425,7 +425,7 @@ func (iptMgr *IptablesManager) Restore(configFile string) error { // open the config file for reading f, err := os.Open(configFile) if err != nil { - metrics.SendErrorMetric(util.IptmID, "Error: failed to open file: %s.", configFile) + metrics.SendErrorLogAndMetric(util.IptmID, "Error: failed to open file: %s.", configFile) return err } defer f.Close() @@ -433,7 +433,7 @@ func (iptMgr *IptablesManager) Restore(configFile string) error { cmd := exec.Command(util.IptablesRestore) cmd.Stdin = f if err := cmd.Start(); err != nil { - metrics.SendErrorMetric(util.IptmID, "Error: failed to run iptables-restore.") + metrics.SendErrorLogAndMetric(util.IptmID, "Error: failed to run iptables-restore.") return err } cmd.Wait() @@ -456,7 +456,7 @@ func grabIptablesLocks() (*os.File, error) { // Grab 1.6.x style lock. l, err := os.OpenFile(util.IptablesLockFile, os.O_CREATE, 0600) if err != nil { - metrics.SendErrorMetric(util.IptmID, "Error: failed to open iptables lock file %s.", util.IptablesLockFile) + metrics.SendErrorLogAndMetric(util.IptmID, "Error: failed to open iptables lock file %s.", util.IptablesLockFile) return nil, err } @@ -467,7 +467,7 @@ func grabIptablesLocks() (*os.File, error) { return true, nil }); err != nil { - metrics.SendErrorMetric(util.IptmID, "Error: failed to acquire new iptables lock: %v.", err) + metrics.SendErrorLogAndMetric(util.IptmID, "Error: failed to acquire new iptables lock: %v.", err) return nil, err } @@ -503,4 +503,4 @@ func grabIptablesFileLock(f *os.File) error { // // Write table headers. // writeLine(filterChains, "*filter") -// } \ No newline at end of file +// } diff --git a/npm/metrics/ai-utils.go b/npm/metrics/ai-utils.go index 271f2f0d1e..bf86746e21 100644 --- a/npm/metrics/ai-utils.go +++ b/npm/metrics/ai-utils.go @@ -50,8 +50,8 @@ func CreateTelemetryHandle(version, aiMetadata string) error { return nil } -// SendErrorMetric is responsible for sending error metrics trhough AI telemetry -func SendErrorMetric(operationID int, format string, args ...interface{}) { +// SendErrorLogAndMetric is responsible for sending log and error metrics through AI telemetry +func SendErrorLogAndMetric(operationID int, format string, args ...interface{}) { // Send error metrics customDimensions := map[string]string{ util.ErrorCode: strconv.Itoa(operationID), diff --git a/npm/npm.go b/npm/npm.go index 5d39be79a1..b86337d7cc 100644 --- a/npm/npm.go +++ b/npm/npm.go @@ -139,7 +139,7 @@ func (npMgr *NetworkPolicyManager) restore() { time.Sleep(restoreRetryWaitTimeInSeconds * time.Second) } - metrics.SendErrorMetric(util.NpmID, "Error: timeout restoring Azure-NPM states") + metrics.SendErrorLogAndMetric(util.NpmID, "Error: timeout restoring Azure-NPM states") panic(err.Error) } @@ -151,7 +151,7 @@ func (npMgr *NetworkPolicyManager) backup() { time.Sleep(backupWaitTimeInSeconds * time.Second) if err = iptMgr.Save(util.IptablesConfigFile); err != nil { - metrics.SendErrorMetric(util.NpmID, "Error: failed to back up Azure-NPM states") + metrics.SendErrorLogAndMetric(util.NpmID, "Error: failed to back up Azure-NPM states") } } } @@ -163,17 +163,17 @@ func (npMgr *NetworkPolicyManager) Start(stopCh <-chan struct{}) error { // Wait for the initial sync of local cache. if !cache.WaitForCacheSync(stopCh, npMgr.podInformer.Informer().HasSynced) { - metrics.SendErrorMetric(util.NpmID, "Pod informer failed to sync") + metrics.SendErrorLogAndMetric(util.NpmID, "Pod informer failed to sync") return fmt.Errorf("Pod informer failed to sync") } if !cache.WaitForCacheSync(stopCh, npMgr.nsInformer.Informer().HasSynced) { - metrics.SendErrorMetric(util.NpmID, "Namespace informer failed to sync") + metrics.SendErrorLogAndMetric(util.NpmID, "Namespace informer failed to sync") return fmt.Errorf("Namespace informer failed to sync") } if !cache.WaitForCacheSync(stopCh, npMgr.npInformer.Informer().HasSynced) { - metrics.SendErrorMetric(util.NpmID, "Network policy informer failed to sync") + metrics.SendErrorLogAndMetric(util.NpmID, "Network policy informer failed to sync") return fmt.Errorf("Network policy informer failed to sync") } @@ -208,13 +208,13 @@ func NewNetworkPolicyManager(clientset *kubernetes.Clientset, informerFactory in } } if err != nil { - metrics.SendErrorMetric(util.NpmID, "Error: failed to retrieving kubernetes version") + metrics.SendErrorLogAndMetric(util.NpmID, "Error: failed to retrieving kubernetes version") panic(err.Error) } log.Logf("API server version: %+v", serverVersion) if err = util.SetIsNewNwPolicyVerFlag(serverVersion); err != nil { - metrics.SendErrorMetric(util.NpmID, "Error: failed to set IsNewNwPolicyVerFlag") + metrics.SendErrorLogAndMetric(util.NpmID, "Error: failed to set IsNewNwPolicyVerFlag") panic(err.Error) } @@ -245,7 +245,7 @@ func NewNetworkPolicyManager(clientset *kubernetes.Clientset, informerFactory in // Create ipset for the namespace. kubeSystemNs := "ns-" + util.KubeSystemFlag if err := allNs.ipsMgr.CreateSet(kubeSystemNs, append([]string{util.IpsetNetHashFlag})); err != nil { - metrics.SendErrorMetric(util.NpmID, "Error: failed to create ipset for namespace %s.", kubeSystemNs) + metrics.SendErrorLogAndMetric(util.NpmID, "Error: failed to create ipset for namespace %s.", kubeSystemNs) } podInformer.Informer().AddEventHandler(