2020-02-23 12:35:47 +01:00
|
|
|
package promremotewrite
|
|
|
|
|
|
|
|
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"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompb"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
|
2021-01-12 23:52:50 +01:00
|
|
|
parserCommon "github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/common"
|
2023-02-13 19:46:50 +01:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/promremotewrite/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:46:19 +02: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 12:35:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// InsertHandler processes remote write for prometheus.
|
2021-08-05 08:46:19 +02:00
|
|
|
func InsertHandler(at *auth.Token, req *http.Request) error {
|
2021-01-12 23:52:50 +01:00
|
|
|
extraLabels, err := parserCommon.GetExtraLabels(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-21 03:38:49 +01:00
|
|
|
isVMRemoteWrite := req.Header.Get("Content-Encoding") == "zstd"
|
|
|
|
return stream.Parse(req.Body, isVMRemoteWrite, func(tss []prompb.TimeSeries) error {
|
2023-01-07 03:59:39 +01:00
|
|
|
return insertRows(at, tss, extraLabels)
|
2020-02-23 12:35:47 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-05 08:46:19 +02:00
|
|
|
func insertRows(at *auth.Token, timeseries []prompb.TimeSeries, 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 timeseries {
|
|
|
|
ts := ×eries[i]
|
2020-10-09 12:29:27 +02:00
|
|
|
rowsTotal += len(ts.Samples)
|
2020-02-23 12:35:47 +01:00
|
|
|
labelsLen := len(labels)
|
|
|
|
for i := range ts.Labels {
|
|
|
|
label := &ts.Labels[i]
|
|
|
|
labels = append(labels, prompbmarshal.Label{
|
|
|
|
Name: bytesutil.ToUnsafeString(label.Name),
|
|
|
|
Value: bytesutil.ToUnsafeString(label.Value),
|
|
|
|
})
|
|
|
|
}
|
2021-01-12 23:52:50 +01:00
|
|
|
labels = append(labels, extraLabels...)
|
2020-02-23 12:35:47 +01: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
|
2022-08-08 13:46:24 +02:00
|
|
|
remotewrite.Push(at, &ctx.WriteRequest)
|
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
|
|
|
|
}
|