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:
parent
9128952454
commit
eac396c637
|
@ -32,9 +32,7 @@ const (
|
|||
memInfoSubsystem = "memory"
|
||||
)
|
||||
|
||||
type meminfoCollector struct {
|
||||
metrics map[string]prometheus.Gauge
|
||||
}
|
||||
type meminfoCollector struct{}
|
||||
|
||||
func init() {
|
||||
Factories["meminfo"] = NewMeminfoCollector
|
||||
|
@ -43,9 +41,7 @@ func init() {
|
|||
// Takes a prometheus registry and returns a new Collector exposing
|
||||
// memory stats.
|
||||
func NewMeminfoCollector() (Collector, error) {
|
||||
return &meminfoCollector{
|
||||
metrics: map[string]prometheus.Gauge{},
|
||||
}, nil
|
||||
return &meminfoCollector{}, nil
|
||||
}
|
||||
|
||||
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)
|
||||
for k, v := range memInfo {
|
||||
if _, ok := c.metrics[k]; !ok {
|
||||
c.metrics[k] = prometheus.NewGauge(prometheus.GaugeOpts{
|
||||
Namespace: Namespace,
|
||||
Subsystem: memInfoSubsystem,
|
||||
Name: k,
|
||||
Help: fmt.Sprintf("Memory information field %s.", k),
|
||||
})
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
prometheus.NewDesc(
|
||||
prometheus.BuildFQName(Namespace, memInfoSubsystem, k),
|
||||
fmt.Sprintf("Memory information field %s.", k),
|
||||
nil, nil,
|
||||
),
|
||||
prometheus.GaugeValue, v,
|
||||
)
|
||||
}
|
||||
c.metrics[k].Set(v)
|
||||
c.metrics[k].Collect(ch)
|
||||
}
|
||||
return err
|
||||
return nil
|
||||
}
|
||||
|
||||
func getMemInfo() (map[string]float64, error) {
|
||||
|
|
Loading…
Reference in New Issue