2019-12-09 19:58:19 +01:00
|
|
|
package vmimport
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vminsert/common"
|
2020-07-23 12:33:10 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vminsert/relabel"
|
2020-09-26 03:13:10 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
2020-09-02 18:41:12 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
|
|
|
|
parserCommon "github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/common"
|
2020-02-23 12:35:47 +01:00
|
|
|
parser "github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/vmimport"
|
2023-02-13 19:20:14 +01:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/vmimport/stream"
|
2019-12-09 19:58:19 +01:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/storage"
|
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
rowsInserted = metrics.NewCounter(`vm_rows_inserted_total{type="vmimport"}`)
|
2020-02-23 12:35:47 +01:00
|
|
|
rowsPerInsert = metrics.NewHistogram(`vm_rows_per_insert{type="vmimport"}`)
|
2019-12-09 19:58:19 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// InsertHandler processes `/api/v1/import` request.
|
|
|
|
//
|
|
|
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/6
|
|
|
|
func InsertHandler(req *http.Request) error {
|
2020-09-02 18:41:12 +02:00
|
|
|
extraLabels, err := parserCommon.GetExtraLabels(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-07 03:59:39 +01:00
|
|
|
isGzipped := req.Header.Get("Content-Encoding") == "gzip"
|
2023-02-13 19:20:14 +01:00
|
|
|
return stream.Parse(req.Body, isGzipped, func(rows []parser.Row) error {
|
2023-01-07 03:59:39 +01:00
|
|
|
return insertRows(rows, extraLabels)
|
2019-12-09 19:58:19 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-02 18:41:12 +02:00
|
|
|
func insertRows(rows []parser.Row, extraLabels []prompbmarshal.Label) error {
|
2019-12-09 19:58:19 +01:00
|
|
|
ctx := getPushCtx()
|
|
|
|
defer putPushCtx(ctx)
|
|
|
|
|
|
|
|
rowsLen := 0
|
|
|
|
for i := range rows {
|
|
|
|
rowsLen += len(rows[i].Values)
|
|
|
|
}
|
|
|
|
ic := &ctx.Common
|
|
|
|
ic.Reset(rowsLen)
|
|
|
|
rowsTotal := 0
|
2020-07-23 12:33:10 +02:00
|
|
|
hasRelabeling := relabel.HasRelabeling()
|
2019-12-09 19:58:19 +01:00
|
|
|
for i := range rows {
|
|
|
|
r := &rows[i]
|
2020-10-09 12:29:27 +02:00
|
|
|
rowsTotal += len(r.Values)
|
2019-12-09 19:58:19 +01:00
|
|
|
ic.Labels = ic.Labels[:0]
|
|
|
|
for j := range r.Tags {
|
|
|
|
tag := &r.Tags[j]
|
|
|
|
ic.AddLabelBytes(tag.Key, tag.Value)
|
|
|
|
}
|
2020-09-02 18:41:12 +02:00
|
|
|
for j := range extraLabels {
|
|
|
|
label := &extraLabels[j]
|
|
|
|
ic.AddLabel(label.Name, label.Value)
|
|
|
|
}
|
2020-07-23 12:33:10 +02:00
|
|
|
if hasRelabeling {
|
|
|
|
ic.ApplyRelabeling()
|
|
|
|
}
|
2020-07-02 18:42:12 +02:00
|
|
|
if len(ic.Labels) == 0 {
|
|
|
|
// Skip metric without labels.
|
|
|
|
continue
|
|
|
|
}
|
2021-03-31 22:12:56 +02:00
|
|
|
ic.SortLabelsIfNeeded()
|
2019-12-09 19:58:19 +01:00
|
|
|
ctx.metricNameBuf = storage.MarshalMetricNameRaw(ctx.metricNameBuf[:0], ic.Labels)
|
|
|
|
values := r.Values
|
|
|
|
timestamps := r.Timestamps
|
2020-09-26 03:13:10 +02:00
|
|
|
if len(timestamps) != len(values) {
|
|
|
|
logger.Panicf("BUG: len(timestamps)=%d must match len(values)=%d", len(timestamps), len(values))
|
|
|
|
}
|
2019-12-09 19:58:19 +01:00
|
|
|
for j, value := range values {
|
|
|
|
timestamp := timestamps[j]
|
2020-07-24 22:19:49 +02:00
|
|
|
if err := ic.WriteDataPoint(ctx.metricNameBuf, nil, timestamp, value); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-09 19:58:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
rowsInserted.Add(rowsTotal)
|
|
|
|
rowsPerInsert.Update(float64(rowsTotal))
|
|
|
|
return ic.FlushBufs()
|
|
|
|
}
|
|
|
|
|
|
|
|
type pushCtx struct {
|
2020-02-23 12:35:47 +01:00
|
|
|
Common common.InsertCtx
|
2019-12-09 19:58:19 +01:00
|
|
|
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)
|
2019-12-09 19:58:19 +01:00
|
|
|
}
|
2024-04-20 21:44:16 +02:00
|
|
|
return &pushCtx{}
|
2019-12-09 19:58:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func putPushCtx(ctx *pushCtx) {
|
|
|
|
ctx.reset()
|
2024-04-20 21:44:16 +02:00
|
|
|
pushCtxPool.Put(ctx)
|
2019-12-09 19:58:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var pushCtxPool sync.Pool
|