From f91bca427bb4492b255e4c41bc0876989980ff26 Mon Sep 17 00:00:00 2001 From: Julius Volz Date: Fri, 12 Aug 2016 01:21:00 +0200 Subject: [PATCH] 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 --- collector/filefd_linux.go | 35 +++++++++++++---------------------- 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/collector/filefd_linux.go b/collector/filefd_linux.go index 190f7800..180c6425 100644 --- a/collector/filefd_linux.go +++ b/collector/filefd_linux.go @@ -18,20 +18,19 @@ package collector import ( "bufio" "fmt" - "github.com/prometheus/client_golang/prometheus" "io" "os" "strconv" "strings" + + "github.com/prometheus/client_golang/prometheus" ) const ( fileFDStatSubsystem = "filefd" ) -type fileFDStatCollector struct { - metrics map[string]prometheus.Gauge -} +type fileFDStatCollector struct{} func init() { Factories[fileFDStatSubsystem] = NewFileFDStatCollector @@ -39,9 +38,7 @@ func init() { // NewFileFDStatCollector returns a new Collector exposing file-nr stats. func NewFileFDStatCollector() (Collector, error) { - return &fileFDStatCollector{ - metrics: map[string]prometheus.Gauge{}, - }, nil + return &fileFDStatCollector{}, nil } 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) } 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) if err != nil { 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 { - m.Collect(ch) - } - return err + return nil } func getFileFDStats(fileName string) (map[string]string, error) {