PR feedback

Signed-off-by: Dustin Hooten <dustinhooten@gmail.com>
pull/1948/head
Dustin Hooten 4 years ago committed by Johannes 'fish' Ziemke
parent b7626ecdbf
commit eecfbcd610

@ -112,12 +112,12 @@ func (c *processCollector) getAllocatedThreads() (int, map[string]int32, int, er
procStates := make(map[string]int32) procStates := make(map[string]int32)
for _, pid := range p { for _, pid := range p {
stat, err := pid.Stat() stat, err := pid.Stat()
// PIDs can vanish between getting the list and getting stats.
if errors.Is(err, os.ErrNotExist) || errorContains(err, syscall.ESRCH) {
level.Debug(c.logger).Log("msg", "file not found when retrieving stats for pid", "pid", pid, "err", err)
continue
}
if err != nil { if err != nil {
// PIDs can vanish between getting the list and getting stats.
if errors.Is(err, os.ErrNotExist) || strings.Contains(err.Error(), syscall.ESRCH.Error()) {
level.Debug(c.logger).Log("msg", "file not found when retrieving stats for pid", "pid", pid, "err", err)
continue
}
level.Debug(c.logger).Log("msg", "error reading stat for pid", "pid", pid, "err", err) level.Debug(c.logger).Log("msg", "error reading stat for pid", "pid", pid, "err", err)
return 0, nil, 0, err return 0, nil, 0, err
} }
@ -127,7 +127,3 @@ func (c *processCollector) getAllocatedThreads() (int, map[string]int32, int, er
} }
return pids, procStates, thread, nil return pids, procStates, thread, nil
} }
func errorContains(err, target error) bool {
return err != nil && target != nil && strings.Contains(err.Error(), target.Error())
}

@ -16,8 +16,6 @@
package collector package collector
import ( import (
"errors"
"syscall"
"testing" "testing"
"github.com/go-kit/kit/log" "github.com/go-kit/kit/log"
@ -54,28 +52,3 @@ func TestReadProcessStatus(t *testing.T) {
t.Fatalf("Total running pids cannot be greater than %d or equals to 0", maxPid) t.Fatalf("Total running pids cannot be greater than %d or equals to 0", maxPid)
} }
} }
func Test_errorContains(t *testing.T) {
testCases := []struct {
err error
target error
expected bool
}{
{err: nil, target: nil, expected: false},
{err: errors.New("e"), target: nil, expected: false},
{err: nil, target: errors.New("e"), expected: false},
{err: errors.New("abc"), target: errors.New("def"), expected: false},
{err: errors.New("abc"), target: errors.New("bc"), expected: true},
{err: errors.New("read /proc/2054/stat: no such process"), target: syscall.ESRCH, expected: true},
}
for _, tc := range testCases {
actual := errorContains(tc.err, tc.target)
if actual != tc.expected {
negation := " not"
if tc.expected {
negation = ""
}
t.Fatalf("Expected \"%v\"%s to contain \"%v\", got %v", tc.err, negation, tc.target, actual)
}
}
}

Loading…
Cancel
Save