Skip to content

Commit 3a9370e

Browse files
committed
Log custom plugin stderr only if the status is not ok.
Otherwise with plugins that run frequently and report ok status, the logs are filled with unnecessary noise and significantly increases log size.
1 parent 8a41d4a commit 3a9370e

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

pkg/custompluginmonitor/plugin/plugin.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (p *Plugin) Run() {
8989

9090
// run each rule in parallel and wait for them to complete
9191
func (p *Plugin) runRules() {
92-
glog.Info("Start to run custom plugins")
92+
glog.V(3).Info("Start to run custom plugins")
9393

9494
for _, rule := range p.config.Rules {
9595
p.syncChan <- struct{}{}
@@ -120,7 +120,7 @@ func (p *Plugin) runRules() {
120120
}
121121

122122
p.Wait()
123-
glog.Info("Finish running custom plugins")
123+
glog.V(3).Info("Finish running custom plugins")
124124
}
125125

126126
// readFromReader reads the maxBytes from the reader and drains the rest.
@@ -203,12 +203,6 @@ func (p *Plugin) run(rule cpmtypes.CustomRule) (exitStatus cpmtypes.Status, outp
203203
}
204204
}
205205

206-
// log the stderr from the plugin
207-
if len(stderr) != 0 {
208-
glog.Infof("Start logs from plugin %q \n %s", rule.Path, string(stderr))
209-
glog.Infof("End logs from plugin %q", rule.Path)
210-
}
211-
212206
// trim suffix useless bytes
213207
output = string(stdout)
214208
output = strings.TrimSpace(output)
@@ -227,8 +221,10 @@ func (p *Plugin) run(rule cpmtypes.CustomRule) (exitStatus cpmtypes.Status, outp
227221
case 0:
228222
return cpmtypes.OK, output
229223
case 1:
224+
logPluginStderr(rule.Path, string(stderr))
230225
return cpmtypes.NonOK, output
231226
default:
227+
logPluginStderr(rule.Path, string(stderr))
232228
return cpmtypes.Unknown, output
233229
}
234230
}
@@ -237,3 +233,10 @@ func (p *Plugin) Stop() {
237233
p.tomb.Stop()
238234
glog.Info("Stop plugin execution")
239235
}
236+
237+
func logPluginStderr(path, logs string) {
238+
if len(logs) != 0 {
239+
glog.Infof("Start logs from plugin %q \n %s", path, string(logs))
240+
glog.Infof("End logs from plugin %q", path)
241+
}
242+
}

pkg/healthchecker/health_checker.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
)
3232

3333
type healthChecker struct {
34+
component string
3435
enableRepair bool
3536
healthCheckFunc func() bool
3637
// The repair is "best-effort" and ignores the error from the underlying actions.
@@ -45,6 +46,7 @@ type healthChecker struct {
4546
// NewHealthChecker returns a new health checker configured with the given options.
4647
func NewHealthChecker(hco *options.HealthCheckerOptions) (types.HealthChecker, error) {
4748
hc := &healthChecker{
49+
component: hco.Component,
4850
enableRepair: hco.EnableRepair,
4951
crictlPath: hco.CriCtlPath,
5052
healthCheckTimeout: hco.HealthCheckTimeout,
@@ -139,14 +141,14 @@ func (hc *healthChecker) CheckHealth() bool {
139141
// The service is unhealthy.
140142
// Attempt repair based on flag.
141143
if hc.enableRepair {
142-
glog.Infof("health-checker: component is unhealthy, proceeding to repair")
143144
// repair if the service has been up for the cool down period.
144145
uptime, err := hc.uptimeFunc()
145146
if err != nil {
146-
glog.Infof("health-checker: %v\n", err.Error())
147+
glog.Infof("error in getting uptime for %v: %v\n", hc.component, err)
147148
}
148-
glog.Infof("health-checker: component uptime: %v\n", uptime)
149+
glog.Infof("%v is unhealthy, component uptime: %v\n", hc.component, uptime)
149150
if uptime > hc.coolDownTime {
151+
glog.Infof("%v cooldown period of %v exceeded, repairing", hc.component, hc.coolDownTime)
150152
hc.repairFunc()
151153
}
152154
}
@@ -159,10 +161,9 @@ func execCommand(timeout time.Duration, command string, args ...string) (string,
159161
defer cancel()
160162

161163
cmd := exec.CommandContext(ctx, command, args...)
162-
glog.Infof("health-checker: executing command : %v\n", cmd)
163164
out, err := cmd.Output()
164165
if err != nil {
165-
glog.Infof("health-checker: command failed : %v, %v\n", err.Error(), out)
166+
glog.Infof("command %v failed: %v, %v\n", cmd, err, out)
166167
return "", err
167168
}
168169
return strings.TrimSuffix(string(out), "\n"), nil

0 commit comments

Comments
 (0)