2023-02-13 17:51:35 +00:00
|
|
|
package stream
|
2021-09-28 19:47:45 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/cgroup"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fasttime"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/common"
|
2023-12-21 16:29:10 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/datadogutils"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/datadogv1"
|
2023-01-07 02:59:39 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/writeconcurrencylimiter"
|
2021-09-28 19:47:45 +00:00
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
)
|
|
|
|
|
2023-12-05 00:30:40 +00:00
|
|
|
// Parse parses DataDog POST request for /api/v1/series from reader and calls callback for the parsed request.
|
2021-09-28 19:47:45 +00:00
|
|
|
//
|
|
|
|
// callback shouldn't hold series after returning.
|
2023-12-21 16:29:10 +00:00
|
|
|
func Parse(r io.Reader, contentEncoding string, callback func(series []datadogv1.Series) error) error {
|
2023-12-05 00:30:40 +00:00
|
|
|
wcr := writeconcurrencylimiter.GetReader(r)
|
2023-01-07 02:59:39 +00:00
|
|
|
defer writeconcurrencylimiter.PutReader(wcr)
|
|
|
|
r = wcr
|
|
|
|
|
2021-09-28 19:47:45 +00:00
|
|
|
switch contentEncoding {
|
|
|
|
case "gzip":
|
|
|
|
zr, err := common.GetGzipReader(r)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot read gzipped DataDog data: %w", err)
|
|
|
|
}
|
|
|
|
defer common.PutGzipReader(zr)
|
|
|
|
r = zr
|
|
|
|
case "deflate":
|
|
|
|
zlr, err := common.GetZlibReader(r)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot read deflated DataDog data: %w", err)
|
|
|
|
}
|
|
|
|
defer common.PutZlibReader(zlr)
|
|
|
|
r = zlr
|
|
|
|
}
|
|
|
|
ctx := getPushCtx(r)
|
|
|
|
defer putPushCtx(ctx)
|
|
|
|
if err := ctx.Read(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-12-05 00:30:40 +00:00
|
|
|
req := getRequest()
|
|
|
|
defer putRequest(req)
|
|
|
|
if err := req.Unmarshal(ctx.reqBuf.B); err != nil {
|
2021-09-28 19:47:45 +00:00
|
|
|
unmarshalErrors.Inc()
|
2023-10-25 19:24:01 +00:00
|
|
|
return fmt.Errorf("cannot unmarshal DataDog POST request with size %d bytes: %w", len(ctx.reqBuf.B), err)
|
2021-09-28 19:47:45 +00:00
|
|
|
}
|
2023-12-05 00:30:40 +00:00
|
|
|
rows := 0
|
|
|
|
series := req.Series
|
|
|
|
for i := range series {
|
|
|
|
rows += len(series[i].Points)
|
2023-12-21 16:29:10 +00:00
|
|
|
if *datadogutils.SanitizeMetricName {
|
|
|
|
series[i].Metric = datadogutils.SanitizeName(series[i].Metric)
|
2023-12-05 00:30:40 +00:00
|
|
|
}
|
2021-09-28 19:47:45 +00:00
|
|
|
}
|
2023-12-05 00:30:40 +00:00
|
|
|
rowsRead.Add(rows)
|
2021-09-28 19:47:45 +00:00
|
|
|
|
2023-12-05 00:30:40 +00:00
|
|
|
if err := callback(series); err != nil {
|
2021-09-28 19:47:45 +00:00
|
|
|
return fmt.Errorf("error when processing imported data: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type pushCtx struct {
|
|
|
|
br *bufio.Reader
|
|
|
|
reqBuf bytesutil.ByteBuffer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *pushCtx) reset() {
|
|
|
|
ctx.br.Reset(nil)
|
|
|
|
ctx.reqBuf.Reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *pushCtx) Read() error {
|
|
|
|
readCalls.Inc()
|
2023-12-21 16:29:10 +00:00
|
|
|
lr := io.LimitReader(ctx.br, int64(datadogutils.MaxInsertRequestSize.N)+1)
|
2021-09-28 19:47:45 +00:00
|
|
|
startTime := fasttime.UnixTimestamp()
|
|
|
|
reqLen, err := ctx.reqBuf.ReadFrom(lr)
|
|
|
|
if err != nil {
|
|
|
|
readErrors.Inc()
|
2023-10-15 22:25:23 +00:00
|
|
|
return fmt.Errorf("cannot read request in %d seconds: %w", fasttime.UnixTimestamp()-startTime, err)
|
2021-09-28 19:47:45 +00:00
|
|
|
}
|
2023-12-21 16:29:10 +00:00
|
|
|
if reqLen > int64(datadogutils.MaxInsertRequestSize.N) {
|
2021-09-28 19:47:45 +00:00
|
|
|
readErrors.Inc()
|
2023-12-21 16:29:10 +00:00
|
|
|
return fmt.Errorf("too big request; mustn't exceed -datadog.maxInsertRequestSize=%d bytes", datadogutils.MaxInsertRequestSize.N)
|
2021-09-28 19:47:45 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2023-12-21 16:29:10 +00:00
|
|
|
readCalls = metrics.NewCounter(`vm_protoparser_read_calls_total{type="datadogv1"}`)
|
|
|
|
readErrors = metrics.NewCounter(`vm_protoparser_read_errors_total{type="datadogv1"}`)
|
|
|
|
rowsRead = metrics.NewCounter(`vm_protoparser_rows_read_total{type="datadogv1"}`)
|
|
|
|
unmarshalErrors = metrics.NewCounter(`vm_protoparser_unmarshal_errors_total{type="datadogv1"}`)
|
2021-09-28 19:47:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func getPushCtx(r io.Reader) *pushCtx {
|
|
|
|
select {
|
|
|
|
case ctx := <-pushCtxPoolCh:
|
|
|
|
ctx.br.Reset(r)
|
|
|
|
return ctx
|
|
|
|
default:
|
|
|
|
if v := pushCtxPool.Get(); v != nil {
|
|
|
|
ctx := v.(*pushCtx)
|
|
|
|
ctx.br.Reset(r)
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
return &pushCtx{
|
|
|
|
br: bufio.NewReaderSize(r, 64*1024),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func putPushCtx(ctx *pushCtx) {
|
|
|
|
ctx.reset()
|
|
|
|
select {
|
|
|
|
case pushCtxPoolCh <- ctx:
|
|
|
|
default:
|
|
|
|
pushCtxPool.Put(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var pushCtxPool sync.Pool
|
|
|
|
var pushCtxPoolCh = make(chan *pushCtx, cgroup.AvailableCPUs())
|
|
|
|
|
2023-12-21 16:29:10 +00:00
|
|
|
func getRequest() *datadogv1.Request {
|
2023-12-05 00:29:00 +00:00
|
|
|
v := requestPool.Get()
|
2021-09-28 19:47:45 +00:00
|
|
|
if v == nil {
|
2023-12-21 16:29:10 +00:00
|
|
|
return &datadogv1.Request{}
|
2021-09-28 19:47:45 +00:00
|
|
|
}
|
2023-12-21 16:29:10 +00:00
|
|
|
return v.(*datadogv1.Request)
|
2021-09-28 19:47:45 +00:00
|
|
|
}
|
|
|
|
|
2023-12-21 16:29:10 +00:00
|
|
|
func putRequest(req *datadogv1.Request) {
|
2023-12-05 00:29:00 +00:00
|
|
|
requestPool.Put(req)
|
2021-09-28 19:47:45 +00:00
|
|
|
}
|
|
|
|
|
2023-12-05 00:29:00 +00:00
|
|
|
var requestPool sync.Pool
|