-
Notifications
You must be signed in to change notification settings - Fork 13
/
influxdb_listener.go
422 lines (361 loc) · 12.1 KB
/
influxdb_listener.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
package influxdblistener
import (
"compress/gzip"
"context"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
"time"
"github.com/circonus-labs/circonus-unified-agent/cua"
"github.com/circonus-labs/circonus-unified-agent/internal"
tlsint "github.com/circonus-labs/circonus-unified-agent/plugins/common/tls"
"github.com/circonus-labs/circonus-unified-agent/plugins/inputs"
"github.com/circonus-labs/circonus-unified-agent/plugins/parsers/influx"
"github.com/circonus-labs/circonus-unified-agent/selfstat"
)
const (
// defaultMaxBodySize is the default maximum request body size, in bytes.
// if the request body is over this size, we will return an HTTP 413 error.
defaultMaxBodySize = 32 * 1024 * 1024
)
type InfluxDBListener struct {
ServiceAddress string `toml:"service_address"`
port int
tlsint.ServerConfig
ReadTimeout internal.Duration `toml:"read_timeout"`
WriteTimeout internal.Duration `toml:"write_timeout"`
MaxBodySize internal.Size `toml:"max_body_size"`
MaxLineSize internal.Size `toml:"max_line_size"` // deprecated in 1.14; ignored
BasicUsername string `toml:"basic_username"`
BasicPassword string `toml:"basic_password"`
DatabaseTag string `toml:"database_tag"`
RetentionPolicyTag string `toml:"retention_policy_tag"`
timeFunc influx.TimeFunc
listener net.Listener
server http.Server
acc cua.Accumulator
bytesRecv selfstat.Stat
requestsServed selfstat.Stat
writesServed selfstat.Stat
queriesServed selfstat.Stat
pingsServed selfstat.Stat
requestsRecv selfstat.Stat
notFoundsServed selfstat.Stat
buffersCreated selfstat.Stat
authFailures selfstat.Stat
Log cua.Logger `toml:"-"`
mux http.ServeMux
}
const sampleConfig = `
instance_id = "" # unique instance identifier (REQUIRED)
## Address and port to host InfluxDB listener on
service_address = ":8186"
## maximum duration before timing out read of the request
read_timeout = "10s"
## maximum duration before timing out write of the response
write_timeout = "10s"
## Maximum allowed HTTP request body size in bytes.
## 0 means to use the default of 32MiB.
max_body_size = "32MiB"
## Optional tag name used to store the database.
## If the write has a database in the query string then it will be kept in this tag name.
## This tag can be used in downstream outputs.
## The default value of nothing means it will be off and the database will not be recorded.
# database_tag = ""
## If set the retention policy specified in the write query will be added as
## the value of this tag name.
# retention_policy_tag = ""
## Set one or more allowed client CA certificate file names to
## enable mutually authenticated TLS connections
tls_allowed_cacerts = ["/etc/circonus-unified-agent/clientca.pem"]
## Add service certificate and key
tls_cert = "/etc/circonus-unified-agent/cert.pem"
tls_key = "/etc/circonus-unified-agent/key.pem"
## Optional username and password to accept for HTTP basic authentication.
## You probably want to make sure you have TLS configured above for this.
# basic_username = "foobar"
# basic_password = "barfoo"
`
func (h *InfluxDBListener) SampleConfig() string {
return sampleConfig
}
func (h *InfluxDBListener) Description() string {
return "Accept metrics over InfluxDB 1.x HTTP API"
}
func (h *InfluxDBListener) Gather(_ context.Context, _ cua.Accumulator) error {
return nil
}
func (h *InfluxDBListener) routes() {
authHandler := internal.AuthHandler(h.BasicUsername, h.BasicPassword, "influxdb",
func(_ http.ResponseWriter) {
h.authFailures.Incr(1)
},
)
h.mux.Handle("/write", authHandler(h.handleWrite()))
h.mux.Handle("/query", authHandler(h.handleQuery()))
h.mux.Handle("/ping", h.handlePing())
h.mux.Handle("/", authHandler(h.handleDefault()))
}
func (h *InfluxDBListener) Init() error {
tags := map[string]string{
"address": h.ServiceAddress,
}
h.bytesRecv = selfstat.Register("influxdb_listener", "bytes_received", tags)
h.requestsServed = selfstat.Register("influxdb_listener", "requests_served", tags)
h.writesServed = selfstat.Register("influxdb_listener", "writes_served", tags)
h.queriesServed = selfstat.Register("influxdb_listener", "queries_served", tags)
h.pingsServed = selfstat.Register("influxdb_listener", "pings_served", tags)
h.requestsRecv = selfstat.Register("influxdb_listener", "requests_received", tags)
h.notFoundsServed = selfstat.Register("influxdb_listener", "not_founds_served", tags)
h.buffersCreated = selfstat.Register("influxdb_listener", "buffers_created", tags)
h.authFailures = selfstat.Register("influxdb_listener", "auth_failures", tags)
h.routes()
if h.MaxBodySize.Size == 0 {
h.MaxBodySize.Size = defaultMaxBodySize
}
if h.MaxLineSize.Size != 0 {
h.Log.Warnf("Use of deprecated configuration: 'max_line_size'; parser now handles lines of unlimited length and option is ignored")
}
if h.ReadTimeout.Duration < time.Second {
h.ReadTimeout.Duration = time.Second * 10
}
if h.WriteTimeout.Duration < time.Second {
h.WriteTimeout.Duration = time.Second * 10
}
return nil
}
// Start starts the InfluxDB listener service.
func (h *InfluxDBListener) Start(ctx context.Context, acc cua.Accumulator) error {
h.acc = acc
tlsConf, err := h.ServerConfig.TLSConfig()
if err != nil {
return fmt.Errorf("TLSConfig: %w", err)
}
h.server = http.Server{
Addr: h.ServiceAddress,
Handler: h,
ReadTimeout: h.ReadTimeout.Duration,
WriteTimeout: h.WriteTimeout.Duration,
TLSConfig: tlsConf,
}
var listener net.Listener
if tlsConf != nil {
listener, err = tls.Listen("tcp", h.ServiceAddress, tlsConf)
if err != nil {
return fmt.Errorf("tls listen (%s): %w", h.ServiceAddress, err)
}
} else {
listener, err = net.Listen("tcp", h.ServiceAddress)
if err != nil {
return fmt.Errorf("net listen (%s): %w", h.ServiceAddress, err)
}
}
h.listener = listener
h.port = listener.Addr().(*net.TCPAddr).Port
go func() {
err = h.server.Serve(h.listener)
if !errors.Is(err, http.ErrServerClosed) {
h.Log.Infof("Error serving HTTP on %s", h.ServiceAddress)
}
}()
h.Log.Infof("Started HTTP listener service on %s", h.ServiceAddress)
return nil
}
// Stop cleans up all resources
func (h *InfluxDBListener) Stop() {
err := h.server.Shutdown(context.Background())
if err != nil {
h.Log.Infof("Error shutting down HTTP server: %v", err.Error())
}
}
func (h *InfluxDBListener) ServeHTTP(res http.ResponseWriter, req *http.Request) {
h.requestsRecv.Incr(1)
h.mux.ServeHTTP(res, req)
h.requestsServed.Incr(1)
}
func (h *InfluxDBListener) handleQuery() http.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
defer h.queriesServed.Incr(1)
// Deliver a dummy response to the query endpoint, as some InfluxDB
// clients test endpoint availability with a query
res.Header().Set("Content-Type", "application/json")
res.Header().Set("X-Influxdb-Version", "1.0")
res.WriteHeader(http.StatusOK)
_, _ = res.Write([]byte("{\"results\":[]}"))
}
}
func (h *InfluxDBListener) handlePing() http.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
defer h.pingsServed.Incr(1)
verbose := req.URL.Query().Get("verbose")
// respond to ping requests
res.Header().Set("X-Influxdb-Version", "1.0")
if verbose != "" && verbose != "0" && verbose != "false" {
res.Header().Set("Content-Type", "application/json")
res.WriteHeader(http.StatusOK)
b, _ := json.Marshal(map[string]string{"version": "1.0"}) // based on header set above
_, _ = res.Write(b)
} else {
res.WriteHeader(http.StatusNoContent)
}
}
}
func (h *InfluxDBListener) handleDefault() http.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
defer h.notFoundsServed.Incr(1)
http.NotFound(res, req)
}
}
func (h *InfluxDBListener) handleWrite() http.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
defer h.writesServed.Incr(1)
// Check that the content length is not too large for us to handle.
if req.ContentLength > h.MaxBodySize.Size {
tooLarge(res)
return
}
db := req.URL.Query().Get("db")
rp := req.URL.Query().Get("rp")
body := req.Body
body = http.MaxBytesReader(res, body, h.MaxBodySize.Size)
// Handle gzip request bodies
if req.Header.Get("Content-Encoding") == "gzip" {
var err error
body, err = gzip.NewReader(body)
if err != nil {
h.Log.Debugf("Error decompressing request body: %v", err.Error())
badRequest(res, err.Error())
return
}
defer body.Close()
}
parser := influx.NewStreamParser(body)
parser.SetTimeFunc(h.timeFunc)
precisionStr := req.URL.Query().Get("precision")
if precisionStr != "" {
precision := getPrecisionMultiplier(precisionStr)
parser.SetTimePrecision(precision)
}
var m cua.Metric
var err error
var parseErrorCount int
var lastPos int = 0
var firstParseErrorStr string
for {
select {
case <-req.Context().Done():
// Shutting down before parsing is finished.
res.WriteHeader(http.StatusServiceUnavailable)
return
default:
}
m, err = parser.Next()
pos := parser.Position()
h.bytesRecv.Incr(int64(pos - lastPos))
lastPos = pos
// Continue parsing metrics even if some are malformed
var perr *influx.ParseError
if errors.As(err, &perr) {
parseErrorCount++
errStr := perr.Error()
if firstParseErrorStr == "" {
firstParseErrorStr = errStr
}
continue
} else if err != nil {
// Either we're exiting cleanly (err ==
// influx.EOF) or there's an unexpected error
break
}
if h.DatabaseTag != "" && db != "" {
m.AddTag(h.DatabaseTag, db)
}
if h.RetentionPolicyTag != "" && rp != "" {
m.AddTag(h.RetentionPolicyTag, rp)
}
h.acc.AddMetric(m)
}
if !errors.Is(err, influx.EOF) {
h.Log.Debugf("Error parsing the request body: %v", err.Error())
badRequest(res, err.Error())
return
}
if parseErrorCount > 0 {
var partialErrorString string
switch parseErrorCount {
case 1:
partialErrorString = firstParseErrorStr
case 2:
partialErrorString = fmt.Sprintf("%s (and 1 other parse error)", firstParseErrorStr)
default:
partialErrorString = fmt.Sprintf("%s (and %d other parse errors)", firstParseErrorStr, parseErrorCount-1)
}
partialWrite(res, partialErrorString)
return
}
// http request success
res.WriteHeader(http.StatusNoContent)
}
}
func tooLarge(res http.ResponseWriter) {
res.Header().Set("Content-Type", "application/json")
res.Header().Set("X-Influxdb-Version", "1.0")
res.Header().Set("X-Influxdb-Error", "http: request body too large")
res.WriteHeader(http.StatusRequestEntityTooLarge)
_, _ = res.Write([]byte(`{"error":"http: request body too large"}`))
}
func badRequest(res http.ResponseWriter, errString string) {
res.Header().Set("Content-Type", "application/json")
res.Header().Set("X-Influxdb-Version", "1.0")
if errString == "" {
errString = "http: bad request"
}
res.Header().Set("X-Influxdb-Error", errString)
res.WriteHeader(http.StatusBadRequest)
_, _ = res.Write([]byte(fmt.Sprintf(`{"error":%q}`, errString)))
}
func partialWrite(res http.ResponseWriter, errString string) {
res.Header().Set("Content-Type", "application/json")
res.Header().Set("X-Influxdb-Version", "1.0")
res.Header().Set("X-Influxdb-Error", errString)
res.WriteHeader(http.StatusBadRequest)
_, _ = res.Write([]byte(fmt.Sprintf(`{"error":%q}`, errString)))
}
func getPrecisionMultiplier(precision string) time.Duration {
// Influxdb defaults silently to nanoseconds if precision isn't
// one of the following:
var d time.Duration
switch precision {
case "u":
d = time.Microsecond
case "ms":
d = time.Millisecond
case "s":
d = time.Second
case "m":
d = time.Minute
case "h":
d = time.Hour
default:
d = time.Nanosecond
}
return d
}
func init() {
// http_listener deprecated in 1.9
inputs.Add("http_listener", func() cua.Input {
return &InfluxDBListener{
ServiceAddress: ":8186",
timeFunc: time.Now,
}
})
inputs.Add("influxdb_listener", func() cua.Input {
return &InfluxDBListener{
ServiceAddress: ":8186",
timeFunc: time.Now,
}
})
}