-
Notifications
You must be signed in to change notification settings - Fork 6
/
cortex.go
249 lines (230 loc) · 6.12 KB
/
cortex.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
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
package push
import (
"context"
"flag"
"log"
"sync"
"time"
"github.com/cortexproject/cortex/pkg/ingester/client"
"github.com/cortexproject/cortex/pkg/util/grpcclient"
"github.com/prometheus/prometheus/pkg/labels"
"github.com/weaveworks/common/user"
"github.com/slim-bean/leafbus/pkg/stream"
)
var (
cortexBatchSize = 1000
timeSeriesPool = sync.Pool{
New: func() interface{} {
return &TimeSeries{
Labels: make([]labels.Labels, 0, cortexBatchSize),
Samples: make([]client.Sample, 0, cortexBatchSize),
}
},
}
packetsPool = sync.Pool{
New: func() interface{} {
return make([]*Packet, 0, cortexBatchSize)
},
}
packetPool = sync.Pool{
New: func() interface{} {
return &Packet{
Sample: samplePool.Get().(client.Sample),
Labels: make([]labels.Label, 0, 10),
}
},
}
samplePool = sync.Pool{
New: func() interface{} {
return client.Sample{
Value: 0,
TimestampMs: 0,
}
},
}
labelPool = sync.Pool{
New: func() interface{} {
return labels.Label{}
},
}
)
type TimeSeries struct {
Labels []labels.Labels
Samples []client.Sample
}
type Packet struct {
Labels labels.Labels
Sample client.Sample
}
type cortex struct {
cortex client.HealthAndIngesterClient
ctx context.Context
data chan *Packet
active []*Packet
actMtx sync.Mutex
streamMap map[string][]*ratedFollower
streamMtx sync.Mutex
}
func newCortex(address string) (*cortex, error) {
fs := flag.NewFlagSet("", flag.PanicOnError)
cfg := client.Config{
GRPCClientConfig: grpcclient.Config{},
}
cfg.RegisterFlags(fs)
clt, err := client.MakeIngesterClient(address, cfg)
if err != nil {
return nil, err
}
c := &cortex{
cortex: clt,
ctx: context.Background(),
data: make(chan *Packet, 100),
active: packetsPool.Get().([]*Packet),
streamMap: map[string][]*ratedFollower{},
}
go c.run()
return c, nil
}
type ratedFollower struct {
*stream.Follower
lastSent int64
}
func (c *cortex) follow(name string, follower *stream.Follower) {
c.streamMtx.Lock()
defer c.streamMtx.Unlock()
if _, ok := c.streamMap[name]; ok {
for i := range c.streamMap[name] {
if c.streamMap[name][i].Follower == follower {
log.Println("ERROR, stream is already being followed with this channel")
return
}
}
log.Printf("New follower registered for: %v, count: %v\n", name, len(c.streamMap[name]))
f := &ratedFollower{
Follower: follower,
lastSent: 0,
}
c.streamMap[name] = append(c.streamMap[name], f)
} else {
log.Println("First follower registered for: ", name)
f := &ratedFollower{
Follower: follower,
lastSent: 0,
}
c.streamMap[name] = []*ratedFollower{f}
}
}
func (c *cortex) unfollow(name string, follower *stream.Follower) {
c.streamMtx.Lock()
defer c.streamMtx.Unlock()
if _, ok := c.streamMap[name]; !ok {
log.Println("ERROR, tried to unfollow a stream not being followed")
return
} else {
for i := range c.streamMap[name] {
if c.streamMap[name][i].Follower == follower {
c.streamMap[name][i] = c.streamMap[name][len(c.streamMap[name])-1]
c.streamMap[name][len(c.streamMap[name])-1] = nil
c.streamMap[name] = c.streamMap[name][:len(c.streamMap[name])-1]
log.Printf("Removed follower for metric %v, %v remaining followers", name, len(c.streamMap[name]))
if len(c.streamMap[name]) == 0 {
log.Printf("No longer following any streams for metric %v, removing\n", name)
delete(c.streamMap, name)
}
return
}
}
log.Printf("ERROR: Failed to remove follower for %v, did not find any matching channels", name)
}
}
func (c *cortex) run() {
for {
select {
case p := <-c.data:
//Send to any live streamers
c.streamMtx.Lock()
if _, ok := c.streamMap[p.Labels.Get(name)]; ok {
for _, f := range c.streamMap[p.Labels.Get(name)] {
if len(f.Pub) >= 1 {
continue
}
// Check to see if it's time to send another sample based on the rate requested, if rate is enabled.
if f.Rate > 0 {
if p.Sample.TimestampMs-f.lastSent < f.Rate {
// Too soon, skip this sample
continue
} else {
// Equal or exceeded rate, update last send and allow this sample to be sent.
f.lastSent = p.Sample.TimestampMs
}
}
d := stream.GetData()
d.Name = p.Labels.Get(name)
d.Timestamp = p.Sample.TimestampMs
d.Val = p.Sample.Value
f.Pub <- d
}
}
c.streamMtx.Unlock()
// Add to batch
c.actMtx.Lock()
c.active = append(c.active, p)
// If batch is full, send to cortex
if len(c.active) == cortexBatchSize {
sending := c.active
c.active = packetsPool.Get().([]*Packet)
go c.push(sending)
}
c.actMtx.Unlock()
}
}
}
func (c *cortex) push(ps []*Packet) {
ts := timeSeriesPool.Get().(*TimeSeries)
for i := range ps {
ts.Labels = append(ts.Labels, ps[i].Labels)
ts.Samples = append(ts.Samples, ps[i].Sample)
}
// FIXME if this function took an array of pointers to ts.Samples I think it would save a bunch of copys and allocs
wr := client.ToWriteRequest(ts.Labels, ts.Samples, client.API)
ctx, cancel := context.WithTimeout(c.ctx, time.Second*2)
defer cancel()
log.Printf("Sending batch to cortex\n")
ctx = user.InjectOrgID(ctx, "leaf")
ctx = user.InjectUserID(ctx, "fake")
_, err := c.cortex.Push(ctx, wr)
if err != nil {
log.Println("Failed to end series to ingester: ", err)
//log.Printf("Timeseries: %v", ts)
//log.Printf("Samples: %v", wr.Timeseries)
}
client.ReuseSlice(wr.Timeseries)
reuseTimeseries(ts)
reusePackets(ps)
}
// ReuseTimeseries puts the timeseries back into a sync.Pool for reuse.
func reuseTimeseries(ts *TimeSeries) {
ts.Labels = ts.Labels[:0]
ts.Samples = ts.Samples[:0]
timeSeriesPool.Put(ts)
}
func reusePackets(ps []*Packet) {
// For each packet
//for i := range ps {
// // For each label in the packets label slice
// for j := range ps[i].Labels {
// // Return the label
// labelPool.Put(ps[i].Labels[j])
// }
// // Clear out the label slice
// ps[i].Labels = ps[i].Labels[:0]
// // Return the sample
// samplePool.Put(ps[i].Sample)
// // Return the packet
// packetPool.Put(ps[i])
//}
// Clear out the packet slice
ps = ps[:0]
// Return the packet slice
packetsPool.Put(ps)
}