mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-12-05 01:01:09 +01:00
71 lines
2.0 KiB
Go
71 lines
2.0 KiB
Go
package vmimport
|
|
|
|
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/prompbmarshal"
|
|
parser "github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/vmimport"
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/writeconcurrencylimiter"
|
|
"github.com/VictoriaMetrics/metrics"
|
|
)
|
|
|
|
var (
|
|
rowsInserted = metrics.NewCounter(`vmagent_rows_inserted_total{type="vmimport"}`)
|
|
rowsPerInsert = metrics.NewHistogram(`vmagent_rows_per_insert{type="vmimport"}`)
|
|
)
|
|
|
|
// InsertHandler processes `/api/v1/import` request.
|
|
//
|
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/6
|
|
func InsertHandler(req *http.Request) error {
|
|
return writeconcurrencylimiter.Do(func() error {
|
|
return parser.ParseStream(req, insertRows)
|
|
})
|
|
}
|
|
|
|
func insertRows(rows []parser.Row) 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 rows {
|
|
r := &rows[i]
|
|
labelsLen := len(labels)
|
|
for j := range r.Tags {
|
|
tag := &r.Tags[j]
|
|
labels = append(labels, prompbmarshal.Label{
|
|
Name: bytesutil.ToUnsafeString(tag.Key),
|
|
Value: bytesutil.ToUnsafeString(tag.Value),
|
|
})
|
|
}
|
|
values := r.Values
|
|
timestamps := r.Timestamps
|
|
_ = timestamps[len(values)-1]
|
|
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:],
|
|
})
|
|
rowsTotal += len(values)
|
|
}
|
|
ctx.WriteRequest.Timeseries = tssDst
|
|
ctx.Labels = labels
|
|
ctx.Samples = samples
|
|
remotewrite.Push(&ctx.WriteRequest)
|
|
rowsInserted.Add(rowsTotal)
|
|
rowsPerInsert.Update(float64(rowsTotal))
|
|
return nil
|
|
}
|