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
|
2020-09-26 01:13:10 +00:00
|
|
|
samplesCount := 0
|
|
|
|
i := 0
|
|
|
|
for i < len(tss) {
|
|
|
|
samplesCount += len(tss[i].Samples)
|
|
|
|
i++
|
|
|
|
if samplesCount > maxRowsPerBlock {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-03-03 17:58:45 +00:00
|
|
|
tssBlock := tss
|
2020-09-26 01:13:10 +00:00
|
|
|
if i < len(tss) {
|
|
|
|
tssBlock = tss[:i]
|
|
|
|
tss = tss[i:]
|
2020-03-03 17:58:45 +00:00
|
|
|
} 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-10-09 10:29:27 +00:00
|
|
|
rowsTotal += len(ts.Samples)
|
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
|
2020-07-24 20:19:49 +00:00
|
|
|
var err error
|
2020-02-23 11:35:47 +00:00
|
|
|
for i := range ts.Samples {
|
|
|
|
r := &ts.Samples[i]
|
2020-07-24 20:19:49 +00:00
|
|
|
metricNameRaw, err = ctx.WriteDataPointExt(metricNameRaw, ctx.Labels, r.Timestamp, r.Value)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("cannot write promscape data to storage: %s", err)
|
|
|
|
return
|
|
|
|
}
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|