2020-02-23 11:35:47 +00:00
|
|
|
package promremotewrite
|
|
|
|
|
|
|
|
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-02-23 11:35:47 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompb"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
|
2021-01-12 22:52:50 +00:00
|
|
|
parserCommon "github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/common"
|
2023-02-13 18:46:50 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/promremotewrite/stream"
|
2021-08-05 06:44:29 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/tenantmetrics"
|
2020-02-23 11:35:47 +00:00
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-08-05 06:46:19 +00:00
|
|
|
rowsInserted = metrics.NewCounter(`vmagent_rows_inserted_total{type="promremotewrite"}`)
|
|
|
|
rowsTenantInserted = tenantmetrics.NewCounterMap(`vmagent_tenant_inserted_rows_total{type="promremotewrite"}`)
|
|
|
|
rowsPerInsert = metrics.NewHistogram(`vmagent_rows_per_insert{type="promremotewrite"}`)
|
2020-02-23 11:35:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// InsertHandler processes remote write for prometheus.
|
2021-08-05 06:46:19 +00:00
|
|
|
func InsertHandler(at *auth.Token, req *http.Request) error {
|
2021-01-12 22:52:50 +00:00
|
|
|
extraLabels, err := parserCommon.GetExtraLabels(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-21 02:38:49 +00:00
|
|
|
isVMRemoteWrite := req.Header.Get("Content-Encoding") == "zstd"
|
|
|
|
return stream.Parse(req.Body, isVMRemoteWrite, func(tss []prompb.TimeSeries) error {
|
2023-01-07 02:59:39 +00:00
|
|
|
return insertRows(at, tss, extraLabels)
|
2020-02-23 11:35:47 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-05 06:46:19 +00:00
|
|
|
func insertRows(at *auth.Token, timeseries []prompb.TimeSeries, extraLabels []prompbmarshal.Label) error {
|
2020-02-23 11:35:47 +00: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 timeseries {
|
|
|
|
ts := ×eries[i]
|
2020-10-09 10:29:27 +00:00
|
|
|
rowsTotal += len(ts.Samples)
|
2020-02-23 11:35:47 +00:00
|
|
|
labelsLen := len(labels)
|
|
|
|
for i := range ts.Labels {
|
|
|
|
label := &ts.Labels[i]
|
|
|
|
labels = append(labels, prompbmarshal.Label{
|
2024-01-14 20:33:19 +00:00
|
|
|
Name: label.Name,
|
|
|
|
Value: label.Value,
|
2020-02-23 11:35:47 +00:00
|
|
|
})
|
|
|
|
}
|
2021-01-12 22:52:50 +00:00
|
|
|
labels = append(labels, extraLabels...)
|
2020-02-23 11:35:47 +00:00
|
|
|
samplesLen := len(samples)
|
|
|
|
for i := range ts.Samples {
|
|
|
|
sample := &ts.Samples[i]
|
|
|
|
samples = append(samples, prompbmarshal.Sample{
|
|
|
|
Value: sample.Value,
|
|
|
|
Timestamp: sample.Timestamp,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
tssDst = append(tssDst, prompbmarshal.TimeSeries{
|
|
|
|
Labels: labels[labelsLen:],
|
|
|
|
Samples: samples[samplesLen:],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
ctx.WriteRequest.Timeseries = tssDst
|
|
|
|
ctx.Labels = labels
|
|
|
|
ctx.Samples = samples
|
2023-11-25 09:31:30 +00:00
|
|
|
if !remotewrite.TryPush(at, &ctx.WriteRequest) {
|
2023-11-24 12:42:11 +00:00
|
|
|
return remotewrite.ErrQueueFullHTTPRetry
|
|
|
|
}
|
2020-02-23 11:35:47 +00:00
|
|
|
rowsInserted.Add(rowsTotal)
|
2021-08-05 06:46:19 +00:00
|
|
|
if at != nil {
|
|
|
|
rowsTenantInserted.Get(at).Add(rowsTotal)
|
2021-08-05 06:44:29 +00:00
|
|
|
}
|
2020-02-23 11:35:47 +00:00
|
|
|
rowsPerInsert.Update(float64(rowsTotal))
|
|
|
|
return nil
|
|
|
|
}
|