forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prometheus.go
366 lines (331 loc) · 13.1 KB
/
prometheus.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// Copyright 2014 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package metrics
import (
"fmt"
"time"
"github.com/golang/glog"
info "github.com/google/cadvisor/info/v1"
"github.com/google/cadvisor/manager"
"github.com/prometheus/client_golang/prometheus"
)
type prometheusMetric struct {
valueType prometheus.ValueType
value float64
labels []string
}
// PrometheusCollector implements prometheus.Collector.
type PrometheusCollector struct {
manager manager.Manager
errors prometheus.Gauge
lastSeen *prometheus.Desc
cpuUsageUserSeconds *prometheus.Desc
cpuUsageSystemSeconds *prometheus.Desc
cpuUsageSecondsPerCPU *prometheus.Desc
memoryUsageBytes *prometheus.Desc
memoryWorkingSet *prometheus.Desc
memoryFailures *prometheus.Desc
fsLimit *prometheus.Desc
fsUsage *prometheus.Desc
fsReads *prometheus.Desc
fsReadsSectors *prometheus.Desc
fsReadsMerged *prometheus.Desc
fsReadTime *prometheus.Desc
fsWrites *prometheus.Desc
fsWritesSectors *prometheus.Desc
fsWritesMerged *prometheus.Desc
fsWriteTime *prometheus.Desc
fsIoInProgress *prometheus.Desc
fsIoTime *prometheus.Desc
fsWeightedIoTime *prometheus.Desc
networkRxBytes *prometheus.Desc
networkRxPackets *prometheus.Desc
networkRxErrors *prometheus.Desc
networkRxDropped *prometheus.Desc
networkTxBytes *prometheus.Desc
networkTxPackets *prometheus.Desc
networkTxErrors *prometheus.Desc
networkTxDropped *prometheus.Desc
tasks *prometheus.Desc
descs []*prometheus.Desc
}
// NewPrometheusCollector returns a new PrometheusCollector.
func NewPrometheusCollector(manager manager.Manager) *PrometheusCollector {
c := &PrometheusCollector{
manager: manager,
errors: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "container",
Name: "scrape_error",
Help: "1 if there was an error while getting container metrics, 0 otherwise",
}),
lastSeen: prometheus.NewDesc(
"container_last_seen",
"Last time a container was seen by the exporter",
[]string{"name", "id"},
nil),
cpuUsageUserSeconds: prometheus.NewDesc(
"container_cpu_user_seconds_total",
"Cumulative user cpu time consumed in seconds.",
[]string{"name", "id"},
nil),
cpuUsageSystemSeconds: prometheus.NewDesc(
"container_cpu_system_seconds_total",
"Cumulative system cpu time consumed in seconds.",
[]string{"name", "id"},
nil),
cpuUsageSecondsPerCPU: prometheus.NewDesc(
"container_cpu_usage_seconds_total",
"Cumulative cpu time consumed per cpu in seconds.",
[]string{"name", "id", "cpu"},
nil),
memoryUsageBytes: prometheus.NewDesc(
"container_memory_usage_bytes",
"Current memory usage in bytes.",
[]string{"name", "id"},
nil),
memoryWorkingSet: prometheus.NewDesc(
"container_memory_working_set_bytes",
"Current working set in bytes.",
[]string{"name", "id"},
nil),
memoryFailures: prometheus.NewDesc(
"container_memory_failures_total",
"Cumulative count of memory allocation failures.",
[]string{"type", "scope", "name", "id"},
nil),
fsLimit: prometheus.NewDesc(
"container_fs_limit_bytes",
"Number of bytes that can be consumed by the container on this filesystem.",
[]string{"name", "id", "device"},
nil),
fsUsage: prometheus.NewDesc(
"container_fs_usage_bytes",
"Number of bytes that are consumed by the container on this filesystem.",
[]string{"name", "id", "device"},
nil),
fsReads: prometheus.NewDesc(
"container_fs_reads_total",
"Cumulative count of reads completed",
[]string{"name", "id", "device"},
nil),
fsReadsSectors: prometheus.NewDesc(
"container_fs_sector_reads_total",
"Cumulative count of sector reads completed",
[]string{"name", "id", "device"},
nil),
fsReadsMerged: prometheus.NewDesc(
"container_fs_reads_merged_total",
"Cumulative count of reads merged",
[]string{"name", "id", "device"},
nil),
fsReadTime: prometheus.NewDesc(
"container_fs_read_seconds_total",
"Cumulative count of seconds spent reading",
[]string{"name", "id", "device"},
nil),
fsWrites: prometheus.NewDesc(
"container_fs_writes_total",
"Cumulative count of writes completed",
[]string{"name", "id", "device"},
nil),
fsWritesSectors: prometheus.NewDesc(
"container_fs_sector_writes_total",
"Cumulative count of sector writes completed",
[]string{"name", "id", "device"},
nil),
fsWritesMerged: prometheus.NewDesc(
"container_fs_writes_merged_total",
"Cumulative count of writes merged",
[]string{"name", "id", "device"},
nil),
fsWriteTime: prometheus.NewDesc(
"container_fs_write_seconds_total",
"Cumulative count of seconds spent writing",
[]string{"name", "id", "device"},
nil),
fsIoInProgress: prometheus.NewDesc(
"container_fs_io_current",
"Number of I/Os currently in progress",
[]string{"name", "id", "device"},
nil),
fsIoTime: prometheus.NewDesc(
"container_fs_io_time_seconds_total",
"Cumulative count of seconds spent doing I/Os",
[]string{"name", "id", "device"},
nil),
fsWeightedIoTime: prometheus.NewDesc(
"container_fs_io_time_weighted_seconds_total",
"Cumulative weighted I/O time in seconds",
[]string{"name", "id", "device"},
nil),
networkRxBytes: prometheus.NewDesc(
"container_network_receive_bytes_total",
"Cumulative count of bytes received",
[]string{"name", "id"},
nil),
networkRxPackets: prometheus.NewDesc(
"container_network_receive_packets_total",
"Cumulative count of packets received",
[]string{"name", "id"},
nil),
networkRxDropped: prometheus.NewDesc(
"container_network_receive_packets_dropped_total",
"Cumulative count of packets dropped while receiving",
[]string{"name", "id"},
nil),
networkRxErrors: prometheus.NewDesc(
"container_network_receive_errors_total",
"Cumulative count of errors encountered while receiving",
[]string{"name", "id"},
nil),
networkTxBytes: prometheus.NewDesc(
"container_network_transmit_bytes_total",
"Cumulative count of bytes transmitted",
[]string{"name", "id"},
nil),
networkTxPackets: prometheus.NewDesc(
"container_network_transmit_packets_total",
"Cumulative count of packets transmitted",
[]string{"name", "id"},
nil),
networkTxDropped: prometheus.NewDesc(
"container_network_transmit_packets_dropped_total",
"Cumulative count of packets dropped while transmitting",
[]string{"name", "id"},
nil),
networkTxErrors: prometheus.NewDesc(
"container_network_transmit_errors_total",
"Cumulative count of errors encountered while transmitting",
[]string{"name", "id"},
nil),
tasks: prometheus.NewDesc(
"container_tasks_state",
"Number of tasks in given state",
[]string{"state", "name", "id"},
nil),
}
c.descs = []*prometheus.Desc{
c.lastSeen,
c.cpuUsageUserSeconds,
c.cpuUsageSystemSeconds,
c.memoryUsageBytes,
c.memoryWorkingSet,
c.memoryFailures,
c.fsLimit,
c.fsUsage,
c.fsReads,
c.fsReadsSectors,
c.fsReadsMerged,
c.fsReadTime,
c.fsWrites,
c.fsWritesSectors,
c.fsWritesMerged,
c.fsWriteTime,
c.fsIoInProgress,
c.fsIoTime,
c.fsWeightedIoTime,
c.networkRxBytes,
c.networkRxPackets,
c.networkRxErrors,
c.networkRxDropped,
c.networkTxBytes,
c.networkTxPackets,
c.networkTxErrors,
c.networkTxDropped,
c.tasks,
}
return c
}
// Describe describes all the metrics ever exported by cadvisor. It
// implements prometheus.PrometheusCollector.
func (c *PrometheusCollector) Describe(ch chan<- *prometheus.Desc) {
c.errors.Describe(ch)
for _, d := range c.descs {
ch <- d
}
}
// Collect fetches the stats from all containers and delivers them as
// Prometheus metrics. It implements prometheus.PrometheusCollector.
func (c *PrometheusCollector) Collect(ch chan<- prometheus.Metric) {
containers, err := c.manager.SubcontainersInfo("/", &info.ContainerInfoRequest{NumStats: 1})
if err != nil {
c.errors.Set(1)
glog.Warning("Couldn't get containers: %s", err)
return
}
for _, container := range containers {
id := container.Name
name := id
if len(container.Aliases) > 0 {
name = container.Aliases[0]
}
stats := container.Stats[0]
for desc, metrics := range map[*prometheus.Desc][]prometheusMetric{
c.cpuUsageUserSeconds: {{valueType: prometheus.CounterValue, value: float64(stats.Cpu.Usage.User) / float64(time.Second)}},
c.cpuUsageSystemSeconds: {{valueType: prometheus.CounterValue, value: float64(stats.Cpu.Usage.System) / float64(time.Second)}},
c.memoryFailures: {
{valueType: prometheus.CounterValue, labels: []string{"pgfault", "container"}, value: float64(stats.Memory.ContainerData.Pgfault)},
{valueType: prometheus.CounterValue, labels: []string{"pgmajfault", "container"}, value: float64(stats.Memory.ContainerData.Pgmajfault)},
{valueType: prometheus.CounterValue, labels: []string{"pgfault", "hierarchy"}, value: float64(stats.Memory.HierarchicalData.Pgfault)},
{valueType: prometheus.CounterValue, labels: []string{"pgmajfault", "hierarchy"}, value: float64(stats.Memory.HierarchicalData.Pgmajfault)},
},
c.tasks: {
{valueType: prometheus.GaugeValue, labels: []string{"sleeping"}, value: float64(stats.TaskStats.NrSleeping)},
{valueType: prometheus.GaugeValue, labels: []string{"running"}, value: float64(stats.TaskStats.NrRunning)},
{valueType: prometheus.GaugeValue, labels: []string{"stopped"}, value: float64(stats.TaskStats.NrStopped)},
{valueType: prometheus.GaugeValue, labels: []string{"uninterruptible"}, value: float64(stats.TaskStats.NrUninterruptible)},
{valueType: prometheus.GaugeValue, labels: []string{"iowaiting"}, value: float64(stats.TaskStats.NrIoWait)},
},
c.lastSeen: {{valueType: prometheus.GaugeValue, value: float64(time.Now().Unix())}},
c.memoryUsageBytes: {{valueType: prometheus.GaugeValue, value: float64(stats.Memory.Usage)}},
c.memoryWorkingSet: {{valueType: prometheus.GaugeValue, value: float64(stats.Memory.WorkingSet)}},
c.networkRxBytes: {{valueType: prometheus.CounterValue, value: float64(stats.Network.RxBytes)}},
c.networkRxPackets: {{valueType: prometheus.CounterValue, value: float64(stats.Network.RxPackets)}},
c.networkRxErrors: {{valueType: prometheus.CounterValue, value: float64(stats.Network.RxErrors)}},
c.networkRxDropped: {{valueType: prometheus.CounterValue, value: float64(stats.Network.RxDropped)}},
c.networkTxBytes: {{valueType: prometheus.CounterValue, value: float64(stats.Network.TxBytes)}},
c.networkTxPackets: {{valueType: prometheus.CounterValue, value: float64(stats.Network.TxPackets)}},
c.networkTxErrors: {{valueType: prometheus.CounterValue, value: float64(stats.Network.TxErrors)}},
c.networkTxDropped: {{valueType: prometheus.CounterValue, value: float64(stats.Network.TxDropped)}},
} {
for _, m := range metrics {
ch <- prometheus.MustNewConstMetric(desc, prometheus.CounterValue, float64(m.value), append(m.labels, name, id)...)
}
}
// Metrics with dynamic labels
for i, value := range stats.Cpu.Usage.PerCpu {
ch <- prometheus.MustNewConstMetric(c.cpuUsageSecondsPerCPU, prometheus.CounterValue, float64(value)/float64(time.Second), name, id, fmt.Sprintf("cpu%02d", i))
}
for _, stat := range stats.Filesystem {
for desc, m := range map[*prometheus.Desc]prometheusMetric{
c.fsReads: {valueType: prometheus.CounterValue, value: float64(stat.ReadsCompleted)},
c.fsReadsSectors: {valueType: prometheus.CounterValue, value: float64(stat.SectorsRead)},
c.fsReadsMerged: {valueType: prometheus.CounterValue, value: float64(stat.ReadsMerged)},
c.fsReadTime: {valueType: prometheus.CounterValue, value: float64(stat.ReadTime) / float64(time.Second)},
c.fsWrites: {valueType: prometheus.CounterValue, value: float64(stat.WritesCompleted)},
c.fsWritesSectors: {valueType: prometheus.CounterValue, value: float64(stat.SectorsWritten)},
c.fsWritesMerged: {valueType: prometheus.CounterValue, value: float64(stat.WritesMerged)},
c.fsWriteTime: {valueType: prometheus.CounterValue, value: float64(stat.WriteTime) / float64(time.Second)},
c.fsIoTime: {valueType: prometheus.CounterValue, value: float64(stat.IoTime) / float64(time.Second)},
c.fsWeightedIoTime: {valueType: prometheus.CounterValue, value: float64(stat.WeightedIoTime) / float64(time.Second)},
c.fsIoInProgress: {valueType: prometheus.GaugeValue, value: float64(stat.IoInProgress)},
c.fsLimit: {valueType: prometheus.GaugeValue, value: float64(stat.Limit)},
c.fsUsage: {valueType: prometheus.GaugeValue, value: float64(stat.Usage)},
} {
ch <- prometheus.MustNewConstMetric(desc, m.valueType, m.value, name, id, stat.Device)
}
}
}
c.errors.Collect(ch)
}