2020-09-26 01:29:45 +00:00
|
|
|
package native
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmagent/common"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmagent/remotewrite"
|
2021-08-05 06:44:29 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/auth"
|
2020-09-26 01:29:45 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
|
|
|
|
parserCommon "github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/common"
|
|
|
|
parser "github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/native"
|
2021-08-05 06:44:29 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/tenantmetrics"
|
2020-09-26 01:29:45 +00:00
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-08-05 06:44:29 +00:00
|
|
|
rowsInserted = metrics.NewCounter(`vmagent_rows_inserted_total{type="native"}`)
|
2021-08-05 06:46:19 +00:00
|
|
|
rowsTenantInserted = tenantmetrics.NewCounterMap(`vmagent_tenant_inserted_rows_total{type="native"}`)
|
2021-08-05 06:44:29 +00:00
|
|
|
rowsPerInsert = metrics.NewHistogram(`vmagent_rows_per_insert{type="native"}`)
|
2020-09-26 01:29:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// InsertHandler processes `/api/v1/import` request.
|
|
|
|
//
|
|
|
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/6
|
2021-08-05 06:46:19 +00:00
|
|
|
func InsertHandler(at *auth.Token, req *http.Request) error {
|
2020-09-26 01:29:45 +00:00
|
|
|
extraLabels, err := parserCommon.GetExtraLabels(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-05 14:01:32 +00:00
|
|
|
isGzip := req.Header.Get("Content-Encoding") == "gzip"
|
2023-01-07 02:59:39 +00:00
|
|
|
return parser.ParseStream(req.Body, isGzip, func(block *parser.Block) error {
|
|
|
|
return insertRows(at, block, extraLabels)
|
2020-09-26 01:29:45 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-05 06:46:19 +00:00
|
|
|
func insertRows(at *auth.Token, block *parser.Block, extraLabels []prompbmarshal.Label) error {
|
2020-09-26 01:29:45 +00:00
|
|
|
ctx := common.GetPushCtx()
|
|
|
|
defer common.PutPushCtx(ctx)
|
|
|
|
|
2020-10-09 10:29:27 +00:00
|
|
|
// Update rowsInserted and rowsPerInsert before actual inserting,
|
|
|
|
// since relabeling can prevent from inserting the rows.
|
|
|
|
rowsLen := len(block.Values)
|
|
|
|
rowsInserted.Add(rowsLen)
|
2021-08-05 06:46:19 +00:00
|
|
|
if at != nil {
|
|
|
|
rowsTenantInserted.Get(at).Add(rowsLen)
|
2021-08-05 06:44:29 +00:00
|
|
|
}
|
2020-10-09 10:29:27 +00:00
|
|
|
rowsPerInsert.Update(float64(rowsLen))
|
|
|
|
|
2020-09-26 01:29:45 +00:00
|
|
|
tssDst := ctx.WriteRequest.Timeseries[:0]
|
|
|
|
labels := ctx.Labels[:0]
|
|
|
|
samples := ctx.Samples[:0]
|
|
|
|
mn := &block.MetricName
|
|
|
|
labelsLen := len(labels)
|
|
|
|
labels = append(labels, prompbmarshal.Label{
|
|
|
|
Name: "__name__",
|
|
|
|
Value: bytesutil.ToUnsafeString(mn.MetricGroup),
|
|
|
|
})
|
|
|
|
for j := range mn.Tags {
|
|
|
|
tag := &mn.Tags[j]
|
|
|
|
labels = append(labels, prompbmarshal.Label{
|
|
|
|
Name: bytesutil.ToUnsafeString(tag.Key),
|
|
|
|
Value: bytesutil.ToUnsafeString(tag.Value),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
labels = append(labels, extraLabels...)
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
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
|
2022-08-08 11:46:24 +00:00
|
|
|
remotewrite.Push(at, &ctx.WriteRequest)
|
2020-09-26 01:29:45 +00:00
|
|
|
return nil
|
|
|
|
}
|