Convert BSD meminfo collector to use ConstMetrics

This suffers from the same concurrency bug as the netstat one:
https://github.com/prometheus/node_exporter/issues/280
pull/285/head
Julius Volz 2016-08-12 01:41:47 +02:00
parent 1001548e43
commit f0796683fe
1 changed files with 11 additions and 19 deletions

View File

@ -20,7 +20,6 @@ import (
"errors" "errors"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
) )
/* /*
@ -46,9 +45,7 @@ const (
memInfoSubsystem = "memory" memInfoSubsystem = "memory"
) )
type meminfoCollector struct { type meminfoCollector struct{}
metrics map[string]prometheus.Gauge
}
func init() { func init() {
Factories["meminfo"] = NewMeminfoCollector Factories["meminfo"] = NewMeminfoCollector
@ -57,9 +54,7 @@ func init() {
// Takes a prometheus registry and returns a new Collector exposing // Takes a prometheus registry and returns a new Collector exposing
// Memory stats. // Memory stats.
func NewMeminfoCollector() (Collector, error) { func NewMeminfoCollector() (Collector, error) {
return &meminfoCollector{ return &meminfoCollector{}, nil
metrics: map[string]prometheus.Gauge{},
}, nil
} }
func (c *meminfoCollector) Update(ch chan<- prometheus.Metric) (err error) { func (c *meminfoCollector) Update(ch chan<- prometheus.Metric) (err error) {
@ -92,19 +87,16 @@ func (c *meminfoCollector) Update(ch chan<- prometheus.Metric) (err error) {
} }
} }
log.Debugf("Set node_mem: %#v", pages)
for k, v := range pages { for k, v := range pages {
if _, ok := c.metrics[k]; !ok { ch <- prometheus.MustNewConstMetric(
c.metrics[k] = prometheus.NewGauge(prometheus.GaugeOpts{ prometheus.NewDesc(
Namespace: Namespace, prometheus.BuildFQName(Namespace, memInfoSubsystem, k),
Subsystem: memInfoSubsystem, k+" from sysctl()",
Name: k, nil, nil,
Help: k + " from sysctl()", ),
})
}
// Convert metrics to kB (same as Linux meminfo). // Convert metrics to kB (same as Linux meminfo).
c.metrics[k].Set(float64(v) * float64(size)) prometheus.UntypedValue, float64(v)*float64(size),
c.metrics[k].Collect(ch) )
} }
return err return err
} }