forked from blind-oracle/cortex-tenant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
processor.go
306 lines (242 loc) · 6.32 KB
/
processor.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
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
package main
import (
"bytes"
"fmt"
"net"
"sync"
"sync/atomic"
"time"
"github.com/blind-oracle/go-common/logger"
"github.com/gogo/protobuf/proto"
"github.com/golang/snappy"
"github.com/google/uuid"
me "github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
"github.com/prometheus/prometheus/prompb"
fh "github.com/valyala/fasthttp"
)
type result struct {
code int
body []byte
err error
}
type processor struct {
cfg config
srv *fh.Server
cli *fh.Client
shuttingDown uint32
logger.Logger
}
func newProcessor(c config) *processor {
p := &processor{
cfg: c,
Logger: logger.NewSimpleLogger("proc"),
}
p.srv = &fh.Server{
Name: "cortex-tenant",
Handler: p.handle,
MaxRequestBodySize: 8 * 1024 * 1024,
ReadTimeout: c.Timeout,
WriteTimeout: c.Timeout,
IdleTimeout: 60 * time.Second,
Concurrency: c.Concurrency,
}
p.cli = &fh.Client{
Name: "cortex-tenant",
ReadTimeout: c.Timeout,
WriteTimeout: c.Timeout,
MaxConnWaitTimeout: 1 * time.Second,
MaxConnsPerHost: 64,
}
// For testing
if c.pipeOut != nil {
p.cli.Dial = func(a string) (net.Conn, error) {
return c.pipeOut.Dial()
}
}
return p
}
func (p *processor) run() (err error) {
var l net.Listener
// For testing
if p.cfg.pipeIn == nil {
if l, err = net.Listen("tcp", p.cfg.Listen); err != nil {
return
}
} else {
l = p.cfg.pipeIn
}
go p.srv.Serve(l)
return
}
func (p *processor) handle(ctx *fh.RequestCtx) {
if bytes.Equal(ctx.Path(), []byte("/alive")) {
if atomic.LoadUint32(&p.shuttingDown) == 1 {
ctx.SetStatusCode(fh.StatusServiceUnavailable)
}
return
}
if !bytes.Equal(ctx.Request.Header.Method(), []byte("POST")) {
ctx.Error("Expecting POST", fh.StatusBadRequest)
return
}
if !bytes.Equal(ctx.Path(), []byte("/push")) {
ctx.SetStatusCode(fh.StatusNotFound)
return
}
wrReqIn, err := p.unmarshal(ctx.Request.Body())
if err != nil {
ctx.Error(err.Error(), fh.StatusBadRequest)
return
}
if len(wrReqIn.Timeseries) == 0 {
// If there's metadata - just accept the request and drop it
if len(wrReqIn.Metadata) > 0 {
return
}
ctx.Error("No timeseries found in the request", fh.StatusBadRequest)
return
}
clientIP := ctx.RemoteAddr()
reqID, _ := uuid.NewRandom()
m, err := p.createWriteRequests(wrReqIn)
if err != nil {
ctx.Error(err.Error(), fh.StatusBadRequest)
return
}
var errs *me.Error
results := p.dispatch(clientIP, reqID, m)
for _, r := range results {
if r.err != nil {
errs = me.Append(errs, r.err)
p.Errorf("src=%s %s", clientIP, r.err)
} else if r.code < 200 || r.code >= 300 {
errs = me.Append(errs, fmt.Errorf("HTTP code %d (%s)", r.code, string(r.body)))
p.Errorf("src=%s req_id=%s HTTP code %d (%s)", clientIP, reqID, r.code, string(r.body))
}
}
if errs.ErrorOrNil() != nil {
ctx.Error(errs.Error(), fh.StatusInternalServerError)
return
}
// Return 500 for any error unless AccpetAll true
if p.cfg.Tenant.AcceptAll {
results[0].code = 204
results[0].body = nil
}
// Otherwise if all went fine return the code and body from 1st request
ctx.SetBody(results[0].body)
ctx.SetStatusCode(results[0].code)
return
}
func (p *processor) createWriteRequests(wrReqIn *prompb.WriteRequest) (map[string]*prompb.WriteRequest, error) {
// Create per-tenant write requests
m := map[string]*prompb.WriteRequest{}
for _, ts := range wrReqIn.Timeseries {
tenant, err := p.processTimeseries(&ts)
if err != nil {
return nil, err
}
wrReqOut, ok := m[tenant]
if !ok {
wrReqOut = &prompb.WriteRequest{}
m[tenant] = wrReqOut
}
wrReqOut.Timeseries = append(wrReqOut.Timeseries, ts)
}
return m, nil
}
func (p *processor) unmarshal(b []byte) (*prompb.WriteRequest, error) {
decoded, err := snappy.Decode(nil, b)
if err != nil {
return nil, errors.Wrap(err, "Unable to unpack Snappy")
}
req := &prompb.WriteRequest{}
if err = proto.Unmarshal(decoded, req); err != nil {
return nil, errors.Wrap(err, "Unable to unmarshal protobuf")
}
return req, nil
}
func (p *processor) marshal(wr *prompb.WriteRequest) (bufOut []byte, err error) {
b := make([]byte, wr.Size())
// Marshal to Protobuf
if _, err = wr.MarshalTo(b); err != nil {
return
}
// Compress with Snappy
return snappy.Encode(nil, b), nil
}
func (p *processor) dispatch(clientIP net.Addr, reqID uuid.UUID, m map[string]*prompb.WriteRequest) (res []result) {
var wg sync.WaitGroup
res = make([]result, len(m))
i := 0
for tenant, wrReq := range m {
wg.Add(1)
go func(idx int, tenant string, wrReq *prompb.WriteRequest) {
defer wg.Done()
var r result
r.code, r.body, r.err = p.send(clientIP, reqID, tenant, wrReq)
res[idx] = r
}(i, tenant, wrReq)
i++
}
wg.Wait()
return
}
func (p *processor) processTimeseries(ts *prompb.TimeSeries) (tenant string, err error) {
idx := 0
for i, l := range ts.Labels {
if l.Name == p.cfg.Tenant.Label {
tenant, idx = l.Value, i
break
}
}
if tenant == "" {
if p.cfg.Tenant.Default == "" {
return "", fmt.Errorf("Label '%s' not found", p.cfg.Tenant.Label)
}
return p.cfg.Tenant.Default, nil
}
if p.cfg.Tenant.LabelRemove {
l := len(ts.Labels)
ts.Labels[idx] = ts.Labels[l-1]
ts.Labels = ts.Labels[:l-1]
}
return
}
func (p *processor) send(clientIP net.Addr, reqID uuid.UUID, tenant string, wr *prompb.WriteRequest) (code int, body []byte, err error) {
req := fh.AcquireRequest()
resp := fh.AcquireResponse()
defer func() {
fh.ReleaseRequest(req)
fh.ReleaseResponse(resp)
}()
buf, err := p.marshal(wr)
if err != nil {
return
}
req.Header.SetMethod("POST")
req.Header.Set("Content-Encoding", "snappy")
req.Header.Set("Content-Type", "application/x-protobuf")
req.Header.Set("X-Prometheus-Remote-Write-Version", "0.1.0")
req.Header.Set("X-Cortex-Tenant-Client", clientIP.String())
req.Header.Set("X-Cortex-Tenant-ReqID", reqID.String())
req.Header.Set(p.cfg.Tenant.Header, tenant)
req.SetRequestURI(p.cfg.Target)
req.SetBody(buf)
if err = p.cli.DoTimeout(req, resp, p.cfg.Timeout); err != nil {
return
}
code = resp.Header.StatusCode()
body = make([]byte, len(resp.Body()))
copy(body, resp.Body())
return
}
func (p *processor) close() (err error) {
// Signal that we're shutting down
atomic.StoreUint32(&p.shuttingDown, 1)
// Let healthcheck detect that we're offline
time.Sleep(p.cfg.TimeoutShutdown)
// Shutdown
return p.srv.Shutdown()
}