Skip to content

Commit

Permalink
[system/process] Log pid in all cases (#187)
Browse files Browse the repository at this point in the history
Return PID as well while returning errors. This helps with debugging.
  • Loading branch information
VihasMakwana authored Nov 12, 2024
1 parent 1548562 commit df681cf
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
6 changes: 3 additions & 3 deletions metric/system/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func (procStats *Stats) pidFill(pid int, filter bool) (ProcState, bool, error) {
// OS-specific entrypoint, get basic info so we can at least run matchProcess
status, err := GetInfoForPid(procStats.Hostfs, pid)
if err != nil {
return status, true, fmt.Errorf("GetInfoForPid: %w", err)
return status, true, fmt.Errorf("GetInfoForPid failed for pid %d: %w", pid, err)
}
if procStats.skipExtended {
return status, true, nil
Expand All @@ -250,7 +250,7 @@ func (procStats *Stats) pidFill(pid int, filter bool) (ProcState, bool, error) {
status, err = FillPidMetrics(procStats.Hostfs, pid, status, procStats.isWhitelistedEnvVar)
if err != nil {
if !errors.Is(err, NonFatalErr{}) {
return status, true, fmt.Errorf("FillPidMetrics: %w", err)
return status, true, fmt.Errorf("FillPidMetrics failed for PID %d: %w", pid, err)
}
wrappedErr = errors.Join(wrappedErr, fmt.Errorf("non-fatal error fetching PID metrics for %d, metrics are valid, but partial: %w", pid, err))
procStats.logger.Debugf(wrappedErr.Error())
Expand Down Expand Up @@ -282,7 +282,7 @@ func (procStats *Stats) pidFill(pid int, filter bool) (ProcState, bool, error) {

status, err = FillMetricsRequiringMoreAccess(pid, status)
if err != nil {
return status, true, fmt.Errorf("FillMetricsRequiringMoreAccess: %w", err)
return status, true, fmt.Errorf("FillMetricsRequiringMoreAccess failed for pid %d: %w", pid, err)
}

// Generate `status.Cmdline` here for compatibility because on Windows
Expand Down
14 changes: 7 additions & 7 deletions metric/system/process/process_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func FetchNumThreads(pid int) (int, error) {

currentProcessHandle, err := syscall.GetCurrentProcess()
if err != nil {
return 0, fmt.Errorf("GetCurrentProcess failed: %w", err)
return 0, fmt.Errorf("GetCurrentProcess failed for PID %d: %w", pid, err)
}
// The pseudo handle need not be closed when it is no longer
// needed, calling CloseHandle has no effect. Adding here to
Expand All @@ -114,7 +114,7 @@ func FetchNumThreads(pid int) (int, error) {
var snapshotHandle syscall.Handle
err = PssCaptureSnapshot(targetProcessHandle, PSSCaptureThreads, 0, &snapshotHandle)
if err != nil {
return 0, fmt.Errorf("PssCaptureSnapshot failed: %w", err)
return 0, fmt.Errorf("PssCaptureSnapshot failed for PID %d: %w", pid, err)
}

info := PssThreadInformation{}
Expand Down Expand Up @@ -189,28 +189,28 @@ func getProcArgs(pid int) ([]string, error) {
false,
uint32(pid))
if err != nil {
return nil, fmt.Errorf("OpenProcess failed: %w", err)
return nil, fmt.Errorf("OpenProcess failed for PID %d: %w", pid, err)
}
defer func() {
_ = syscall.CloseHandle(handle)
}()
pbi, err := windows.NtQueryProcessBasicInformation(handle)
if err != nil {
return nil, fmt.Errorf("NtQueryProcessBasicInformation failed: %w", err)
return nil, fmt.Errorf("NtQueryProcessBasicInformation failed for PID %d: %w", pid, err)
}

userProcParams, err := windows.GetUserProcessParams(handle, pbi)
if err != nil {
return nil, fmt.Errorf("GetUserProcessParams failed: %w", err)
return nil, fmt.Errorf("GetUserProcessParams failed for PID %d: %w", pid, err)
}
argsW, err := windows.ReadProcessUnicodeString(handle, &userProcParams.CommandLine)
if err != nil {
return nil, fmt.Errorf("ReadProcessUnicodeString failed: %w", err)
return nil, fmt.Errorf("ReadProcessUnicodeString failed for PID %d: %w", pid, err)
}

procList, err := windows.ByteSliceToStringSlice(argsW)
if err != nil {
return nil, fmt.Errorf("ByteSliceToStringSlice failed: %w", err)
return nil, fmt.Errorf("ByteSliceToStringSlice failed for PID %d: %w", pid, err)
}
return procList, nil
}
Expand Down

0 comments on commit df681cf

Please sign in to comment.