From f2bcf3774c1d14f51ac32df2977ad1c4bb829366 Mon Sep 17 00:00:00 2001 From: Jon Ayers Date: Thu, 12 Sep 2024 17:21:05 +0000 Subject: [PATCH] fix: avoid logging no such process errors for process priority --- agent/agent.go | 45 ++++++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/agent/agent.go b/agent/agent.go index e401331c5c215..3c958254df0da 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -1677,11 +1677,14 @@ func (a *agent) manageProcessPriority(ctx context.Context, debouncer *logDebounc score, niceErr := proc.Niceness(a.syscaller) if niceErr != nil && !xerrors.Is(niceErr, os.ErrPermission) { - debouncer.Warn(ctx, "unable to get proc niceness", - slog.F("cmd", proc.Cmd()), - slog.F("pid", proc.PID), - slog.Error(niceErr), - ) + if !isNoSuchProcessErr(niceErr) { + debouncer.Warn(ctx, "unable to get proc niceness", + slog.F("cmd", proc.Cmd()), + slog.F("pid", proc.PID), + slog.Error(niceErr), + ) + } + continue } @@ -1697,12 +1700,14 @@ func (a *agent) manageProcessPriority(ctx context.Context, debouncer *logDebounc if niceErr == nil { err := proc.SetNiceness(a.syscaller, niceness) if err != nil && !xerrors.Is(err, os.ErrPermission) { - debouncer.Warn(ctx, "unable to set proc niceness", - slog.F("cmd", proc.Cmd()), - slog.F("pid", proc.PID), - slog.F("niceness", niceness), - slog.Error(err), - ) + if !isNoSuchProcessErr(err) { + debouncer.Warn(ctx, "unable to set proc niceness", + slog.F("cmd", proc.Cmd()), + slog.F("pid", proc.PID), + slog.F("niceness", niceness), + slog.Error(err), + ) + } } } @@ -1711,12 +1716,14 @@ func (a *agent) manageProcessPriority(ctx context.Context, debouncer *logDebounc oomScoreStr := strconv.Itoa(oomScore) err := afero.WriteFile(a.filesystem, fmt.Sprintf("/proc/%d/oom_score_adj", proc.PID), []byte(oomScoreStr), 0o644) if err != nil && !xerrors.Is(err, os.ErrPermission) { - debouncer.Warn(ctx, "unable to set oom_score_adj", - slog.F("cmd", proc.Cmd()), - slog.F("pid", proc.PID), - slog.F("score", oomScoreStr), - slog.Error(err), - ) + if !isNoSuchProcessErr(err) { + debouncer.Warn(ctx, "unable to set oom_score_adj", + slog.F("cmd", proc.Cmd()), + slog.F("pid", proc.PID), + slog.F("score", oomScoreStr), + slog.Error(err), + ) + } } } modProcs = append(modProcs, proc) @@ -2146,3 +2153,7 @@ func (l *logDebouncer) log(ctx context.Context, level slog.Level, msg string, fi } l.messages[msg] = time.Now() } + +func isNoSuchProcessErr(err error) bool { + return err != nil && strings.Contains(err.Error(), "no such process") +}