Skip to content

Commit 311eba6

Browse files
authored
test: [NPM] Removing fail on AITelemetry error (#1288)
* Removing extra log lines and adding an option to print in sendLog * removing fail on AI initialization error. * fixing lint
1 parent 2e7b543 commit 311eba6

File tree

4 files changed

+14
-14
lines changed

4 files changed

+14
-14
lines changed

npm/cmd/start.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,12 @@ func start(config npmconfig.Config, flags npmconfig.Flags) error {
134134
npMgr := npm.NewNetworkPolicyManager(config, factory, dp, exec.New(), version, k8sServerVersion)
135135
err = metrics.CreateTelemetryHandle(config.NPMVersion(), version, npm.GetAIMetadata())
136136
if err != nil {
137-
klog.Infof("CreateTelemetryHandle failed with error %v.", err)
138-
return fmt.Errorf("CreateTelemetryHandle failed with error %w", err)
137+
klog.Infof("CreateTelemetryHandle failed with error %v. AITelemetry is not initialized.", err)
139138
}
140139

141140
go restserver.NPMRestServerListenAndServe(config, npMgr)
142141

143-
metrics.SendLog(util.NpmID, "starting NPM")
142+
metrics.SendLog(util.NpmID, "starting NPM", metrics.PrintLog)
144143
if err = npMgr.Start(config, stopChannel); err != nil {
145144
metrics.SendErrorLogAndMetric(util.NpmID, "Failed to start NPM due to %+v", err)
146145
return fmt.Errorf("failed to start with err: %w", err)

npm/cmd/start_daemon.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ func startDaemon(config npmconfig.Config) error {
9696

9797
err = metrics.CreateTelemetryHandle(config.NPMVersion(), version, npm.GetAIMetadata())
9898
if err != nil {
99-
klog.Infof("CreateTelemetryHandle failed with error %v.", err)
100-
return fmt.Errorf("CreateTelemetryHandle failed with error %w", err)
99+
klog.Infof("CreateTelemetryHandle failed with error %v. AITelemetry is not initialized.", err)
101100
}
102101

103102
err = n.Start(config, wait.NeverStop)
@@ -106,7 +105,7 @@ func startDaemon(config npmconfig.Config) error {
106105
return fmt.Errorf("failed to start dataplane: %w", err)
107106
}
108107

109-
metrics.SendLog(util.FanOutServerID, "started fan-out daemon")
108+
metrics.SendLog(util.FanOutServerID, "started fan-out daemon", metrics.PrintLog)
110109

111110
return nil
112111
}

npm/cmd/start_server.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,12 @@ func startControlplane(config npmconfig.Config, flags npmconfig.Flags) error {
115115

116116
err = metrics.CreateTelemetryHandle(config.NPMVersion(), version, npm.GetAIMetadata())
117117
if err != nil {
118-
klog.Infof("CreateTelemetryHandle failed with error %v.", err)
119-
return fmt.Errorf("CreateTelemetryHandle failed with error %w", err)
118+
klog.Infof("CreateTelemetryHandle failed with error %v. AITelemetry is not initialized.", err)
120119
}
121120

122121
go restserver.NPMRestServerListenAndServe(config, npMgr)
123122

124-
metrics.SendLog(util.FanOutServerID, "starting fan-out server")
123+
metrics.SendLog(util.FanOutServerID, "starting fan-out server", metrics.PrintLog)
125124

126125
return npMgr.Start(config, wait.NeverStop) //nolint:wrapcheck // unnecessary to wrap error
127126
}

npm/metrics/ai-utils.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
var (
1515
th aitelemetry.TelemetryHandle
1616
npmVersion int
17+
PrintLog = true
18+
DonotPrint = false
1719
)
1820

1921
// CreateTelemetryHandle creates a handler to initialize AI telemetry
@@ -68,28 +70,29 @@ func SendErrorLogAndMetric(operationID int, format string, args ...interface{})
6870
// Send error logs
6971
msg := fmt.Sprintf(format, args...)
7072
log.Errorf(msg)
71-
SendLog(operationID, msg)
73+
SendLog(operationID, msg, DonotPrint)
7274
}
7375

7476
// SendMetric sends metrics
7577
func SendMetric(metric aitelemetry.Metric) {
7678
if th == nil {
77-
log.Logf("AppInsights didn't initialize")
7879
return
7980
}
8081
th.TrackMetric(metric)
8182
}
8283

8384
// SendLog sends log
84-
func SendLog(operationID int, msg string) {
85+
func SendLog(operationID int, msg string, printLog bool) {
8586
msg = fmt.Sprintf("%s - (NPM v%d)", msg, npmVersion)
8687
report := aitelemetry.Report{
8788
Message: msg,
8889
Context: strconv.Itoa(operationID),
8990
CustomDimensions: make(map[string]string),
9091
}
92+
if printLog {
93+
klog.Infof(msg)
94+
}
9195
if th == nil {
92-
log.Logf("AppInsights didn't initialized.")
9396
return
9497
}
9598
th.TrackLog(report)
@@ -104,5 +107,5 @@ func SendHeartbeatWithNumPolicies() {
104107
message = fmt.Sprintf("warn: NPM hearbeat. Couldn't get number of policies for telemetry log: %v", err)
105108
klog.Warning(message)
106109
}
107-
SendLog(util.NpmID, message)
110+
SendLog(util.NpmID, message, DonotPrint)
108111
}

0 commit comments

Comments
 (0)