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"
|
|
|
|
"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"
|
2020-02-23 12:35:47 +01:00
|
|
|
parser "github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/promremotewrite"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/writeconcurrencylimiter"
|
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
rowsInserted = metrics.NewCounter(`vmagent_rows_inserted_total{type="promremotewrite"}`)
|
|
|
|
rowsPerInsert = metrics.NewHistogram(`vmagent_rows_per_insert{type="promremotewrite"}`)
|
|
|
|
)
|
|
|
|
|
|
|
|
// InsertHandler processes remote write for prometheus.
|
|
|
|
func InsertHandler(req *http.Request) error {
|
2021-01-12 23:52:50 +01:00
|
|
|
extraLabels, err := parserCommon.GetExtraLabels(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-23 12:35:47 +01:00
|
|
|
return writeconcurrencylimiter.Do(func() error {
|
2021-01-12 23:52:50 +01:00
|
|
|
return parser.ParseStream(req, func(tss []prompb.TimeSeries) error {
|
|
|
|
return insertRows(tss, extraLabels)
|
|
|
|
})
|
2020-02-23 12:35:47 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-01-12 23:52:50 +01:00
|
|
|
func insertRows(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
|
|
|
|
remotewrite.Push(&ctx.WriteRequest)
|
|
|
|
rowsInserted.Add(rowsTotal)
|
|
|
|
rowsPerInsert.Update(float64(rowsTotal))
|
|
|
|
return nil
|
|
|
|
}
|