Convert process collector to const metrics

This is needed to remove the deprecated Set method from the Counter.
This commit is contained in:
beorn7 2016-10-25 18:19:24 +02:00
parent 334af0119a
commit 6450fc55b1
1 changed files with 46 additions and 53 deletions

View File

@ -19,10 +19,10 @@ type processCollector struct {
pid int pid int
collectFn func(chan<- Metric) collectFn func(chan<- Metric)
pidFn func() (int, error) pidFn func() (int, error)
cpuTotal Counter cpuTotal *Desc
openFDs, maxFDs Gauge openFDs, maxFDs *Desc
vsize, rss Gauge vsize, rss *Desc
startTime Gauge startTime *Desc
} }
// NewProcessCollector returns a collector which exports the current state of // NewProcessCollector returns a collector which exports the current state of
@ -48,36 +48,36 @@ func NewProcessCollectorPIDFn(
pidFn: pidFn, pidFn: pidFn,
collectFn: func(chan<- Metric) {}, collectFn: func(chan<- Metric) {},
cpuTotal: NewCounter(CounterOpts{ cpuTotal: NewDesc(
Namespace: namespace, namespace+"_process_cpu_seconds_total",
Name: "process_cpu_seconds_total", "Total user and system CPU time spent in seconds.",
Help: "Total user and system CPU time spent in seconds.", nil, nil,
}), ),
openFDs: NewGauge(GaugeOpts{ openFDs: NewDesc(
Namespace: namespace, namespace+"_process_open_fds",
Name: "process_open_fds", "Number of open file descriptors.",
Help: "Number of open file descriptors.", nil, nil,
}), ),
maxFDs: NewGauge(GaugeOpts{ maxFDs: NewDesc(
Namespace: namespace, namespace+"_process_max_fds",
Name: "process_max_fds", "Maximum number of open file descriptors.",
Help: "Maximum number of open file descriptors.", nil, nil,
}), ),
vsize: NewGauge(GaugeOpts{ vsize: NewDesc(
Namespace: namespace, namespace+"_process_virtual_memory_bytes",
Name: "process_virtual_memory_bytes", "Virtual memory size in bytes.",
Help: "Virtual memory size in bytes.", nil, nil,
}), ),
rss: NewGauge(GaugeOpts{ rss: NewDesc(
Namespace: namespace, namespace+"_process_resident_memory_bytes",
Name: "process_resident_memory_bytes", "Resident memory size in bytes.",
Help: "Resident memory size in bytes.", nil, nil,
}), ),
startTime: NewGauge(GaugeOpts{ startTime: NewDesc(
Namespace: namespace, namespace+"_process_start_time_seconds",
Name: "process_start_time_seconds", "Start time of the process since unix epoch in seconds.",
Help: "Start time of the process since unix epoch in seconds.", nil, nil,
}), ),
} }
// Set up process metric collection if supported by the runtime. // Set up process metric collection if supported by the runtime.
@ -90,12 +90,12 @@ func NewProcessCollectorPIDFn(
// Describe returns all descriptions of the collector. // Describe returns all descriptions of the collector.
func (c *processCollector) Describe(ch chan<- *Desc) { func (c *processCollector) Describe(ch chan<- *Desc) {
ch <- c.cpuTotal.Desc() ch <- c.cpuTotal
ch <- c.openFDs.Desc() ch <- c.openFDs
ch <- c.maxFDs.Desc() ch <- c.maxFDs
ch <- c.vsize.Desc() ch <- c.vsize
ch <- c.rss.Desc() ch <- c.rss
ch <- c.startTime.Desc() ch <- c.startTime
} }
// Collect returns the current state of all metrics of the collector. // Collect returns the current state of all metrics of the collector.
@ -117,26 +117,19 @@ func (c *processCollector) processCollect(ch chan<- Metric) {
} }
if stat, err := p.NewStat(); err == nil { if stat, err := p.NewStat(); err == nil {
c.cpuTotal.Set(stat.CPUTime()) ch <- MustNewConstMetric(c.cpuTotal, CounterValue, stat.CPUTime())
ch <- c.cpuTotal ch <- MustNewConstMetric(c.vsize, GaugeValue, float64(stat.VirtualMemory()))
c.vsize.Set(float64(stat.VirtualMemory())) ch <- MustNewConstMetric(c.rss, GaugeValue, float64(stat.ResidentMemory()))
ch <- c.vsize
c.rss.Set(float64(stat.ResidentMemory()))
ch <- c.rss
if startTime, err := stat.StartTime(); err == nil { if startTime, err := stat.StartTime(); err == nil {
c.startTime.Set(startTime) ch <- MustNewConstMetric(c.startTime, GaugeValue, startTime)
ch <- c.startTime
} }
} }
if fds, err := p.FileDescriptorsLen(); err == nil { if fds, err := p.FileDescriptorsLen(); err == nil {
c.openFDs.Set(float64(fds)) ch <- MustNewConstMetric(c.openFDs, GaugeValue, float64(fds))
ch <- c.openFDs
} }
if limits, err := p.NewLimits(); err == nil { if limits, err := p.NewLimits(); err == nil {
c.maxFDs.Set(float64(limits.OpenFiles)) ch <- MustNewConstMetric(c.maxFDs, GaugeValue, float64(limits.OpenFiles))
ch <- c.maxFDs
} }
} }