-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
60 lines (49 loc) · 986 Bytes
/
metrics.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
package god
import (
"fmt"
"sort"
"strings"
"time"
)
type metric struct {
name string
metric Metric
}
var metrics []metric
// Metric represents a periodically collected metric.
type Metric interface {
// Collect is called every second to retrieve the current value.
Collect() string
}
// Register will register the provided metric.
func Register(name string, m Metric) {
// add metric
metrics = append(metrics, metric{
name: name,
metric: m,
})
// sort by name
sort.Slice(metrics, func(i, j int) bool {
return metrics[i].name < metrics[j].name
})
}
func printMetrics() {
// create ticker
ticker := time.Tick(time.Second)
// print metrics
for {
// await tick
<-ticker
// check metrics
if len(metrics) == 0 {
continue
}
// collect strings
s := make([]string, 0, len(metrics))
for _, m := range metrics {
s = append(s, fmt.Sprintf("%s: %s", m.name, m.metric.Collect()))
}
// print
fmt.Println(strings.Join(s, " - "))
}
}