Skip to content

Commit

Permalink
[process] - Add a boolean to detect partial matches (#199)
Browse files Browse the repository at this point in the history
This PR follows up on
#195 and
adds a new boolean to indicate if a given process state is partial.
This is useful while returning errors to the caller.
  • Loading branch information
VihasMakwana authored Dec 24, 2024
1 parent fa3caa1 commit b9fc63c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
11 changes: 9 additions & 2 deletions metric/system/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ func (procStats *Stats) Get() ([]mapstr.M, []mapstr.M, error) {
procs = append(procs, proc)
rootEvents = append(rootEvents, rootMap)
}
if len(failedPIDs) > 0 {
if wrappedErr != nil && len(failedPIDs) > 0 {
procStats.logger.Debugf("error fetching process metrics: %v", wrappedErr)
return procs, rootEvents, NonFatalErr{Err: fmt.Errorf(errFetchingPIDs, len(failedPIDs))}
}
return procs, rootEvents, nil
return procs, rootEvents, toNonFatal(wrappedErr)
}

// GetOne fetches process data for a given PID if its name matches the regexes provided from the host.
Expand Down Expand Up @@ -224,6 +224,10 @@ func (procStats *Stats) pidIter(pid int, procMap ProcsMap, proclist []ProcState)
procStats.logger.Debugf("Process name does not match the provided regex; PID=%d; name=%s", pid, status.Name)
return procMap, proclist, nonFatalErr
}
// there was some non-fatal error and given state is partial
if nonFatalErr != nil {
status.Partial = true
}
procMap[pid] = status
proclist = append(proclist, status)

Expand Down Expand Up @@ -422,12 +426,15 @@ func (procStats *Stats) isWhitelistedEnvVar(varName string) bool {
}

func extractFailedPIDs(procMap ProcsMap) []int {
// calculate the total amount of partial/failed PIDs
list := make([]int, 0)
for pid, state := range procMap {
if state.Failed {
list = append(list, pid)
// delete the failed state so we don't return the state to caller
delete(procMap, pid)
} else if state.Partial {
list = append(list, pid)
}
}
return list
Expand Down
4 changes: 3 additions & 1 deletion metric/system/process/process_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ type ProcState struct {
// meta
SampleTime time.Time `struct:"-,omitempty"`

// boolean to indicate that given PID has failed due to some error.
// boolean to indicate that given PID has completeley failed due to some error.
Failed bool `struct:"-,omitempty"`
// boolean to indicate that given state is partially filled.
Partial bool `struct:"-,omitempty"`
}

// ProcCPUInfo is the main struct for CPU metrics
Expand Down

0 comments on commit b9fc63c

Please sign in to comment.