forked from veepee-oss/influxdb-relay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
442 lines (364 loc) · 9.78 KB
/
http.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
package relay
import (
"bytes"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"golang.org/x/time/rate"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/influxdata/influxdb/models"
"github.com/strike-team/influxdb-relay/config"
)
// HTTP is a relay for HTTP influxdb writes
type HTTP struct {
addr string
name string
schema string
cert string
rp string
pingResponseCode int
pingResponseHeaders map[string]string
closing int64
l net.Listener
backends []*httpBackend
start time.Time
log bool
logger *log.Logger
rateLimiter *rate.Limiter
healthTimeout time.Duration
}
type relayHandlerFunc func(h *HTTP, w http.ResponseWriter, r *http.Request, start time.Time)
type relayMiddleware func(h *HTTP, handlerFunc relayHandlerFunc) relayHandlerFunc
// Default HTTP settings and a few constants
const (
DefaultHTTPPingResponse = http.StatusNoContent
DefaultHTTPTimeout = 10 * time.Second
DefaultMaxDelayInterval = 10 * time.Second
DefaultBatchSizeKB = 512
KB = 1024
MB = 1024 * KB
)
var (
handlers = map[string]relayHandlerFunc{
"/write": (*HTTP).handleStandard,
"/api/v1/prom/write": (*HTTP).handleProm,
"/ping": (*HTTP).handlePing,
"/status": (*HTTP).handleStatus,
"/admin": (*HTTP).handleAdmin,
"/admin/flush": (*HTTP).handleFlush,
"/health": (*HTTP).handleHealth,
}
middlewares = []relayMiddleware{
(*HTTP).bodyMiddleWare,
(*HTTP).queryMiddleWare,
(*HTTP).logMiddleWare,
(*HTTP).rateMiddleware,
}
)
// NewHTTP creates a new HTTP relay
// This relay will most likely be tied to a RelayService
// and manage a set of HTTPBackends
func NewHTTP(cfg config.HTTPConfig, verbose bool, fs config.Filters) (Relay, error) {
h := new(HTTP)
h.addr = cfg.Addr
h.name = cfg.Name
h.log = verbose
h.logger = log.New(os.Stdout, "relay: ", 0)
h.pingResponseCode = DefaultHTTPPingResponse
if cfg.DefaultPingResponse != 0 {
h.pingResponseCode = cfg.DefaultPingResponse
}
h.pingResponseHeaders = make(map[string]string)
h.pingResponseHeaders["X-InfluxDB-Version"] = "relay"
if h.pingResponseCode != http.StatusNoContent {
h.pingResponseHeaders["Content-Length"] = "0"
}
h.cert = cfg.SSLCombinedPem
h.rp = cfg.DefaultRetentionPolicy
// If a cert is specified, this means the user
// wants to do HTTPS
h.schema = "http"
if h.cert != "" {
h.schema = "https"
}
// For each output specified in the config, we are going to create a backend
for i := range cfg.Outputs {
backend, err := newHTTPBackend(&cfg.Outputs[i], fs)
if err != nil {
return nil, err
}
h.backends = append(h.backends, backend)
}
// If a RateLimit is specified, create a new limiter
if cfg.RateLimit != 0 {
if cfg.BurstLimit != 0 {
h.rateLimiter = rate.NewLimiter(rate.Limit(cfg.RateLimit), cfg.BurstLimit)
} else {
h.rateLimiter = rate.NewLimiter(rate.Limit(cfg.RateLimit), 1)
}
}
h.healthTimeout = time.Duration(cfg.HealthTimeout) * time.Millisecond
return h, nil
}
// Name is the name of the HTTP relay
// a default name might be generated if it is
// not specified in the configuration file
func (h *HTTP) Name() string {
if h.name == "" {
return fmt.Sprintf("%s://%s", h.schema, h.addr)
}
return h.name
}
// Run actually launch the HTTP endpoint
func (h *HTTP) Run() error {
var cert tls.Certificate
l, err := net.Listen("tcp", h.addr)
if err != nil {
return err
}
// support HTTPS
if h.cert != "" {
cert, err = tls.LoadX509KeyPair(h.cert, h.cert)
if err != nil {
return err
}
l = tls.NewListener(l, &tls.Config{
Certificates: []tls.Certificate{cert},
})
}
h.l = l
if h.log {
h.logger.Printf("starting %s relay %q on %v", strings.ToUpper(h.schema), h.Name(), h.addr)
}
err = http.Serve(l, h)
if atomic.LoadInt64(&h.closing) != 0 {
return nil
}
return err
}
// Stop actually stops the HTTP endpoint
func (h *HTTP) Stop() error {
atomic.StoreInt64(&h.closing, 1)
return h.l.Close()
}
// ServeHTTP is the function that handles the different route
// The response is a JSON object describing the state of the operation
func (h *HTTP) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// h.start = time.Now()
if fun, ok := handlers[r.URL.Path]; ok {
allMiddlewares(h, fun)(h, w, r, time.Now())
} else {
jsonResponse(w, response{http.StatusNotFound, http.StatusText(http.StatusNotFound)})
return
}
}
type responseData struct {
ContentType string
ContentEncoding string
StatusCode int
Body []byte
}
func (rd *responseData) Write(w http.ResponseWriter) {
if rd.ContentType != "" {
w.Header().Set("Content-Type", rd.ContentType)
}
if rd.ContentEncoding != "" {
w.Header().Set("Content-Encoding", rd.ContentEncoding)
}
w.Header().Set("Content-Length", strconv.Itoa(len(rd.Body)))
w.WriteHeader(rd.StatusCode)
w.Write(rd.Body)
}
func jsonResponse(w http.ResponseWriter, r response) {
data, err := json.Marshal(r.body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Length", fmt.Sprint(len(data)))
w.WriteHeader(r.code)
_, _ = w.Write(data)
}
type stats interface {
}
type poster interface {
post([]byte, string, string, string) (*responseData, error)
getStats() stats
}
type simpleStats struct {
Location string `json:"location"`
}
type simplePoster struct {
client *http.Client
location string
}
func newSimplePoster(location string, timeout time.Duration, skipTLSVerification bool) *simplePoster {
// Configure custom transport for http.Client
// Used for support skip-tls-verification option
transport := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: skipTLSVerification,
},
}
return &simplePoster{
client: &http.Client{
Timeout: timeout,
Transport: transport,
},
location: location,
}
}
func (s *simplePoster) getStats() stats {
return simpleStats{Location: s.location}
}
func (s *simplePoster) post(buf []byte, query string, auth string, endpoint string) (*responseData, error) {
req, err := http.NewRequest("POST", s.location+endpoint, bytes.NewReader(buf))
if err != nil {
return nil, err
}
req.URL.RawQuery = query
req.Header.Set("Content-Type", "text/plain")
req.Header.Set("Content-Length", strconv.Itoa(len(buf)))
if auth != "" {
req.Header.Set("Authorization", auth)
}
resp, err := s.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return &responseData{
ContentType: resp.Header.Get("Content-Type"),
ContentEncoding: resp.Header.Get("Content-Encoding"),
StatusCode: resp.StatusCode,
Body: data,
}, nil
}
type httpBackend struct {
poster
name string
inputType config.Input
admin string
endpoints config.HTTPEndpointConfig
location string
tagRegexps []*regexp.Regexp
measurementRegexps []*regexp.Regexp
}
// validateRegexps checks if a request on this backend matches
// all the tag regular expressions for this backend
func (b *httpBackend) validateRegexps(ps models.Points) error {
// For each point
for _, p := range ps {
// Check if the measurement of each point
// matches ALL measurement regular expressions
m := p.Name()
for _, r := range b.measurementRegexps {
if !r.Match(m) {
return errors.New("bad measurement")
}
}
// For each tag of each point
for _, t := range p.Tags() {
// Check if each tag of each point
// matches ALL tags regular expressions
for _, r := range b.tagRegexps {
if !r.Match(t.Key) {
return errors.New("bad tag")
}
}
}
}
return nil
}
func (b *httpBackend) getRetryBuffer() *retryBuffer {
if p, ok := b.poster.(*retryBuffer); ok {
return p
}
return nil
}
func newHTTPBackend(cfg *config.HTTPOutputConfig, fs config.Filters) (*httpBackend, error) {
// Get default name
if cfg.Name == "" {
cfg.Name = cfg.Location
}
// Set a timeout
timeout := DefaultHTTPTimeout
if cfg.Timeout != "" {
t, err := time.ParseDuration(cfg.Timeout)
if err != nil {
return nil, fmt.Errorf("error parsing HTTP timeout '%v'", err)
}
timeout = t
}
// Get underlying Poster instance
var p poster = newSimplePoster(cfg.Location, timeout, cfg.SkipTLSVerification)
// If configured, create a retryBuffer per backend.
// This way we serialize retries against each backend.
if cfg.BufferSizeMB > 0 {
max := DefaultMaxDelayInterval
if cfg.MaxDelayInterval != "" {
m, err := time.ParseDuration(cfg.MaxDelayInterval)
if err != nil {
return nil, fmt.Errorf("error parsing max retry time %v", err)
}
max = m
}
batch := DefaultBatchSizeKB * KB
if cfg.MaxBatchKB > 0 {
batch = cfg.MaxBatchKB * KB
}
p = newRetryBuffer(cfg.BufferSizeMB*MB, batch, max, p)
}
var tagRegexps []*regexp.Regexp
var measurementRegexps []*regexp.Regexp
// Get regexps related to this HTTP backend
for _, f := range fs {
for _, e := range f.Outputs {
if e == cfg.Name {
if f.TagRegexp != nil {
tagRegexps = append(tagRegexps, f.TagRegexp)
}
if f.MeasurementRegexp != nil {
measurementRegexps = append(measurementRegexps, f.MeasurementRegexp)
}
}
}
}
return &httpBackend{
poster: p,
name: cfg.Name,
tagRegexps: tagRegexps,
measurementRegexps: measurementRegexps,
endpoints: cfg.Endpoints,
location: cfg.Location,
}, nil
}
// ErrBufferFull error indicates that retry buffer is full
var ErrBufferFull = errors.New("retry buffer full")
var bufPool = sync.Pool{New: func() interface{} { return new(bytes.Buffer) }}
func getBuf() *bytes.Buffer {
if bb, ok := bufPool.Get().(*bytes.Buffer); ok {
return bb
}
return new(bytes.Buffer)
}
func putBuf(b *bytes.Buffer) {
b.Reset()
bufPool.Put(b)
}