From 9332d153ffcce435d799501f96c4fbe33bd0126c Mon Sep 17 00:00:00 2001 From: Jon Ayers Date: Thu, 12 Sep 2024 19:00:42 +0100 Subject: [PATCH 1/2] fix: avoid logging no such process errors for process priority (#14655) --- agent/agent.go | 45 ++++++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/agent/agent.go b/agent/agent.go index f0e357479bc47..08e29238e3d2c 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -1670,11 +1670,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 } @@ -1690,12 +1693,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), + ) + } } } @@ -1704,12 +1709,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) @@ -2139,3 +2146,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") +} From d0bce331d40925dbeafac851bf823a41c16f1f28 Mon Sep 17 00:00:00 2001 From: Jon Ayers Date: Thu, 12 Sep 2024 23:00:04 +0100 Subject: [PATCH 2/2] fix: suppress benign errors when listing processes (#14660) --- agent/agent.go | 55 ++++++++++++++++++------------------ agent/agentproc/proc_unix.go | 14 +++++++-- 2 files changed, 38 insertions(+), 31 deletions(-) diff --git a/agent/agent.go b/agent/agent.go index 08e29238e3d2c..833fabc212265 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -1669,16 +1669,12 @@ func (a *agent) manageProcessPriority(ctx context.Context, debouncer *logDebounc } score, niceErr := proc.Niceness(a.syscaller) - if niceErr != nil && !xerrors.Is(niceErr, os.ErrPermission) { - 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 + if !isBenignProcessErr(niceErr) { + debouncer.Warn(ctx, "unable to get proc niceness", + slog.F("cmd", proc.Cmd()), + slog.F("pid", proc.PID), + slog.Error(niceErr), + ) } // We only want processes that don't have a nice value set @@ -1692,15 +1688,13 @@ 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) { - 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), - ) - } + if !isBenignProcessErr(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), + ) } } @@ -1708,15 +1702,13 @@ func (a *agent) manageProcessPriority(ctx context.Context, debouncer *logDebounc if oomScore != unsetOOMScore && oomScore != proc.OOMScoreAdj && !isCustomOOMScore(agentScore, proc) { 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) { - 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), - ) - } + if !isBenignProcessErr(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) @@ -2147,6 +2139,13 @@ func (l *logDebouncer) log(ctx context.Context, level slog.Level, msg string, fi l.messages[msg] = time.Now() } +func isBenignProcessErr(err error) bool { + return err != nil && + (xerrors.Is(err, os.ErrNotExist) || + xerrors.Is(err, os.ErrPermission) || + isNoSuchProcessErr(err)) +} + func isNoSuchProcessErr(err error) bool { return err != nil && strings.Contains(err.Error(), "no such process") } diff --git a/agent/agentproc/proc_unix.go b/agent/agentproc/proc_unix.go index 2eeb7d5a2253f..d35d9f1829722 100644 --- a/agent/agentproc/proc_unix.go +++ b/agent/agentproc/proc_unix.go @@ -45,8 +45,7 @@ func List(fs afero.Fs, syscaller Syscaller) ([]*Process, error) { cmdline, err := afero.ReadFile(fs, filepath.Join(defaultProcDir, entry, "cmdline")) if err != nil { - var errNo syscall.Errno - if xerrors.As(err, &errNo) && errNo == syscall.EPERM { + if isBenignError(err) { continue } return nil, xerrors.Errorf("read cmdline: %w", err) @@ -54,7 +53,7 @@ func List(fs afero.Fs, syscaller Syscaller) ([]*Process, error) { oomScore, err := afero.ReadFile(fs, filepath.Join(defaultProcDir, entry, "oom_score_adj")) if err != nil { - if xerrors.Is(err, os.ErrPermission) { + if isBenignError(err) { continue } @@ -124,3 +123,12 @@ func (p *Process) Cmd() string { func (p *Process) cmdLine() []string { return strings.Split(p.CmdLine, "\x00") } + +func isBenignError(err error) bool { + var errno syscall.Errno + if !xerrors.As(err, &errno) { + return false + } + + return errno == syscall.ESRCH || errno == syscall.EPERM || xerrors.Is(err, os.ErrNotExist) +}