2023-10-05 12:39:51 +00:00
|
|
|
package stream
|
|
|
|
|
|
|
|
import (
|
2023-10-15 22:25:23 +00:00
|
|
|
"bufio"
|
2023-10-05 12:39:51 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2023-10-15 22:25:23 +00:00
|
|
|
"sync"
|
2023-10-05 12:39:51 +00:00
|
|
|
|
2023-10-15 22:25:23 +00:00
|
|
|
"github.com/VictoriaMetrics/metrics"
|
2023-10-05 12:39:51 +00:00
|
|
|
|
2023-10-15 22:25:23 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/cgroup"
|
2023-10-05 12:39:51 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fasttime"
|
2023-10-15 22:25:23 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/flagutil"
|
2023-10-05 12:39:51 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/common"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/newrelic"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/writeconcurrencylimiter"
|
|
|
|
)
|
|
|
|
|
2023-10-15 22:25:23 +00:00
|
|
|
var (
|
|
|
|
maxInsertRequestSize = flagutil.NewBytes("newrelic.maxInsertRequestSize", 64*1024*1024, "The maximum size in bytes of a single NewRelic request "+
|
|
|
|
"to /newrelic/infra/v2/metrics/events/bulk")
|
|
|
|
)
|
2023-10-05 12:39:51 +00:00
|
|
|
|
2023-10-15 22:25:23 +00:00
|
|
|
// Parse parses NewRelic POST request for /newrelic/infra/v2/metrics/events/bulk from r and calls callback for the parsed request.
|
2023-10-05 12:39:51 +00:00
|
|
|
//
|
2023-10-15 22:25:23 +00:00
|
|
|
// callback shouldn't hold rows after returning.
|
|
|
|
func Parse(r io.Reader, isGzip bool, callback func(rows []newrelic.Row) error) error {
|
2023-10-05 12:39:51 +00:00
|
|
|
wcr := writeconcurrencylimiter.GetReader(r)
|
|
|
|
defer writeconcurrencylimiter.PutReader(wcr)
|
|
|
|
r = wcr
|
|
|
|
|
|
|
|
if isGzip {
|
|
|
|
zr, err := common.GetGzipReader(r)
|
|
|
|
if err != nil {
|
2023-10-15 22:25:23 +00:00
|
|
|
return fmt.Errorf("cannot read gzipped NewRelic agent data: %w", err)
|
2023-10-05 12:39:51 +00:00
|
|
|
}
|
|
|
|
defer common.PutGzipReader(zr)
|
|
|
|
r = zr
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := getPushCtx(r)
|
|
|
|
defer putPushCtx(ctx)
|
|
|
|
if err := ctx.Read(); err != nil {
|
2023-10-15 22:25:23 +00:00
|
|
|
return fmt.Errorf("cannot read NewRelic request: %w", err)
|
2023-10-05 12:39:51 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 22:25:23 +00:00
|
|
|
rows := getRows()
|
|
|
|
defer putRows(rows)
|
2023-10-05 12:39:51 +00:00
|
|
|
|
2023-10-15 22:25:23 +00:00
|
|
|
if err := rows.Unmarshal(ctx.reqBuf.B); err != nil {
|
2023-10-05 12:39:51 +00:00
|
|
|
unmarshalErrors.Inc()
|
2023-10-15 22:25:23 +00:00
|
|
|
return fmt.Errorf("cannot unmarshal NewRelic request: %w", err)
|
2023-10-05 12:39:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fill in missing timestamps
|
|
|
|
currentTimestamp := int64(fasttime.UnixTimestamp())
|
2023-10-15 22:25:23 +00:00
|
|
|
for i := range rows.Rows {
|
|
|
|
r := &rows.Rows[i]
|
|
|
|
if r.Timestamp == 0 {
|
|
|
|
r.Timestamp = currentTimestamp * 1e3
|
2023-10-05 12:39:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-15 22:25:23 +00:00
|
|
|
if err := callback(rows.Rows); err != nil {
|
2023-10-05 12:39:51 +00:00
|
|
|
return fmt.Errorf("error when processing imported data: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-10-15 22:25:23 +00:00
|
|
|
|
|
|
|
func getRows() *newrelic.Rows {
|
|
|
|
v := rowsPool.Get()
|
|
|
|
if v == nil {
|
|
|
|
return &newrelic.Rows{}
|
|
|
|
}
|
|
|
|
return v.(*newrelic.Rows)
|
|
|
|
}
|
|
|
|
|
|
|
|
func putRows(rows *newrelic.Rows) {
|
|
|
|
rows.Reset()
|
|
|
|
rowsPool.Put(rows)
|
|
|
|
}
|
|
|
|
|
|
|
|
var rowsPool sync.Pool
|
|
|
|
|
|
|
|
var (
|
|
|
|
readCalls = metrics.NewCounter(`vm_protoparser_read_calls_total{type="newrelic"}`)
|
|
|
|
readErrors = metrics.NewCounter(`vm_protoparser_read_errors_total{type="newrelic"}`)
|
|
|
|
unmarshalErrors = metrics.NewCounter(`vm_protoparser_unmarshal_errors_total{type="newrelic"}`)
|
|
|
|
)
|
|
|
|
|
|
|
|
var pushCtxPool sync.Pool
|
|
|
|
var pushCtxPoolCh = make(chan *pushCtx, cgroup.AvailableCPUs())
|
|
|
|
|
|
|
|
type pushCtx struct {
|
|
|
|
br *bufio.Reader
|
|
|
|
reqBuf bytesutil.ByteBuffer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *pushCtx) Read() error {
|
|
|
|
readCalls.Inc()
|
|
|
|
lr := io.LimitReader(ctx.br, maxInsertRequestSize.N+1)
|
|
|
|
startTime := fasttime.UnixTimestamp()
|
|
|
|
reqLen, err := ctx.reqBuf.ReadFrom(lr)
|
|
|
|
if err != nil {
|
|
|
|
readErrors.Inc()
|
|
|
|
return fmt.Errorf("cannot read request in %d seconds: %w", fasttime.UnixTimestamp()-startTime, err)
|
|
|
|
}
|
|
|
|
if reqLen > maxInsertRequestSize.N {
|
|
|
|
readErrors.Inc()
|
|
|
|
return fmt.Errorf("too big request; mustn't exceed -newrelic.maxInsertRequestSize=%d bytes", maxInsertRequestSize.N)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *pushCtx) reset() {
|
|
|
|
ctx.br.Reset(nil)
|
|
|
|
ctx.reqBuf.Reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|