Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions npm/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions npm/cmd/start_daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
5 changes: 2 additions & 3 deletions npm/cmd/start_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
13 changes: 8 additions & 5 deletions npm/metrics/ai-utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
var (
th aitelemetry.TelemetryHandle
npmVersion int
PrintLog = true
DonotPrint = false
)

// CreateTelemetryHandle creates a handler to initialize AI telemetry
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}