diff options
author | Julius Volz <julius@soundcloud.com> | 2016-08-12 01:21:00 +0200 |
---|---|---|
committer | Julius Volz <julius@soundcloud.com> | 2016-08-12 01:26:25 +0200 |
commit | f91bca427bb4492b255e4c41bc0876989980ff26 (patch) | |
tree | bf65524e90cb1f08558ac3f7e17ccbb37f6d7f1e /collector/filefd_linux.go | |
parent | 9128952454a1490d90f570466598ad98f22f2fad (diff) | |
download | prometheus_node_collector-f91bca427bb4492b255e4c41bc0876989980ff26.tar.bz2 prometheus_node_collector-f91bca427bb4492b255e4c41bc0876989980ff26.tar.xz prometheus_node_collector-f91bca427bb4492b255e4c41bc0876989980ff26.zip |
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
Diffstat (limited to 'collector/filefd_linux.go')
-rw-r--r-- | collector/filefd_linux.go | 35 |
1 files changed, 13 insertions, 22 deletions
diff --git a/collector/filefd_linux.go b/collector/filefd_linux.go index 190f780..180c642 100644 --- a/collector/filefd_linux.go +++ b/collector/filefd_linux.go | |||
@@ -18,20 +18,19 @@ package collector | |||
18 | import ( | 18 | import ( |
19 | "bufio" | 19 | "bufio" |
20 | "fmt" | 20 | "fmt" |
21 | "github.com/prometheus/client_golang/prometheus" | ||
22 | "io" | 21 | "io" |
23 | "os" | 22 | "os" |
24 | "strconv" | 23 | "strconv" |
25 | "strings" | 24 | "strings" |
25 | |||
26 | "github.com/prometheus/client_golang/prometheus" | ||
26 | ) | 27 | ) |
27 | 28 | ||
28 | const ( | 29 | const ( |
29 | fileFDStatSubsystem = "filefd" | 30 | fileFDStatSubsystem = "filefd" |
30 | ) | 31 | ) |
31 | 32 | ||
32 | type fileFDStatCollector struct { | 33 | type fileFDStatCollector struct{} |
33 | metrics map[string]prometheus.Gauge | ||
34 | } | ||
35 | 34 | ||
36 | func init() { | 35 | func init() { |
37 | Factories[fileFDStatSubsystem] = NewFileFDStatCollector | 36 | Factories[fileFDStatSubsystem] = NewFileFDStatCollector |
@@ -39,9 +38,7 @@ func init() { | |||
39 | 38 | ||
40 | // NewFileFDStatCollector returns a new Collector exposing file-nr stats. | 39 | // NewFileFDStatCollector returns a new Collector exposing file-nr stats. |
41 | func NewFileFDStatCollector() (Collector, error) { | 40 | func NewFileFDStatCollector() (Collector, error) { |
42 | return &fileFDStatCollector{ | 41 | return &fileFDStatCollector{}, nil |
43 | metrics: map[string]prometheus.Gauge{}, | ||
44 | }, nil | ||
45 | } | 42 | } |
46 | 43 | ||
47 | func (c *fileFDStatCollector) Update(ch chan<- prometheus.Metric) (err error) { | 44 | func (c *fileFDStatCollector) Update(ch chan<- prometheus.Metric) (err error) { |
@@ -50,26 +47,20 @@ func (c *fileFDStatCollector) Update(ch chan<- prometheus.Metric) (err error) { | |||
50 | return fmt.Errorf("couldn't get file-nr: %s", err) | 47 | return fmt.Errorf("couldn't get file-nr: %s", err) |
51 | } | 48 | } |
52 | for name, value := range fileFDStat { | 49 | for name, value := range fileFDStat { |
53 | if _, ok := c.metrics[name]; !ok { | ||
54 | c.metrics[name] = prometheus.NewGauge( | ||
55 | prometheus.GaugeOpts{ | ||
56 | Namespace: Namespace, | ||
57 | Subsystem: fileFDStatSubsystem, | ||
58 | Name: name, | ||
59 | Help: fmt.Sprintf("File descriptor statistics: %s.", name), | ||
60 | }, | ||
61 | ) | ||
62 | } | ||
63 | v, err := strconv.ParseFloat(value, 64) | 50 | v, err := strconv.ParseFloat(value, 64) |
64 | if err != nil { | 51 | if err != nil { |
65 | return fmt.Errorf("invalid value %s in file-nr: %s", value, err) | 52 | return fmt.Errorf("invalid value %s in file-nr: %s", value, err) |
66 | } | 53 | } |
67 | c.metrics[name].Set(v) | 54 | ch <- prometheus.MustNewConstMetric( |
68 | } | 55 | prometheus.NewDesc( |
69 | for _, m := range c.metrics { | 56 | prometheus.BuildFQName(Namespace, fileFDStatSubsystem, name), |
70 | m.Collect(ch) | 57 | fmt.Sprintf("File descriptor statistics: %s.", name), |
58 | nil, nil, | ||
59 | ), | ||
60 | prometheus.GaugeValue, v, | ||
61 | ) | ||
71 | } | 62 | } |
72 | return err | 63 | return nil |
73 | } | 64 | } |
74 | 65 | ||
75 | func getFileFDStats(fileName string) (map[string]string, error) { | 66 | func getFileFDStats(fileName string) (map[string]string, error) { |