-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatic_collector.go
137 lines (106 loc) · 2.77 KB
/
static_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
package stats
import (
"bytes"
"encoding/json"
"sort"
)
type StaticCollector struct {
counters map[string]Int64VectorGetter
gauges map[string]Int64VectorGetter
histograms map[string]HistogramVectorGetter
}
func NewStaticCollector() *StaticCollector {
return &StaticCollector{
counters: make(map[string]Int64VectorGetter),
gauges: make(map[string]Int64VectorGetter),
histograms: make(map[string]HistogramVectorGetter),
}
}
func (c *StaticCollector) Close() error { return nil }
func (c *StaticCollector) RegisterCounter(n string, g Int64VectorGetter) {
c.counters[n] = g
}
func (c *StaticCollector) RegisterGauge(n string, g Int64VectorGetter) {
c.gauges[n] = g
}
func (c *StaticCollector) RegisterHistogram(n string, g HistogramVectorGetter) {
c.histograms[n] = g
}
type Int64Snapshot struct {
Name string
Labels map[string]string
Value int64
}
func int64snapshots(n string, g Int64VectorGetter) []Int64Snapshot {
var sns []Int64Snapshot
for _, v := range g.Get() {
sns = append(
sns,
Int64Snapshot{
Name: n,
Labels: v.Tags,
Value: v.Value,
},
)
}
return sns
}
type HistogramSnapshot struct {
Name string
Value HistogramValue
}
type Snapshot struct {
Counters []Int64Snapshot
Gauges []Int64Snapshot
Histograms []HistogramSnapshot
}
func compareLabels(x, y map[string]string) int {
var (
xj, _ = json.Marshal(x)
yj, _ = json.Marshal(y)
)
return bytes.Compare(xj, yj)
}
type Int64Snapshots []Int64Snapshot
func (ss Int64Snapshots) Len() int { return len(ss) }
func (ss Int64Snapshots) Less(i int, j int) bool {
if ss[i].Name != ss[j].Name {
return ss[i].Name < ss[j].Name
}
return compareLabels(ss[i].Labels, ss[j].Labels) < 0
}
func (ss Int64Snapshots) Swap(i int, j int) {
ss[j], ss[i] = ss[i], ss[j]
}
type HistogramSnapshots []HistogramSnapshot
func (ss HistogramSnapshots) Len() int { return len(ss) }
func (ss HistogramSnapshots) Less(i int, j int) bool {
if ss[i].Name != ss[j].Name {
return ss[i].Name < ss[j].Name
}
return compareLabels(ss[i].Value.Tags, ss[j].Value.Tags) < 0
}
func (ss HistogramSnapshots) Swap(i int, j int) {
ss[j], ss[i] = ss[i], ss[j]
}
func (c *StaticCollector) Get() Snapshot {
var (
counters, gauges []Int64Snapshot
histograms []HistogramSnapshot
)
for n, g := range c.counters {
counters = append(counters, int64snapshots(n, g)...)
}
for n, g := range c.gauges {
gauges = append(gauges, int64snapshots(n, g)...)
}
for n, g := range c.histograms {
for _, v := range g.Get() {
histograms = append(histograms, HistogramSnapshot{Name: n, Value: *v})
}
}
sort.Sort(Int64Snapshots(counters))
sort.Sort(Int64Snapshots(gauges))
sort.Sort(HistogramSnapshots(histograms))
return Snapshot{Counters: counters, Gauges: gauges, Histograms: histograms}
}