2020-09-26 04:29:45 +03:00
|
|
|
package native
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vminsert/common"
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vminsert/relabel"
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
2025-07-29 14:48:55 +03:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompb"
|
2023-02-13 10:42:58 -08:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/native/stream"
|
2025-03-18 16:24:48 +01:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/protoparserutil"
|
2020-09-26 04:29:45 +03:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/storage"
|
|
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
rowsInserted = metrics.NewCounter(`vm_rows_inserted_total{type="native"}`)
|
|
|
|
|
rowsPerInsert = metrics.NewHistogram(`vm_rows_per_insert{type="native"}`)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// InsertHandler processes `/api/v1/import/native` request.
|
|
|
|
|
func InsertHandler(req *http.Request) error {
|
2025-03-18 16:24:48 +01:00
|
|
|
extraLabels, err := protoparserutil.GetExtraLabels(req)
|
2020-09-26 04:29:45 +03:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2025-03-14 18:10:37 +01:00
|
|
|
encoding := req.Header.Get("Content-Encoding")
|
|
|
|
|
return stream.Parse(req.Body, encoding, func(block *stream.Block) error {
|
2023-01-06 18:59:39 -08:00
|
|
|
return insertRows(block, extraLabels)
|
2020-09-26 04:29:45 +03:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-29 14:48:55 +03:00
|
|
|
func insertRows(block *stream.Block, extraLabels []prompb.Label) error {
|
2020-09-26 04:29:45 +03:00
|
|
|
ctx := getPushCtx()
|
|
|
|
|
defer putPushCtx(ctx)
|
|
|
|
|
|
2020-10-09 13:29:27 +03:00
|
|
|
// Update rowsInserted and rowsPerInsert before actual inserting,
|
|
|
|
|
// since relabeling can prevent from inserting the rows.
|
2020-09-26 04:29:45 +03:00
|
|
|
rowsLen := len(block.Values)
|
2020-10-09 13:29:27 +03:00
|
|
|
rowsInserted.Add(rowsLen)
|
|
|
|
|
rowsPerInsert.Update(float64(rowsLen))
|
|
|
|
|
|
2020-09-26 04:29:45 +03:00
|
|
|
ic := &ctx.Common
|
|
|
|
|
ic.Reset(rowsLen)
|
|
|
|
|
hasRelabeling := relabel.HasRelabeling()
|
|
|
|
|
mn := &block.MetricName
|
|
|
|
|
ic.Labels = ic.Labels[:0]
|
|
|
|
|
ic.AddLabelBytes(nil, mn.MetricGroup)
|
|
|
|
|
for j := range mn.Tags {
|
|
|
|
|
tag := &mn.Tags[j]
|
|
|
|
|
ic.AddLabelBytes(tag.Key, tag.Value)
|
|
|
|
|
}
|
|
|
|
|
for j := range extraLabels {
|
|
|
|
|
label := &extraLabels[j]
|
|
|
|
|
ic.AddLabel(label.Name, label.Value)
|
|
|
|
|
}
|
2024-12-10 22:19:16 +02:00
|
|
|
if !ic.TryPrepareLabels(hasRelabeling) {
|
2020-09-26 04:29:45 +03:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
ctx.metricNameBuf = storage.MarshalMetricNameRaw(ctx.metricNameBuf[:0], ic.Labels)
|
|
|
|
|
values := block.Values
|
|
|
|
|
timestamps := block.Timestamps
|
|
|
|
|
if len(timestamps) != len(values) {
|
|
|
|
|
logger.Panicf("BUG: len(timestamps)=%d must match len(values)=%d", len(timestamps), len(values))
|
|
|
|
|
}
|
|
|
|
|
for j, value := range values {
|
|
|
|
|
timestamp := timestamps[j]
|
2024-12-10 22:19:16 +02:00
|
|
|
// TODO: @f41gh7 looks like it's better to use WriteDataPointExt
|
|
|
|
|
// since metricName never changes inside insertRows call
|
2024-12-18 22:27:51 +01:00
|
|
|
if err := ic.WriteDataPoint(ctx.metricNameBuf, ic.Labels, timestamp, value); err != nil {
|
2020-09-26 04:29:45 +03:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ic.FlushBufs()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type pushCtx struct {
|
|
|
|
|
Common common.InsertCtx
|
|
|
|
|
metricNameBuf []byte
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ctx *pushCtx) reset() {
|
|
|
|
|
ctx.Common.Reset(0)
|
|
|
|
|
ctx.metricNameBuf = ctx.metricNameBuf[:0]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getPushCtx() *pushCtx {
|
2024-04-20 21:44:16 +02:00
|
|
|
if v := pushCtxPool.Get(); v != nil {
|
|
|
|
|
return v.(*pushCtx)
|
2020-09-26 04:29:45 +03:00
|
|
|
}
|
2024-04-20 21:44:16 +02:00
|
|
|
return &pushCtx{}
|
2020-09-26 04:29:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func putPushCtx(ctx *pushCtx) {
|
|
|
|
|
ctx.reset()
|
2024-04-20 21:44:16 +02:00
|
|
|
pushCtxPool.Put(ctx)
|
2020-09-26 04:29:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var pushCtxPool sync.Pool
|