2019-12-09 18:58:19 +00:00
|
|
|
package vmimport
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vminsert/netstorage"
|
2020-07-23 10:33:10 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vminsert/relabel"
|
2019-12-09 18:58:19 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/auth"
|
2020-09-26 01:13:10 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
2020-09-02 16:41:12 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
|
|
|
|
parserCommon "github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/common"
|
2020-02-23 11:35:47 +00:00
|
|
|
parser "github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/vmimport"
|
2019-12-09 18:58:19 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/storage"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/tenantmetrics"
|
2020-02-23 11:35:47 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/writeconcurrencylimiter"
|
2019-12-09 18:58:19 +00:00
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
rowsInserted = tenantmetrics.NewCounterMap(`vm_rows_inserted_total{type="vmimport"}`)
|
2020-02-23 11:35:47 +00:00
|
|
|
rowsPerInsert = metrics.NewHistogram(`vm_rows_per_insert{type="vmimport"}`)
|
2019-12-09 18:58:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// InsertHandler processes `/api/v1/import` request.
|
|
|
|
//
|
|
|
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/6
|
|
|
|
func InsertHandler(at *auth.Token, req *http.Request) error {
|
2020-09-02 16:41:12 +00:00
|
|
|
extraLabels, err := parserCommon.GetExtraLabels(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-23 11:35:47 +00:00
|
|
|
return writeconcurrencylimiter.Do(func() error {
|
|
|
|
return parser.ParseStream(req, func(rows []parser.Row) error {
|
2020-09-02 16:41:12 +00:00
|
|
|
return insertRows(at, rows, extraLabels)
|
2020-02-23 11:35:47 +00:00
|
|
|
})
|
2019-12-09 18:58:19 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-02 16:41:12 +00:00
|
|
|
func insertRows(at *auth.Token, rows []parser.Row, extraLabels []prompbmarshal.Label) error {
|
2020-02-23 11:35:47 +00:00
|
|
|
ctx := netstorage.GetInsertCtx()
|
|
|
|
defer netstorage.PutInsertCtx(ctx)
|
2019-12-09 18:58:19 +00:00
|
|
|
|
2020-02-26 19:17:35 +00:00
|
|
|
ctx.Reset() // This line is required for initializing ctx internals.
|
2019-12-09 18:58:19 +00:00
|
|
|
rowsTotal := 0
|
2020-07-23 10:33:10 +00:00
|
|
|
hasRelabeling := relabel.HasRelabeling()
|
2019-12-09 18:58:19 +00:00
|
|
|
for i := range rows {
|
|
|
|
r := &rows[i]
|
2020-10-09 10:29:27 +00:00
|
|
|
rowsTotal += len(r.Values)
|
2020-02-23 11:35:47 +00:00
|
|
|
ctx.Labels = ctx.Labels[:0]
|
2019-12-09 18:58:19 +00:00
|
|
|
for j := range r.Tags {
|
|
|
|
tag := &r.Tags[j]
|
2020-02-23 11:35:47 +00:00
|
|
|
ctx.AddLabelBytes(tag.Key, tag.Value)
|
2019-12-09 18:58:19 +00:00
|
|
|
}
|
2020-09-02 16:41:12 +00:00
|
|
|
for j := range extraLabels {
|
|
|
|
label := &extraLabels[j]
|
|
|
|
ctx.AddLabel(label.Name, label.Value)
|
|
|
|
}
|
2020-07-23 10:33:10 +00:00
|
|
|
if hasRelabeling {
|
|
|
|
ctx.ApplyRelabeling()
|
|
|
|
}
|
2020-07-02 16:42:12 +00:00
|
|
|
if len(ctx.Labels) == 0 {
|
|
|
|
// Skip metric without labels.
|
|
|
|
continue
|
|
|
|
}
|
2020-02-23 11:35:47 +00:00
|
|
|
ctx.MetricNameBuf = storage.MarshalMetricNameRaw(ctx.MetricNameBuf[:0], at.AccountID, at.ProjectID, ctx.Labels)
|
|
|
|
storageNodeIdx := ctx.GetStorageNodeIdx(at, ctx.Labels)
|
2019-12-09 18:58:19 +00:00
|
|
|
values := r.Values
|
|
|
|
timestamps := r.Timestamps
|
2020-09-26 01:13:10 +00:00
|
|
|
if len(timestamps) != len(values) {
|
|
|
|
logger.Panicf("BUG: len(timestamps)=%d must match len(values)=%d", len(timestamps), len(values))
|
|
|
|
}
|
2019-12-09 18:58:19 +00:00
|
|
|
for j, value := range values {
|
|
|
|
timestamp := timestamps[j]
|
2020-02-23 11:35:47 +00:00
|
|
|
if err := ctx.WriteDataPointExt(at, storageNodeIdx, ctx.MetricNameBuf, timestamp, value); err != nil {
|
2019-12-09 18:58:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rowsInserted.Get(at).Add(rowsTotal)
|
|
|
|
rowsPerInsert.Update(float64(rowsTotal))
|
2020-02-23 11:35:47 +00:00
|
|
|
return ctx.FlushBufs()
|
2019-12-09 18:58:19 +00:00
|
|
|
}
|