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/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/datadogv2"
|
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-21 16:29:10 +00:00
|
|
|
// Parse parses DataDog POST request for /api/v2/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, contentType string, callback func(series []datadogv2.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
|
|
|
|
}
|
2023-12-21 16:29:10 +00:00
|
|
|
|
2021-09-28 19:47:45 +00:00
|
|
|
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)
|
2023-12-21 16:29:10 +00:00
|
|
|
|
|
|
|
var err error
|
|
|
|
switch contentType {
|
|
|
|
case "application/x-protobuf":
|
|
|
|
err = datadogv2.UnmarshalProtobuf(req, ctx.reqBuf.B)
|
|
|
|
default:
|
|
|
|
err = datadogv2.UnmarshalJSON(req, ctx.reqBuf.B)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2021-09-28 19:47:45 +00:00
|
|
|
unmarshalErrors.Inc()
|
2023-12-21 16:29:10 +00:00
|
|
|
return fmt.Errorf("cannot unmarshal DataDog %s request with size %d bytes: %w", contentType, len(ctx.reqBuf.B), err)
|
2021-09-28 19:47:45 +00:00
|
|
|
}
|
2023-12-21 16:29:10 +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="datadogv2"}`)
|
|
|
|
readErrors = metrics.NewCounter(`vm_protoparser_read_errors_total{type="datadogv2"}`)
|
|
|
|
rowsRead = metrics.NewCounter(`vm_protoparser_rows_read_total{type="datadogv2"}`)
|
|
|
|
unmarshalErrors = metrics.NewCounter(`vm_protoparser_unmarshal_errors_total{type="datadogv2"}`)
|
2021-09-28 19:47:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func getPushCtx(r io.Reader) *pushCtx {
|
2024-04-20 19:59:49 +00:00
|
|
|
if v := pushCtxPool.Get(); v != nil {
|
|
|
|
ctx := v.(*pushCtx)
|
2021-09-28 19:47:45 +00:00
|
|
|
ctx.br.Reset(r)
|
|
|
|
return ctx
|
2024-04-20 19:59:49 +00:00
|
|
|
}
|
|
|
|
return &pushCtx{
|
|
|
|
br: bufio.NewReaderSize(r, 64*1024),
|
2021-09-28 19:47:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func putPushCtx(ctx *pushCtx) {
|
|
|
|
ctx.reset()
|
2024-04-20 19:59:49 +00:00
|
|
|
pushCtxPool.Put(ctx)
|
2021-09-28 19:47:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var pushCtxPool sync.Pool
|
|
|
|
|
2023-12-21 16:29:10 +00:00
|
|
|
func getRequest() *datadogv2.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 &datadogv2.Request{}
|
2021-09-28 19:47:45 +00:00
|
|
|
}
|
2023-12-21 16:29:10 +00:00
|
|
|
return v.(*datadogv2.Request)
|
2021-09-28 19:47:45 +00:00
|
|
|
}
|
|
|
|
|
2023-12-21 16:29:10 +00:00
|
|
|
func putRequest(req *datadogv2.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
|