-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmetrics.go
More file actions
343 lines (318 loc) · 9.27 KB
/
metrics.go
File metadata and controls
343 lines (318 loc) · 9.27 KB
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
package main
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"runtime"
"sync"
"time"
"github.com/omegaup/quark/grader"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/load"
"github.com/shirou/gopsutil/mem"
)
type staticConfig struct {
Targets []string `json:"targets"`
Labels map[string]string `json:"labels,omitempty"`
}
var (
gauges = map[string]prometheus.Gauge{
"cpu_load1": prometheus.NewGauge(prometheus.GaugeOpts{
Subsystem: "os",
Help: "CPU load 1",
Name: "cpu_load1",
}),
"cpu_load5": prometheus.NewGauge(prometheus.GaugeOpts{
Subsystem: "os",
Help: "CPU load 5",
Name: "cpu_load5",
}),
"cpu_load15": prometheus.NewGauge(prometheus.GaugeOpts{
Subsystem: "os",
Help: "CPU load 15",
Name: "cpu_load15",
}),
"mem_total": prometheus.NewGauge(prometheus.GaugeOpts{
Subsystem: "os",
Help: "Total amount of RAM",
Name: "mem_total",
}),
"mem_used": prometheus.NewGauge(prometheus.GaugeOpts{
Subsystem: "os",
Help: "RAM used by programs",
Name: "mem_used",
}),
"disk_total": prometheus.NewGauge(prometheus.GaugeOpts{
Subsystem: "os",
Help: "Total amount of disk space",
Name: "disk_total",
}),
"disk_used": prometheus.NewGauge(prometheus.GaugeOpts{
Subsystem: "os",
Help: "Disk space used by programs",
Name: "disk_used",
}),
"grader_queue_total_length": prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "quark",
Subsystem: "grader",
Help: "The length of the queue",
Name: "queue_total_length",
}),
"grader_queue_ephemeral_length": prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "quark",
Subsystem: "grader",
Help: "The length of the ephemeral queue",
Name: "queue_ephemeral_length",
}),
"grader_queue_low_length": prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "quark",
Subsystem: "grader",
Help: "The length of the low-priority queue",
Name: "queue_low_length",
}),
"grader_queue_normal_length": prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "quark",
Subsystem: "grader",
Help: "The length of the normal-priority queue",
Name: "queue_normal_length",
}),
"grader_queue_high_length": prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "quark",
Subsystem: "grader",
Help: "The length of the high-priority queue",
Name: "queue_high_length",
}),
}
gaugeVecs = map[string]*prometheus.GaugeVec{
"grader_runner_up": prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "quark",
Subsystem: "grader",
Help: "Graders seen in the last 5 minutes",
Name: "runner_up",
},
[]string{"runner_hostname", "runner_public_ip"},
),
}
counters = map[string]prometheus.Counter{
"grader_ephemeral_runs_total": prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "quark",
Subsystem: "grader",
Help: "Number of graded ephemeral runs",
Name: "ephemeral_runs_total",
}),
"grader_ci_jobs_total": prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "quark",
Subsystem: "grader",
Help: "Number of CI jobs",
Name: "ci_jobs_total",
}),
"grader_runs_total": prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "quark",
Subsystem: "grader",
Help: "Number of graded runs",
Name: "runs_total",
}),
"grader_runs_retry": prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "quark",
Subsystem: "grader",
Help: "Number of runs that were retried",
Name: "runs_retry",
}),
"grader_runs_abandoned": prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "quark",
Subsystem: "grader",
Help: "Number of runs that were abandoned",
Name: "runs_abandoned",
}),
"grader_runs_je": prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "quark",
Subsystem: "grader",
Help: "Number of runs that were JE",
Name: "runs_je",
}),
}
summaries = map[string]prometheus.Summary{
"grader_queue_delay_seconds": prometheus.NewSummary(prometheus.SummaryOpts{
Namespace: "quark",
Subsystem: "grader",
Help: "The duration of a run in any queue",
Name: "queue_delay_seconds",
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
}),
"grader_queue_ephemeral_delay_seconds": prometheus.NewSummary(prometheus.SummaryOpts{
Namespace: "quark",
Subsystem: "grader",
Help: "The duration of a run in the ephemeral queue",
Name: "queue_ephemeral_delay_seconds",
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
}),
"grader_queue_low_delay_seconds": prometheus.NewSummary(prometheus.SummaryOpts{
Namespace: "quark",
Subsystem: "grader",
Help: "The duration of a run in the low-priority queue",
Name: "queue_low_delay_seconds",
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
}),
"grader_queue_normal_delay_seconds": prometheus.NewSummary(prometheus.SummaryOpts{
Namespace: "quark",
Subsystem: "grader",
Help: "The duration of a run in the normal-priority queue",
Name: "queue_normal_delay_seconds",
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
}),
"grader_queue_high_delay_seconds": prometheus.NewSummary(prometheus.SummaryOpts{
Namespace: "quark",
Subsystem: "grader",
Help: "The duration of a run in the high-priority queue",
Name: "queue_high_delay_seconds",
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
}),
}
)
type observedRunner struct {
hostname string
publicIP string
lastSeen time.Time
}
type prometheusMetrics struct {
sync.Mutex
runners map[string]observedRunner
}
func (p *prometheusMetrics) GaugeAdd(name string, value float64) {
if gauge, ok := gauges[name]; ok {
gauge.Add(value)
}
}
func (p *prometheusMetrics) CounterAdd(name string, value float64) {
if counter, ok := counters[name]; ok {
counter.Add(value)
}
}
func (p *prometheusMetrics) SummaryObserve(name string, value float64) {
if summary, ok := summaries[name]; ok {
summary.Observe(value)
}
}
func (p *prometheusMetrics) RunnerObserve(hostname string, publicIP string) {
p.Lock()
p.runners[hostname] = observedRunner{
hostname: hostname,
publicIP: publicIP,
lastSeen: time.Now(),
}
p.Unlock()
}
func setupMetrics(ctx *grader.Context) {
for _, gauge := range gauges {
prometheus.MustRegister(gauge)
}
for _, gaugeVec := range gaugeVecs {
prometheus.MustRegister(gaugeVec)
}
for _, counter := range counters {
prometheus.MustRegister(counter)
}
for _, summary := range summaries {
prometheus.MustRegister(summary)
}
buildInfoCounter := prometheus.NewCounter(prometheus.CounterOpts{
Help: "Information about the build",
Name: "build_info",
ConstLabels: prometheus.Labels{
"version": ProgramVersion,
"go_version": runtime.Version(),
},
})
prometheus.MustRegister(buildInfoCounter)
buildInfoCounter.Inc()
m := &prometheusMetrics{
runners: make(map[string]observedRunner),
}
ctx.Metrics = m
metricsMux := http.NewServeMux()
metricsMux.Handle("/metrics", promhttp.Handler())
metricsMux.HandleFunc("/metrics/runners", m.runnersHandler)
go func() {
addr := fmt.Sprintf(":%d", ctx.Config.Metrics.Port)
err := http.ListenAndServe(addr, metricsMux)
if !errors.Is(err, http.ErrServerClosed) {
ctx.Log.Error(
"http listen and serve",
map[string]any{
"err": err,
},
)
}
}()
go func() {
ticker := time.NewTicker(time.Minute)
defer ticker.Stop()
for range ticker.C {
m.gaugesUpdate()
}
}()
}
func (p *prometheusMetrics) gaugesUpdate() {
if s, err := load.Avg(); err == nil {
gauges["cpu_load1"].Set(s.Load1)
gauges["cpu_load5"].Set(s.Load5)
gauges["cpu_load15"].Set(s.Load15)
}
if s, err := mem.VirtualMemory(); err == nil {
gauges["mem_total"].Set(float64(s.Total))
gauges["mem_used"].Set(float64(s.Used))
}
if s, err := disk.Usage("/"); err == nil {
gauges["disk_total"].Set(float64(s.Total))
gauges["disk_used"].Set(float64(s.Used))
}
cutoffTime := time.Now().Add(-3 * time.Minute)
shouldReset := false
p.Lock()
runners := make([]observedRunner, 0, len(p.runners))
for hostname, runner := range p.runners {
if runner.lastSeen.Before(cutoffTime) {
shouldReset = true
delete(p.runners, hostname)
continue
}
runners = append(runners, runner)
}
p.Unlock()
v := gaugeVecs["grader_runner_up"]
if shouldReset {
v.Reset()
}
for _, runner := range runners {
v.WithLabelValues(runner.hostname, runner.publicIP).Set(1.0)
}
}
func (p *prometheusMetrics) runnersHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
w.Header().Add("Content-Type", "application/json")
var configs []staticConfig
cutoffTime := time.Now().Add(-3 * time.Minute)
p.Lock()
for hostname, runner := range p.runners {
if runner.lastSeen.Before(cutoffTime) {
delete(p.runners, hostname)
continue
}
configs = append(
configs,
staticConfig{
Targets: []string{runner.publicIP},
Labels: map[string]string{
"__address__": runner.hostname,
"hostname": runner.hostname,
},
},
)
}
p.Unlock()
json.NewEncoder(w).Encode(configs)
}