Skip to content

Commit

Permalink
PR improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
belimawr committed Nov 3, 2023
1 parent 60212fd commit b1ecbfd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
24 changes: 14 additions & 10 deletions metric/system/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (procStats *Stats) GetSelf() (ProcState, error) {
func (procStats *Stats) pidIter(pid int, procMap ProcsMap, proclist []ProcState) (ProcsMap, []ProcState) {
status, saved, err := procStats.pidFill(pid, true)
if err != nil {
if !errors.Is(err, NotEnoughPrivilegesErr{}) {
if !errors.Is(err, NonFatalErr{}) {
procStats.logger.Debugf("Error fetching PID info for %d, skipping: %s", pid, err)
return procMap, proclist
}
Expand All @@ -184,21 +184,25 @@ func (procStats *Stats) pidIter(pid int, procMap ProcsMap, proclist []ProcState)
return procMap, proclist
}

// NotEnoughPrivilegesErr is returned when the current access
// rights are not enough to get some metrics.
// This error can be safely ignored and only means the function/method
// could not gather all metrics, however whatever has been gethered
// is still valid.
type NotEnoughPrivilegesErr struct {
// NonFatalErr is returned when there was an error
// collecting metrics, however the metrics already
// gathered and returned are still valid.
// This error can be safely ignored, this will result
// in having partial metrics for a process rather than
// no metrics at all.
//
// It was introduced to allow for partial metrics collection
// on privileged process on Windows.
type NonFatalErr struct {
Err error
}

func (c NotEnoughPrivilegesErr) Error() string {
func (c NonFatalErr) Error() string {
return "Not enough privileges to fetch information: " + c.Err.Error()
}

func (c NotEnoughPrivilegesErr) Is(other error) bool {
_, is := other.(NotEnoughPrivilegesErr)
func (c NonFatalErr) Is(other error) bool {
_, is := other.(NonFatalErr)
return is
}

Expand Down
4 changes: 2 additions & 2 deletions metric/system/process/process_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,12 @@ func FillPidMetrics(_ resolve.Resolver, pid int, state ProcState, _ func(string)
func FillMetricsRequiringMoreAccess(pid int, state ProcState) (ProcState, error) {
argList, err := getProcArgs(pid)
if err != nil {
return state, fmt.Errorf("error fetching process args: %w", NotEnoughPrivilegesErr{Err: err})
return state, fmt.Errorf("error fetching process args: %w", NonFatalErr{Err: err})
}
state.Args = argList

if numThreads, err := FetchNumThreads(pid); err != nil {
return state, fmt.Errorf("error fetching num threads: %w", NotEnoughPrivilegesErr{Err: err})
return state, fmt.Errorf("error fetching num threads: %w", NonFatalErr{Err: err})
} else {
state.NumThreads = opt.IntWith(numThreads)
}
Expand Down

0 comments on commit b1ecbfd

Please sign in to comment.