diff options
author | Johannes 'fish' Ziemke <github@freigeist.org> | 2016-12-28 15:21:31 +0100 |
---|---|---|
committer | Johannes 'fish' Ziemke <github@freigeist.org> | 2017-01-03 14:11:10 +0100 |
commit | 8e50b80d1285be8334066aca7a9f7ec26221f7c1 (patch) | |
tree | 71ca4eb02d97823134567ef8956c5b1e5124afde /collector/diskstats_linux.go | |
parent | c53bc168fe759d84ab8a031f2eae0777b221506d (diff) | |
download | prometheus_node_collector-8e50b80d1285be8334066aca7a9f7ec26221f7c1.tar.bz2 prometheus_node_collector-8e50b80d1285be8334066aca7a9f7ec26221f7c1.tar.xz prometheus_node_collector-8e50b80d1285be8334066aca7a9f7ec26221f7c1.zip |
Convert remaining collectors to use ConstMetrics
Diffstat (limited to 'collector/diskstats_linux.go')
-rw-r--r-- | collector/diskstats_linux.go | 245 |
1 files changed, 111 insertions, 134 deletions
diff --git a/collector/diskstats_linux.go b/collector/diskstats_linux.go index e7b24fd..8523e23 100644 --- a/collector/diskstats_linux.go +++ b/collector/diskstats_linux.go | |||
@@ -40,7 +40,7 @@ var ( | |||
40 | 40 | ||
41 | type diskstatsCollector struct { | 41 | type diskstatsCollector struct { |
42 | ignoredDevicesPattern *regexp.Regexp | 42 | ignoredDevicesPattern *regexp.Regexp |
43 | metrics []prometheus.Collector | 43 | descs []typedDesc |
44 | } | 44 | } |
45 | 45 | ||
46 | func init() { | 46 | func init() { |
@@ -55,129 +55,116 @@ func NewDiskstatsCollector() (Collector, error) { | |||
55 | return &diskstatsCollector{ | 55 | return &diskstatsCollector{ |
56 | ignoredDevicesPattern: regexp.MustCompile(*ignoredDevices), | 56 | ignoredDevicesPattern: regexp.MustCompile(*ignoredDevices), |
57 | // Docs from https://www.kernel.org/doc/Documentation/iostats.txt | 57 | // Docs from https://www.kernel.org/doc/Documentation/iostats.txt |
58 | metrics: []prometheus.Collector{ | 58 | descs: []typedDesc{ |
59 | prometheus.NewCounterVec( | 59 | { |
60 | prometheus.CounterOpts{ | 60 | desc: prometheus.NewDesc( |
61 | Namespace: Namespace, | 61 | prometheus.BuildFQName(Namespace, diskSubsystem, "reads_completed"), |
62 | Subsystem: diskSubsystem, | 62 | "The total number of reads completed successfully.", |
63 | Name: "reads_completed", | 63 | diskLabelNames, |
64 | Help: "The total number of reads completed successfully.", | 64 | nil, |
65 | }, | 65 | ), valueType: prometheus.CounterValue, |
66 | diskLabelNames, | 66 | }, |
67 | ), | 67 | { |
68 | prometheus.NewCounterVec( | 68 | desc: prometheus.NewDesc( |
69 | prometheus.CounterOpts{ | 69 | prometheus.BuildFQName(Namespace, diskSubsystem, "reads_merged"), |
70 | Namespace: Namespace, | 70 | "The total number of reads merged. See https://www.kernel.org/doc/Documentation/iostats.txt.", |
71 | Subsystem: diskSubsystem, | 71 | diskLabelNames, |
72 | Name: "reads_merged", | 72 | nil, |
73 | Help: "The number of reads merged. See https://www.kernel.org/doc/Documentation/iostats.txt.", | 73 | ), valueType: prometheus.CounterValue, |
74 | }, | 74 | }, |
75 | diskLabelNames, | 75 | { |
76 | ), | 76 | desc: prometheus.NewDesc( |
77 | prometheus.NewCounterVec( | 77 | prometheus.BuildFQName(Namespace, diskSubsystem, "sectors_read"), |
78 | prometheus.CounterOpts{ | 78 | "The total number of sectors read successfully.", |
79 | Namespace: Namespace, | 79 | diskLabelNames, |
80 | Subsystem: diskSubsystem, | 80 | nil, |
81 | Name: "sectors_read", | 81 | ), valueType: prometheus.CounterValue, |
82 | Help: "The total number of sectors read successfully.", | 82 | }, |
83 | }, | 83 | { |
84 | diskLabelNames, | 84 | desc: prometheus.NewDesc( |
85 | ), | 85 | prometheus.BuildFQName(Namespace, diskSubsystem, "read_time_ms"), |
86 | prometheus.NewCounterVec( | 86 | "The total number of milliseconds spent by all reads.", |
87 | prometheus.CounterOpts{ | 87 | diskLabelNames, |
88 | Namespace: Namespace, | 88 | nil, |
89 | Subsystem: diskSubsystem, | 89 | ), valueType: prometheus.CounterValue, |
90 | Name: "read_time_ms", | 90 | }, |
91 | Help: "The total number of milliseconds spent by all reads.", | 91 | { |
92 | }, | 92 | desc: prometheus.NewDesc( |
93 | diskLabelNames, | 93 | prometheus.BuildFQName(Namespace, diskSubsystem, "writes_completed"), |
94 | ), | 94 | "The total number of writes completed successfully.", |
95 | prometheus.NewCounterVec( | 95 | diskLabelNames, |
96 | prometheus.CounterOpts{ | 96 | nil, |
97 | Namespace: Namespace, | 97 | ), valueType: prometheus.CounterValue, |
98 | Subsystem: diskSubsystem, | 98 | }, |
99 | Name: "writes_completed", | 99 | { |
100 | Help: "The total number of writes completed successfully.", | 100 | desc: prometheus.NewDesc( |
101 | }, | 101 | prometheus.BuildFQName(Namespace, diskSubsystem, "writes_merged"), |
102 | diskLabelNames, | 102 | "The number of writes merged. See https://www.kernel.org/doc/Documentation/iostats.txt.", |
103 | ), | 103 | diskLabelNames, |
104 | prometheus.NewCounterVec( | 104 | nil, |
105 | prometheus.CounterOpts{ | 105 | ), valueType: prometheus.CounterValue, |
106 | Namespace: Namespace, | 106 | }, |
107 | Subsystem: diskSubsystem, | 107 | { |
108 | Name: "writes_merged", | 108 | desc: prometheus.NewDesc( |
109 | Help: "The number of writes merged. See https://www.kernel.org/doc/Documentation/iostats.txt.", | 109 | prometheus.BuildFQName(Namespace, diskSubsystem, "sectors_written"), |
110 | }, | 110 | "The total number of sectors written successfully.", |
111 | diskLabelNames, | 111 | diskLabelNames, |
112 | ), | 112 | nil, |
113 | prometheus.NewCounterVec( | 113 | ), valueType: prometheus.CounterValue, |
114 | prometheus.CounterOpts{ | 114 | }, |
115 | Namespace: Namespace, | 115 | { |
116 | Subsystem: diskSubsystem, | 116 | desc: prometheus.NewDesc( |
117 | Name: "sectors_written", | 117 | prometheus.BuildFQName(Namespace, diskSubsystem, "write_time_ms"), |
118 | Help: "The total number of sectors written successfully.", | 118 | "This is the total number of milliseconds spent by all writes.", |
119 | }, | 119 | diskLabelNames, |
120 | diskLabelNames, | 120 | nil, |
121 | ), | 121 | ), valueType: prometheus.CounterValue, |
122 | prometheus.NewCounterVec( | 122 | }, |
123 | prometheus.CounterOpts{ | 123 | { |
124 | Namespace: Namespace, | 124 | desc: prometheus.NewDesc( |
125 | Subsystem: diskSubsystem, | 125 | prometheus.BuildFQName(Namespace, diskSubsystem, "io_now"), |
126 | Name: "write_time_ms", | 126 | "The number of I/Os currently in progress.", |
127 | Help: "This is the total number of milliseconds spent by all writes.", | 127 | diskLabelNames, |
128 | }, | 128 | nil, |
129 | diskLabelNames, | 129 | ), valueType: prometheus.GaugeValue, |
130 | ), | 130 | }, |
131 | prometheus.NewGaugeVec( | 131 | { |
132 | prometheus.GaugeOpts{ | 132 | desc: prometheus.NewDesc( |
133 | Namespace: Namespace, | 133 | prometheus.BuildFQName(Namespace, diskSubsystem, "io_time_ms"), |
134 | Subsystem: diskSubsystem, | 134 | "Total Milliseconds spent doing I/Os.", |
135 | Name: "io_now", | 135 | diskLabelNames, |
136 | Help: "The number of I/Os currently in progress.", | 136 | nil, |
137 | }, | 137 | ), valueType: prometheus.CounterValue, |
138 | diskLabelNames, | 138 | }, |
139 | ), | 139 | { |
140 | prometheus.NewCounterVec( | 140 | desc: prometheus.NewDesc( |
141 | prometheus.CounterOpts{ | 141 | prometheus.BuildFQName(Namespace, diskSubsystem, "io_time_weighted"), |
142 | Namespace: Namespace, | 142 | "The weighted # of milliseconds spent doing I/Os. See https://www.kernel.org/doc/Documentation/iostats.txt.", |
143 | Subsystem: diskSubsystem, | 143 | diskLabelNames, |
144 | Name: "io_time_ms", | 144 | nil, |
145 | Help: "Milliseconds spent doing I/Os.", | 145 | ), valueType: prometheus.CounterValue, |
146 | }, | 146 | }, |
147 | diskLabelNames, | 147 | { |
148 | ), | 148 | desc: prometheus.NewDesc( |
149 | prometheus.NewCounterVec( | 149 | prometheus.BuildFQName(Namespace, diskSubsystem, "bytes_read"), |
150 | prometheus.CounterOpts{ | 150 | "The total number of bytes read successfully.", |
151 | Namespace: Namespace, | 151 | diskLabelNames, |
152 | Subsystem: diskSubsystem, | 152 | nil, |
153 | Name: "io_time_weighted", | 153 | ), valueType: prometheus.CounterValue, |
154 | Help: "The weighted # of milliseconds spent doing I/Os. See https://www.kernel.org/doc/Documentation/iostats.txt.", | 154 | }, |
155 | }, | 155 | { |
156 | diskLabelNames, | 156 | desc: prometheus.NewDesc( |
157 | ), | 157 | prometheus.BuildFQName(Namespace, diskSubsystem, "bytes_written"), |
158 | prometheus.NewCounterVec( | 158 | "The total number of bytes written successfully.", |
159 | prometheus.CounterOpts{ | 159 | diskLabelNames, |
160 | Namespace: Namespace, | 160 | nil, |
161 | Subsystem: diskSubsystem, | 161 | ), valueType: prometheus.CounterValue, |
162 | Name: "bytes_read", | 162 | }, |
163 | Help: "The total number of bytes read successfully.", | ||
164 | }, | ||
165 | diskLabelNames, | ||
166 | ), | ||
167 | prometheus.NewCounterVec( | ||
168 | prometheus.CounterOpts{ | ||
169 | Namespace: Namespace, | ||
170 | Subsystem: diskSubsystem, | ||
171 | Name: "bytes_written", | ||
172 | Help: "The total number of bytes written successfully.", | ||
173 | }, | ||
174 | diskLabelNames, | ||
175 | ), | ||
176 | }, | 163 | }, |
177 | }, nil | 164 | }, nil |
178 | } | 165 | } |
179 | 166 | ||
180 | func (c *diskstatsCollector) Update(ch chan<- prometheus.Metric) (err error) { | 167 | func (c *diskstatsCollector) Update(ch chan<- prometheus.Metric) error { |
181 | procDiskStats := procFilePath("diskstats") | 168 | procDiskStats := procFilePath("diskstats") |
182 | diskStats, err := getDiskStats() | 169 | diskStats, err := getDiskStats() |
183 | if err != nil { | 170 | if err != nil { |
@@ -190,29 +177,19 @@ func (c *diskstatsCollector) Update(ch chan<- prometheus.Metric) (err error) { | |||
190 | continue | 177 | continue |
191 | } | 178 | } |
192 | 179 | ||
193 | if len(stats) != len(c.metrics) { | 180 | if len(stats) != len(c.descs) { |
194 | return fmt.Errorf("invalid line for %s for %s", procDiskStats, dev) | 181 | return fmt.Errorf("invalid line for %s for %s", procDiskStats, dev) |
195 | } | 182 | } |
196 | 183 | ||
197 | for k, value := range stats { | 184 | for i, value := range stats { |
198 | v, err := strconv.ParseFloat(value, 64) | 185 | v, err := strconv.ParseFloat(value, 64) |
199 | if err != nil { | 186 | if err != nil { |
200 | return fmt.Errorf("invalid value %s in diskstats: %s", value, err) | 187 | return fmt.Errorf("invalid value %s in diskstats: %s", value, err) |
201 | } | 188 | } |
202 | 189 | ch <- c.descs[i].mustNewConstMetric(v, dev) | |
203 | if counter, ok := c.metrics[k].(*prometheus.CounterVec); ok { | ||
204 | counter.WithLabelValues(dev).Set(v) | ||
205 | } else if gauge, ok := c.metrics[k].(*prometheus.GaugeVec); ok { | ||
206 | gauge.WithLabelValues(dev).Set(v) | ||
207 | } else { | ||
208 | return fmt.Errorf("unexpected collector %d", k) | ||
209 | } | ||
210 | } | 190 | } |
211 | } | 191 | } |
212 | for _, c := range c.metrics { | 192 | return nil |
213 | c.Collect(ch) | ||
214 | } | ||
215 | return err | ||
216 | } | 193 | } |
217 | 194 | ||
218 | func getDiskStats() (map[string]map[int]string, error) { | 195 | func getDiskStats() (map[string]map[int]string, error) { |