forked from 0xERR0R/dex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector.go
165 lines (139 loc) · 4.81 KB
/
collector.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
package main
import (
"context"
"encoding/json"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
"strings"
"sync"
)
var labelCname = []string{"container_name"}
type DockerCollector struct {
cli *client.Client
}
func newDockerCollector() *DockerCollector {
cli, err := client.NewEnvClient()
if err != nil {
log.Fatalf("can't create docker client: ", err)
}
return &DockerCollector{
cli: cli,
}
}
func (c *DockerCollector) Describe(_ chan<- *prometheus.Desc) {
}
func (c *DockerCollector) Collect(ch chan<- prometheus.Metric) {
containers, err := c.cli.ContainerList(context.Background(), types.ContainerListOptions{
All: true,
})
if err != nil {
log.Error("can't list containers: ", err)
return
}
var wg sync.WaitGroup
for _, container := range containers {
wg.Add(1)
go c.processContainer(container, ch, &wg)
}
wg.Wait()
}
func (c *DockerCollector) processContainer(container types.Container, ch chan<- prometheus.Metric, wg *sync.WaitGroup) {
defer wg.Done()
cName := strings.TrimPrefix(strings.Join(container.Names, ";"), "/")
var isRunning float64
if container.State == "running" {
isRunning = 1
}
// container state metric for all containers
ch <- prometheus.MustNewConstMetric(prometheus.NewDesc(
"dex_container_running",
"1 if docker container is running, 0 otherwise",
labelCname,
nil,
), prometheus.GaugeValue, isRunning, cName)
// stats metrics only for running containers
if isRunning == 1 {
if stats, err := c.cli.ContainerStats(context.Background(), container.ID, false); err != nil {
log.Fatal(err)
} else {
var containerStats containerStats
err := json.NewDecoder(stats.Body).Decode(&containerStats)
if err != nil {
log.Error("can't read api stats: ", err)
}
if err := stats.Body.Close(); err != nil {
log.Error("can't close body: ", err)
}
c.blockIoMetrics(ch, &containerStats, cName)
c.memoryMetrics(ch, &containerStats, cName)
c.networkMetrics(ch, &containerStats, cName)
c.CPUMetrics(ch, &containerStats, cName)
}
}
}
func (c *DockerCollector) CPUMetrics(ch chan<- prometheus.Metric, containerStats *containerStats, cName string) {
cpuDelta := containerStats.CPU.CPUUsage.TotalUsage - containerStats.PreCPU.CPUUsage.TotalUsage
sysemDelta := containerStats.CPU.SystemCpuUsage - containerStats.PreCPU.SystemCpuUsage
cpuUtilization := float64(cpuDelta) / float64(sysemDelta) * 100.0
ch <- prometheus.MustNewConstMetric(prometheus.NewDesc(
"dex_cpu_utilization_percent",
"CPU utilization in percent",
labelCname,
nil,
), prometheus.GaugeValue, cpuUtilization, cName)
}
func (c *DockerCollector) networkMetrics(ch chan<- prometheus.Metric, containerStats *containerStats, cName string) {
ch <- prometheus.MustNewConstMetric(prometheus.NewDesc(
"dex_network_rx_bytes",
"Network received bytes total",
labelCname,
nil,
), prometheus.CounterValue, float64(containerStats.Networks.Eth0.RxBytes), cName)
ch <- prometheus.MustNewConstMetric(prometheus.NewDesc(
"dex_network_tx_bytes",
"Network sent bytes total",
labelCname,
nil,
), prometheus.CounterValue, float64(containerStats.Networks.Eth0.TxBytes), cName)
}
func (c *DockerCollector) memoryMetrics(ch chan<- prometheus.Metric, containerStats *containerStats, cName string) {
// From official documentation
//Note: On Linux, the Docker CLI reports memory usage by subtracting page cache usage from the total memory usage.
//The API does not perform such a calculation but rather provides the total memory usage and the amount from the page cache so that clients can use the data as needed.
memoryUsage := containerStats.Memory.Usage - containerStats.Memory.MemoryStats.Cache
memoryUtilization := float64(memoryUsage) / float64(containerStats.Memory.Limit) * 100.0
ch <- prometheus.MustNewConstMetric(prometheus.NewDesc(
"dex_memory_usage_bytes",
"Total memory usage bytes",
labelCname,
nil,
), prometheus.CounterValue, float64(memoryUsage), cName)
ch <- prometheus.MustNewConstMetric(prometheus.NewDesc(
"dex_memory_utilization_percent",
"Memory utilization percent",
labelCname,
nil,
), prometheus.GaugeValue, memoryUtilization, cName)
}
func (c *DockerCollector) blockIoMetrics(ch chan<- prometheus.Metric, containerStats *containerStats, cName string) {
for _, b := range containerStats.BlockIO.IOBytes {
if b.Op == "Read" {
ch <- prometheus.MustNewConstMetric(prometheus.NewDesc(
"dex_block_io_read_bytes",
"Block I/O read bytes",
labelCname,
nil,
), prometheus.CounterValue, float64(b.Value), cName)
}
if b.Op == "Write" {
ch <- prometheus.MustNewConstMetric(prometheus.NewDesc(
"dex_block_io_write_bytes",
"Block I/O write bytes",
labelCname,
nil,
), prometheus.CounterValue, float64(b.Value), cName)
}
}
}