-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
127 lines (111 loc) · 2.59 KB
/
main.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
package main
import (
"log"
"net/http"
"time"
"github.com/mschurenko/fargate_exporter/utils"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const (
// set to something lower than scrape interval
collectSleep = 5
)
var (
// task metrics
taskLabels = []string{
"cluster_name",
"task_family",
"task_id",
}
dfSize = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "fargate_overlay_disk_size",
Help: "disk size",
},
taskLabels,
)
dfFree = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "fargate_overlay_disk_free",
Help: "disk free",
},
taskLabels,
)
dfAvail = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "fargate_overlay_disk_avail",
Help: "disk available",
},
taskLabels,
)
// container metrics
containerLabels = append(taskLabels, []string{"container_name"}...)
totalCPU = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "fargate_cpu_usage_total",
Help: "Total CPU time consumed",
},
containerLabels,
)
systemCPU = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "fargate_cpu_usage_system",
Help: "System Usage",
},
containerLabels,
)
memoryLimit = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "fargate_memory_limit",
Help: "number of times memory usage hits limits",
},
containerLabels,
)
memoryUsage = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "fargate_memory_usage",
Help: "current res_counter usage for memory",
},
containerLabels,
)
)
func collectDiskFree() {
for {
ds := utils.GetDiskStats("/")
labels := []string{
ds.ClusterName,
ds.Family,
ds.TaskID,
}
dfSize.WithLabelValues(labels...).Set(float64(ds.Size))
dfFree.WithLabelValues(labels...).Set(float64(ds.Free))
dfAvail.WithLabelValues(labels...).Set(float64(ds.Avail))
time.Sleep(time.Second * collectSleep)
}
}
func collectContainerStats() {
for {
cs := utils.GetContainerStats()
for _, c := range cs {
labels := []string{
c.ClusterName,
c.Family,
c.TaskID,
c.ContainerName,
}
totalCPU.WithLabelValues(labels...).Set(float64(c.TotalCPU))
systemCPU.WithLabelValues(labels...).Set(float64(c.SystemCPU))
memoryUsage.WithLabelValues(labels...).Set(float64(c.MemoryUsage))
memoryLimit.WithLabelValues(labels...).Set(float64(c.MemoryLimit))
}
time.Sleep(time.Second * collectSleep)
}
}
func main() {
go collectDiskFree()
go collectContainerStats()
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(":2112", nil))
}