@@ -21,10 +21,11 @@ const (
21
21
)
22
22
23
23
var (
24
- knownContainerIDs map [string ]prometheus.Labels
25
- knownContainerNetworks map [string ]prometheus.Labels
26
- knownContainerInfos map [string ]prometheus.Labels
27
- knownDataNames map [string ]prometheus.Labels
24
+ knownContainerIDs map [string ]prometheus.Labels
25
+ knownContainerNetworks map [string ]prometheus.Labels
26
+ knownContainerDiskStats map [string ]prometheus.Labels
27
+ knownContainerInfos map [string ]prometheus.Labels
28
+ knownDataNames map [string ]prometheus.Labels
28
29
29
30
pids * prometheus.GaugeVec
30
31
cpuUsageUser * prometheus.GaugeVec
@@ -42,18 +43,15 @@ var (
42
43
networkReceiveDropped * prometheus.GaugeVec
43
44
networkTransmitDropped * prometheus.GaugeVec
44
45
45
- containerInfo * prometheus.GaugeVec
46
+ diskIOBytes * prometheus.GaugeVec
46
47
47
- dataFree * prometheus.GaugeVec
48
- dataAvailable * prometheus.GaugeVec
49
- dataSize * prometheus.GaugeVec
50
- dataInodesFree * prometheus.GaugeVec
51
- dataInodes * prometheus.GaugeVec
48
+ containerInfo * prometheus.GaugeVec
52
49
)
53
50
54
51
func setup () {
55
52
containerLabels := []string {"container_name" , "compose_project" , "compose_service" }
56
53
containerNetworkLabels := append (containerLabels , "interface" )
54
+ containerDiskLabels := append (containerLabels , "op" )
57
55
containerInfoLabels := []string {
58
56
"container_id" ,
59
57
"container_name" ,
@@ -131,6 +129,11 @@ func setup() {
131
129
Help : "Container network transmit drops" ,
132
130
}, containerNetworkLabels )
133
131
132
+ diskIOBytes = prometheus .NewGaugeVec (prometheus.GaugeOpts {
133
+ Name : containerPrefix + "disk_io_bytes" ,
134
+ Help : "Container disk IO bytes" ,
135
+ }, containerDiskLabels )
136
+
134
137
containerInfo = prometheus .NewGaugeVec (prometheus.GaugeOpts {
135
138
Name : containerPrefix + "info" ,
136
139
Help : "Container info" ,
@@ -152,12 +155,15 @@ func setup() {
152
155
prometheus .MustRegister (networkReceiveDropped )
153
156
prometheus .MustRegister (networkTransmitDropped )
154
157
158
+ prometheus .MustRegister (diskIOBytes )
159
+
155
160
prometheus .MustRegister (containerInfo )
156
161
}
157
162
158
163
func updateContainers (docker * client.Client ) {
159
164
newKnownContainerIDs := make (map [string ]prometheus.Labels )
160
165
newKnownContainerNetworks := make (map [string ]prometheus.Labels )
166
+ newKnownContainerDiskStats := make (map [string ]prometheus.Labels )
161
167
newKnownContainerInfos := make (map [string ]prometheus.Labels )
162
168
containers , err := docker .ContainerList (context .Background (), types.ContainerListOptions {})
163
169
if err != nil {
@@ -219,6 +225,19 @@ func updateContainers(docker *client.Client) {
219
225
networkTransmitDropped .With (labels ).Set (float64 (net .TxDropped ))
220
226
}
221
227
228
+ // Disk IO
229
+ for _ , stat := range stats .BlkioStats .IoServiceBytesRecursive {
230
+ labels := prometheus.Labels {
231
+ "container_name" : strings .TrimPrefix (container .Names [0 ], "/" ),
232
+ "compose_project" : container .Labels ["com.docker.compose.project" ],
233
+ "compose_service" : container .Labels ["com.docker.compose.service" ],
234
+ "op" : stat .Op ,
235
+ }
236
+ newKnownContainerDiskStats [container .ID + "bytes" + stat .Op ] = labels
237
+
238
+ diskIOBytes .With (labels ).Set (float64 (stat .Value ))
239
+ }
240
+
222
241
// Container info
223
242
{
224
243
labels := prometheus.Labels {
@@ -264,6 +283,11 @@ func updateContainers(docker *client.Client) {
264
283
networkTransmitDropped .Delete (labels )
265
284
}
266
285
}
286
+ for id , labels := range knownContainerDiskStats {
287
+ if newKnownContainerDiskStats [id ] == nil {
288
+ diskIOBytes .Delete (labels )
289
+ }
290
+ }
267
291
for id , labels := range knownContainerInfos {
268
292
if newKnownContainerInfos [id ] == nil {
269
293
containerInfo .Delete (labels )
@@ -274,20 +298,18 @@ func updateContainers(docker *client.Client) {
274
298
knownContainerInfos = newKnownContainerInfos
275
299
}
276
300
277
- func updateMetrics (docker * client.Client ) {
278
- for {
279
- updateContainers (docker )
280
- }
281
- }
282
-
283
301
func main () {
284
302
docker , err := client .NewClientWithOpts (client .FromEnv )
285
303
if err != nil {
286
304
panic (err )
287
305
}
288
306
289
307
setup ()
290
- go updateMetrics (docker )
308
+ go func () {
309
+ for {
310
+ updateContainers (docker )
311
+ }
312
+ }()
291
313
292
314
http .Handle ("/metrics" , promhttp .Handler ())
293
315
http .ListenAndServe (":8080" , nil )
0 commit comments