diff --git a/npm/cmd/start.go b/npm/cmd/start.go index ed4eeaa0a2..db7c7f56a0 100644 --- a/npm/cmd/start.go +++ b/npm/cmd/start.go @@ -134,13 +134,12 @@ func start(config npmconfig.Config, flags npmconfig.Flags) error { npMgr := npm.NewNetworkPolicyManager(config, factory, dp, exec.New(), version, k8sServerVersion) err = metrics.CreateTelemetryHandle(config.NPMVersion(), version, npm.GetAIMetadata()) if err != nil { - klog.Infof("CreateTelemetryHandle failed with error %v.", err) - return fmt.Errorf("CreateTelemetryHandle failed with error %w", err) + klog.Infof("CreateTelemetryHandle failed with error %v. AITelemetry is not initialized.", err) } go restserver.NPMRestServerListenAndServe(config, npMgr) - metrics.SendLog(util.NpmID, "starting NPM") + metrics.SendLog(util.NpmID, "starting NPM", metrics.PrintLog) if err = npMgr.Start(config, stopChannel); err != nil { metrics.SendErrorLogAndMetric(util.NpmID, "Failed to start NPM due to %+v", err) return fmt.Errorf("failed to start with err: %w", err) diff --git a/npm/cmd/start_daemon.go b/npm/cmd/start_daemon.go index 4fa0fe3c6a..d0af8f276e 100644 --- a/npm/cmd/start_daemon.go +++ b/npm/cmd/start_daemon.go @@ -96,8 +96,7 @@ func startDaemon(config npmconfig.Config) error { err = metrics.CreateTelemetryHandle(config.NPMVersion(), version, npm.GetAIMetadata()) if err != nil { - klog.Infof("CreateTelemetryHandle failed with error %v.", err) - return fmt.Errorf("CreateTelemetryHandle failed with error %w", err) + klog.Infof("CreateTelemetryHandle failed with error %v. AITelemetry is not initialized.", err) } err = n.Start(config, wait.NeverStop) @@ -106,7 +105,7 @@ func startDaemon(config npmconfig.Config) error { return fmt.Errorf("failed to start dataplane: %w", err) } - metrics.SendLog(util.FanOutServerID, "started fan-out daemon") + metrics.SendLog(util.FanOutServerID, "started fan-out daemon", metrics.PrintLog) return nil } diff --git a/npm/cmd/start_server.go b/npm/cmd/start_server.go index d620543a8c..6137902d30 100644 --- a/npm/cmd/start_server.go +++ b/npm/cmd/start_server.go @@ -115,13 +115,12 @@ func startControlplane(config npmconfig.Config, flags npmconfig.Flags) error { err = metrics.CreateTelemetryHandle(config.NPMVersion(), version, npm.GetAIMetadata()) if err != nil { - klog.Infof("CreateTelemetryHandle failed with error %v.", err) - return fmt.Errorf("CreateTelemetryHandle failed with error %w", err) + klog.Infof("CreateTelemetryHandle failed with error %v. AITelemetry is not initialized.", err) } go restserver.NPMRestServerListenAndServe(config, npMgr) - metrics.SendLog(util.FanOutServerID, "starting fan-out server") + metrics.SendLog(util.FanOutServerID, "starting fan-out server", metrics.PrintLog) return npMgr.Start(config, wait.NeverStop) //nolint:wrapcheck // unnecessary to wrap error } diff --git a/npm/metrics/ai-utils.go b/npm/metrics/ai-utils.go index ee74dc2e94..769042dde1 100644 --- a/npm/metrics/ai-utils.go +++ b/npm/metrics/ai-utils.go @@ -14,6 +14,8 @@ import ( var ( th aitelemetry.TelemetryHandle npmVersion int + PrintLog = true + DonotPrint = false ) // CreateTelemetryHandle creates a handler to initialize AI telemetry @@ -68,28 +70,29 @@ func SendErrorLogAndMetric(operationID int, format string, args ...interface{}) // Send error logs msg := fmt.Sprintf(format, args...) log.Errorf(msg) - SendLog(operationID, msg) + SendLog(operationID, msg, DonotPrint) } // SendMetric sends metrics func SendMetric(metric aitelemetry.Metric) { if th == nil { - log.Logf("AppInsights didn't initialize") return } th.TrackMetric(metric) } // SendLog sends log -func SendLog(operationID int, msg string) { +func SendLog(operationID int, msg string, printLog bool) { msg = fmt.Sprintf("%s - (NPM v%d)", msg, npmVersion) report := aitelemetry.Report{ Message: msg, Context: strconv.Itoa(operationID), CustomDimensions: make(map[string]string), } + if printLog { + klog.Infof(msg) + } if th == nil { - log.Logf("AppInsights didn't initialized.") return } th.TrackLog(report) @@ -104,5 +107,5 @@ func SendHeartbeatWithNumPolicies() { message = fmt.Sprintf("warn: NPM hearbeat. Couldn't get number of policies for telemetry log: %v", err) klog.Warning(message) } - SendLog(util.NpmID, message) + SendLog(util.NpmID, message, DonotPrint) }