mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-12-01 14:47:38 +00:00
68 lines
2 KiB
Go
68 lines
2 KiB
Go
|
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"
|
||
|
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 {
|
||
|
return writeconcurrencylimiter.Do(func() error {
|
||
|
return parser.ParseStream(req, insertRows)
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func insertRows(timeseries []prompb.TimeSeries) error {
|
||
|
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]
|
||
|
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),
|
||
|
})
|
||
|
}
|
||
|
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:],
|
||
|
})
|
||
|
rowsTotal += len(ts.Samples)
|
||
|
}
|
||
|
ctx.WriteRequest.Timeseries = tssDst
|
||
|
ctx.Labels = labels
|
||
|
ctx.Samples = samples
|
||
|
remotewrite.Push(&ctx.WriteRequest)
|
||
|
rowsInserted.Add(rowsTotal)
|
||
|
rowsPerInsert.Update(float64(rowsTotal))
|
||
|
return nil
|
||
|
}
|