forked from mirror/client_golang
Merge pull request #66 from prometheus/process-collector-explicit-error-interface
Use clear error interface for process pidFn
This commit is contained in:
commit
728064bb68
|
@ -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)
|
||||||
|
}
|
||||||
|
|
|
@ -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()
|
||||||
|
|
Loading…
Reference in New Issue