Convert meminfo collector to use ConstMetrics

This suffers from the same concurrency bug as the netstat one:
https://github.com/prometheus/node_exporter/issues/280
This commit is contained in:
Julius Volz 2016-08-12 01:30:15 +02:00
parent 9128952454
commit eac396c637
1 changed files with 11 additions and 17 deletions

View File

@ -32,9 +32,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
@ -43,9 +41,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) {
@ -55,18 +51,16 @@ func (c *meminfoCollector) Update(ch chan<- prometheus.Metric) (err error) {
} }
log.Debugf("Set node_mem: %#v", memInfo) log.Debugf("Set node_mem: %#v", memInfo)
for k, v := range memInfo { for k, v := range memInfo {
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, fmt.Sprintf("Memory information field %s.", k),
Name: k, nil, nil,
Help: fmt.Sprintf("Memory information field %s.", k), ),
}) prometheus.GaugeValue, v,
)
} }
c.metrics[k].Set(v) return nil
c.metrics[k].Collect(ch)
}
return err
} }
func getMemInfo() (map[string]float64, error) { func getMemInfo() (map[string]float64, error) {