2020-02-23 12:35:47 +01:00
|
|
|
package vmimport
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmagent/common"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmagent/remotewrite"
|
2021-08-05 08:44:29 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/auth"
|
2020-02-23 12:35:47 +01:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
2020-09-26 03:29:45 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
2020-02-23 12:35:47 +01:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
|
2020-09-02 18:41:12 +02:00
|
|
|
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"
|
2021-08-05 08:44:29 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/tenantmetrics"
|
2020-02-23 12:35:47 +01:00
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-08-05 08:44:29 +02:00
|
|
|
rowsInserted = metrics.NewCounter(`vmagent_rows_inserted_total{type="vmimport"}`)
|
2021-08-05 08:46:19 +02:00
|
|
|
rowsTenantInserted = tenantmetrics.NewCounterMap(`vmagent_tenant_inserted_rows_total{type="vmimport"}`)
|
2021-08-05 08:44:29 +02:00
|
|
|
rowsPerInsert = metrics.NewHistogram(`vmagent_rows_per_insert{type="vmimport"}`)
|
2020-02-23 12:35:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// InsertHandler processes `/api/v1/import` request.
|
|
|
|
//
|
|
|
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/6
|
2021-08-05 08:46:19 +02:00
|
|
|
func InsertHandler(at *auth.Token, 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(at, rows, extraLabels)
|
2020-02-23 12:35:47 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-05 08:46:19 +02:00
|
|
|
func insertRows(at *auth.Token, rows []parser.Row, extraLabels []prompbmarshal.Label) error {
|
2020-02-23 12:35:47 +01:00
|
|
|
ctx := common.GetPushCtx()
|
|
|
|
defer common.PutPushCtx(ctx)
|
|
|
|
|
|
|
|
rowsTotal := 0
|
|
|
|
tssDst := ctx.WriteRequest.Timeseries[:0]
|
|
|
|
labels := ctx.Labels[:0]
|
|
|
|
samples := ctx.Samples[:0]
|
|
|
|
for i := range rows {
|
|
|
|
r := &rows[i]
|
2020-10-09 12:29:27 +02:00
|
|
|
rowsTotal += len(r.Values)
|
2020-02-23 12:35:47 +01:00
|
|
|
labelsLen := len(labels)
|
|
|
|
for j := range r.Tags {
|
|
|
|
tag := &r.Tags[j]
|
|
|
|
labels = append(labels, prompbmarshal.Label{
|
|
|
|
Name: bytesutil.ToUnsafeString(tag.Key),
|
|
|
|
Value: bytesutil.ToUnsafeString(tag.Value),
|
|
|
|
})
|
|
|
|
}
|
2020-09-02 18:41:12 +02:00
|
|
|
labels = append(labels, extraLabels...)
|
2020-02-23 12:35:47 +01:00
|
|
|
values := r.Values
|
|
|
|
timestamps := r.Timestamps
|
2020-09-26 03:29:45 +02:00
|
|
|
if len(timestamps) != len(values) {
|
|
|
|
logger.Panicf("BUG: len(timestamps)=%d must match len(values)=%d", len(timestamps), len(values))
|
|
|
|
}
|
2020-02-23 12:35:47 +01:00
|
|
|
samplesLen := len(samples)
|
|
|
|
for j, value := range values {
|
|
|
|
samples = append(samples, prompbmarshal.Sample{
|
|
|
|
Value: value,
|
|
|
|
Timestamp: timestamps[j],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
tssDst = append(tssDst, prompbmarshal.TimeSeries{
|
|
|
|
Labels: labels[labelsLen:],
|
|
|
|
Samples: samples[samplesLen:],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
ctx.WriteRequest.Timeseries = tssDst
|
|
|
|
ctx.Labels = labels
|
|
|
|
ctx.Samples = samples
|
2023-11-24 13:42:11 +01:00
|
|
|
if !remotewrite.Push(at, &ctx.WriteRequest) {
|
|
|
|
return remotewrite.ErrQueueFullHTTPRetry
|
|
|
|
}
|
2020-02-23 12:35:47 +01:00
|
|
|
rowsInserted.Add(rowsTotal)
|
2021-08-05 08:46:19 +02:00
|
|
|
if at != nil {
|
|
|
|
rowsTenantInserted.Get(at).Add(rowsTotal)
|
2021-08-05 08:44:29 +02:00
|
|
|
}
|
2020-02-23 12:35:47 +01:00
|
|
|
rowsPerInsert.Update(float64(rowsTotal))
|
|
|
|
return nil
|
|
|
|
}
|