Merge pull request #283 from prometheus/meminfo-constmetrics

Convert meminfo collector to use ConstMetrics
pull/286/head
Julius Volz 8 years ago committed by GitHub
commit c0b46391e2

@ -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)
c.metrics[k].Collect(ch)
} }
return err return nil
} }
func getMemInfo() (map[string]float64, error) { func getMemInfo() (map[string]float64, error) {

Loading…
Cancel
Save