Convert filefd 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:21:00 +02:00
parent 9128952454
commit f91bca427b
1 changed files with 13 additions and 22 deletions

View File

@ -18,20 +18,19 @@ package collector
import ( import (
"bufio" "bufio"
"fmt" "fmt"
"github.com/prometheus/client_golang/prometheus"
"io" "io"
"os" "os"
"strconv" "strconv"
"strings" "strings"
"github.com/prometheus/client_golang/prometheus"
) )
const ( const (
fileFDStatSubsystem = "filefd" fileFDStatSubsystem = "filefd"
) )
type fileFDStatCollector struct { type fileFDStatCollector struct{}
metrics map[string]prometheus.Gauge
}
func init() { func init() {
Factories[fileFDStatSubsystem] = NewFileFDStatCollector Factories[fileFDStatSubsystem] = NewFileFDStatCollector
@ -39,9 +38,7 @@ func init() {
// NewFileFDStatCollector returns a new Collector exposing file-nr stats. // NewFileFDStatCollector returns a new Collector exposing file-nr stats.
func NewFileFDStatCollector() (Collector, error) { func NewFileFDStatCollector() (Collector, error) {
return &fileFDStatCollector{ return &fileFDStatCollector{}, nil
metrics: map[string]prometheus.Gauge{},
}, nil
} }
func (c *fileFDStatCollector) Update(ch chan<- prometheus.Metric) (err error) { func (c *fileFDStatCollector) Update(ch chan<- prometheus.Metric) (err error) {
@ -50,26 +47,20 @@ func (c *fileFDStatCollector) Update(ch chan<- prometheus.Metric) (err error) {
return fmt.Errorf("couldn't get file-nr: %s", err) return fmt.Errorf("couldn't get file-nr: %s", err)
} }
for name, value := range fileFDStat { for name, value := range fileFDStat {
if _, ok := c.metrics[name]; !ok {
c.metrics[name] = prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: fileFDStatSubsystem,
Name: name,
Help: fmt.Sprintf("File descriptor statistics: %s.", name),
},
)
}
v, err := strconv.ParseFloat(value, 64) v, err := strconv.ParseFloat(value, 64)
if err != nil { if err != nil {
return fmt.Errorf("invalid value %s in file-nr: %s", value, err) return fmt.Errorf("invalid value %s in file-nr: %s", value, err)
} }
c.metrics[name].Set(v) ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(Namespace, fileFDStatSubsystem, name),
fmt.Sprintf("File descriptor statistics: %s.", name),
nil, nil,
),
prometheus.GaugeValue, v,
)
} }
for _, m := range c.metrics { return nil
m.Collect(ch)
}
return err
} }
func getFileFDStats(fileName string) (map[string]string, error) { func getFileFDStats(fileName string) (map[string]string, error) {