-
Notifications
You must be signed in to change notification settings - Fork 1
/
aggregator.go
172 lines (147 loc) · 3.89 KB
/
aggregator.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
166
167
168
169
170
171
172
package main
import (
"context"
"fmt"
log "github.com/Sirupsen/logrus"
prom "github.com/prometheus/client_golang/prometheus"
)
type metricInfo struct {
name string
aggrType string
value float64
labelNames []string
labels prom.Labels
timestamp int64
}
type aggregationFn func(*metricInfo)
type aggregation struct {
afn aggregationFn
atype string
}
func metricHelp(minfo *metricInfo) string {
return "void"
}
func makeSumAggregation(registerer prom.Registerer,
mInfo *metricInfo) aggregationFn {
ops := prom.CounterOpts{
Name: mInfo.name,
Help: metricHelp(mInfo),
}
ctr := prom.NewCounterVec(ops, mInfo.labelNames)
if err := registerer.Register(ctr); err != nil {
log.Warnf("Failed to create a counter '%s': %v", mInfo.name, err)
}
return func(inmInfo *metricInfo) {
log.Debugf("[COUNTER AGGR] :: (%s) (%v) <- %f", inmInfo.name,
inmInfo.labels, inmInfo.value)
ctr.With(inmInfo.labels).Add(inmInfo.value)
}
}
func makeGaugeAggregation(registerer prom.Registerer,
mInfo *metricInfo) aggregationFn {
luTs := mInfo.timestamp
ops := prom.GaugeOpts{
Name: mInfo.name,
Help: metricHelp(mInfo),
}
gm := prom.NewGaugeVec(ops, mInfo.labelNames)
if err := registerer.Register(gm); err != nil {
log.Warnf("Failed to create a gauge '%s': %v", mInfo.name, err)
}
return func(inmInfo *metricInfo) {
if inmInfo.timestamp >= luTs {
log.Debugf("[GAUGE AGGR] :: (%s) (%v) <- %f", inmInfo.name,
inmInfo.labels, inmInfo.value)
gm.With(inmInfo.labels).Set(inmInfo.value)
luTs = inmInfo.timestamp
}
}
}
func makeAggregation(registerer prom.Registerer,
mInfo *metricInfo) (*aggregation, error) {
var err error
var afn aggregationFn
switch mInfo.aggrType {
case "counter":
afn = makeSumAggregation(registerer, mInfo)
log.Debugf("New COUNTER aggregation: %s", mInfo.name)
case "gauge":
afn = makeGaugeAggregation(registerer, mInfo)
log.Debugf("New GAUGE aggregation: %s", mInfo.name)
default:
err = fmt.Errorf("Unknown aggregation type: '%s'", mInfo.aggrType)
}
if err != nil {
return nil, err
}
return &aggregation{
afn: afn,
atype: mInfo.aggrType,
}, nil
}
type aggregatorContext struct {
registerer prom.Registerer
gatherer prom.Gatherer
aggrTable map[string]*aggregation
}
func aggregateMetric(actx *aggregatorContext, mInfo *metricInfo) error {
aggr, ok := actx.aggrTable[mInfo.name]
if !ok {
var err error
aggr, err = makeAggregation(actx.registerer, mInfo)
if err != nil {
return err
}
actx.aggrTable[mInfo.name] = aggr
}
if aggr.atype != mInfo.aggrType {
return fmt.Errorf("Can't aggregate metric \"%s\" type \"%s\" as it is"+
" already regisited with different type \"%s\"", mInfo.name,
mInfo.aggrType, aggr.atype)
}
aggr.afn(mInfo)
return nil
}
func newAggregatorContext(registry *prom.Registry) *aggregatorContext {
var ctx aggregatorContext
if registry == nil {
ctx.gatherer = prom.DefaultGatherer
ctx.registerer = prom.DefaultRegisterer
} else {
ctx.gatherer = registry
ctx.registerer = registry
}
ctx.aggrTable = make(map[string]*aggregation)
return &ctx
}
func launchAggregator(ctx context.Context, minfoChan chan *metricInfo,
pio *promIO) error {
return launchAggregatorWithCustomRegistry(ctx, minfoChan, pio, nil)
}
func launchAggregatorWithCustomRegistry(ctx context.Context,
minfoChan chan *metricInfo, pio *promIO, registry *prom.Registry) error {
log.Debug("Running aggregator ...")
actx := newAggregatorContext(registry)
for {
select {
case mInfo, chanOk := <-minfoChan:
if chanOk {
err := aggregateMetric(actx, mInfo)
if err != nil {
log.Warnf("Failed to aggregate metric '%v': %v",
mInfo.name, err)
}
}
break
case _, chanOk := <-pio.scrapeSignalChan:
if chanOk {
log.Debug("Sending aggregated metrics ...")
msg := promMessage{gatherer: actx.gatherer}
pio.messageChan <- msg
}
break
case <-ctx.Done():
return ctx.Err()
}
}
}