Use clear error interface for process pidFn

If a given pidFn for a process collector can't determine the pid, this
had to be signaled with an invalid pid so far (pid <= 0). In order to
make the error interface clearer for users, this change introduces an
explicit error parameter.
This commit is contained in:
Tobias Schmidt 2015-02-02 14:53:06 -05:00
parent babc844741
commit 3cb16a9503
2 changed files with 24 additions and 12 deletions

View File

@ -5,7 +5,7 @@ import "github.com/prometheus/procfs"
type processCollector struct { type processCollector struct {
pid int pid int
collectFn func(chan<- Metric) collectFn func(chan<- Metric)
pidFn func() int pidFn func() (int, error)
cpuTotal Counter cpuTotal Counter
openFDs, maxFDs Gauge openFDs, maxFDs Gauge
vsize, rss Gauge vsize, rss Gauge
@ -16,7 +16,10 @@ type processCollector struct {
// process metrics including cpu, memory and file descriptor usage as well as // process metrics including cpu, memory and file descriptor usage as well as
// the process start time for the given process id under the given namespace. // the process start time for the given process id under the given namespace.
func NewProcessCollector(pid int, namespace string) *processCollector { func NewProcessCollector(pid int, namespace string) *processCollector {
return NewProcessCollectorPIDFn(func() int { return pid }, namespace) return NewProcessCollectorPIDFn(
func() (int, error) { return pid, nil },
namespace,
)
} }
// NewProcessCollectorPIDFn returns a collector which exports the current state // NewProcessCollectorPIDFn returns a collector which exports the current state
@ -25,7 +28,7 @@ func NewProcessCollector(pid int, namespace string) *processCollector {
// called on each collect and is used to determine the process to export // called on each collect and is used to determine the process to export
// metrics for. // metrics for.
func NewProcessCollectorPIDFn( func NewProcessCollectorPIDFn(
pidFn func() int, pidFn func() (int, error),
namespace string, namespace string,
) *processCollector { ) *processCollector {
c := processCollector{ c := processCollector{
@ -90,15 +93,15 @@ func (c *processCollector) Collect(ch chan<- Metric) {
func noopCollect(ch chan<- Metric) {} func noopCollect(ch chan<- Metric) {}
func (c *processCollector) procfsCollect(ch chan<- Metric) { func (c *processCollector) procfsCollect(ch chan<- Metric) {
p, err := procfs.NewProc(c.pidFn()) pid, err := c.pidFn()
if err != nil { if err != nil {
// Report collect errors for all metrics. c.reportCollectErrors(ch, err)
ch <- NewInvalidMetric(c.cpuTotal.Desc(), err) return
ch <- NewInvalidMetric(c.openFDs.Desc(), err) }
ch <- NewInvalidMetric(c.maxFDs.Desc(), err)
ch <- NewInvalidMetric(c.vsize.Desc(), err) p, err := procfs.NewProc(pid)
ch <- NewInvalidMetric(c.rss.Desc(), err) if err != nil {
ch <- NewInvalidMetric(c.startTime.Desc(), err) c.reportCollectErrors(ch, err)
return return
} }
@ -138,3 +141,12 @@ func (c *processCollector) procfsCollect(ch chan<- Metric) {
ch <- c.maxFDs ch <- c.maxFDs
} }
} }
func (c *processCollector) reportCollectErrors(ch chan<- Metric, err error) {
ch <- NewInvalidMetric(c.cpuTotal.Desc(), err)
ch <- NewInvalidMetric(c.openFDs.Desc(), err)
ch <- NewInvalidMetric(c.maxFDs.Desc(), err)
ch <- NewInvalidMetric(c.vsize.Desc(), err)
ch <- NewInvalidMetric(c.rss.Desc(), err)
ch <- NewInvalidMetric(c.startTime.Desc(), err)
}

View File

@ -19,7 +19,7 @@ func TestProcessCollector(t *testing.T) {
registry := newRegistry() registry := newRegistry()
registry.Register(NewProcessCollector(os.Getpid(), "")) registry.Register(NewProcessCollector(os.Getpid(), ""))
registry.Register(NewProcessCollectorPIDFn( registry.Register(NewProcessCollectorPIDFn(
func() int { return os.Getpid() }, "foobar")) func() (int, error) { return os.Getpid(), nil }, "foobar"))
s := httptest.NewServer(InstrumentHandler("prometheus", registry)) s := httptest.NewServer(InstrumentHandler("prometheus", registry))
defer s.Close() defer s.Close()