2020-02-23 11:35:47 +00:00
|
|
|
package prompush
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vminsert/common"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
|
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
rowsInserted = metrics.NewCounter(`vm_rows_inserted_total{type="promscrape"}`)
|
|
|
|
rowsPerInsert = metrics.NewHistogram(`vm_rows_per_insert{type="promscrape"}`)
|
|
|
|
)
|
|
|
|
|
2020-03-03 17:58:45 +00:00
|
|
|
const maxRowsPerBlock = 10000
|
|
|
|
|
|
|
|
// Push pushes wr to storage.
|
2020-02-23 11:35:47 +00:00
|
|
|
func Push(wr *prompbmarshal.WriteRequest) {
|
2020-07-02 16:42:12 +00:00
|
|
|
ctx := common.GetInsertCtx()
|
|
|
|
defer common.PutInsertCtx(ctx)
|
2020-02-23 11:35:47 +00:00
|
|
|
|
2020-03-03 17:58:45 +00:00
|
|
|
tss := wr.Timeseries
|
|
|
|
for len(tss) > 0 {
|
|
|
|
// Process big tss in smaller blocks in order to reduce maxmimum memory usage
|
|
|
|
tssBlock := tss
|
|
|
|
if len(tssBlock) > maxRowsPerBlock {
|
|
|
|
tssBlock = tss[:maxRowsPerBlock]
|
|
|
|
tss = tss[maxRowsPerBlock:]
|
|
|
|
} else {
|
|
|
|
tss = nil
|
|
|
|
}
|
2020-07-02 16:42:12 +00:00
|
|
|
push(ctx, tssBlock)
|
2020-03-03 17:58:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-02 16:42:12 +00:00
|
|
|
func push(ctx *common.InsertCtx, tss []prompbmarshal.TimeSeries) {
|
2020-02-23 11:35:47 +00:00
|
|
|
rowsLen := 0
|
2020-03-03 17:58:45 +00:00
|
|
|
for i := range tss {
|
|
|
|
rowsLen += len(tss[i].Samples)
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
2020-07-02 16:42:12 +00:00
|
|
|
ctx.Reset(rowsLen)
|
2020-02-23 11:35:47 +00:00
|
|
|
rowsTotal := 0
|
2020-03-03 17:58:45 +00:00
|
|
|
for i := range tss {
|
|
|
|
ts := &tss[i]
|
2020-07-02 16:42:12 +00:00
|
|
|
ctx.Labels = ctx.Labels[:0]
|
2020-02-23 11:35:47 +00:00
|
|
|
for j := range ts.Labels {
|
|
|
|
label := &ts.Labels[j]
|
2020-07-02 16:42:12 +00:00
|
|
|
ctx.AddLabel(label.Name, label.Value)
|
|
|
|
}
|
|
|
|
ctx.ApplyRelabeling()
|
|
|
|
if len(ctx.Labels) == 0 {
|
|
|
|
// Skip metric without labels.
|
|
|
|
continue
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
var metricNameRaw []byte
|
|
|
|
for i := range ts.Samples {
|
|
|
|
r := &ts.Samples[i]
|
2020-07-02 16:42:12 +00:00
|
|
|
metricNameRaw = ctx.WriteDataPointExt(metricNameRaw, ctx.Labels, r.Timestamp, r.Value)
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
rowsTotal += len(ts.Samples)
|
|
|
|
}
|
|
|
|
rowsInserted.Add(rowsTotal)
|
|
|
|
rowsPerInsert.Update(float64(rowsTotal))
|
2020-07-02 16:42:12 +00:00
|
|
|
if err := ctx.FlushBufs(); err != nil {
|
2020-02-23 11:35:47 +00:00
|
|
|
logger.Errorf("cannot flush promscrape data to storage: %s", err)
|
|
|
|
}
|
|
|
|
}
|