2020-02-23 12:35:47 +01: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 18:58:45 +01:00
|
|
|
const maxRowsPerBlock = 10000
|
|
|
|
|
2022-08-08 13:46:24 +02:00
|
|
|
// Push pushes wr for the given at to storage.
|
2020-02-23 12:35:47 +01:00
|
|
|
func Push(wr *prompbmarshal.WriteRequest) {
|
2020-07-02 18:42:12 +02:00
|
|
|
ctx := common.GetInsertCtx()
|
|
|
|
defer common.PutInsertCtx(ctx)
|
2020-02-23 12:35:47 +01:00
|
|
|
|
2020-03-03 18:58:45 +01:00
|
|
|
tss := wr.Timeseries
|
|
|
|
for len(tss) > 0 {
|
2023-02-13 13:27:13 +01:00
|
|
|
// Process big tss in smaller blocks in order to reduce maximum memory usage
|
2020-09-26 03:13:10 +02:00
|
|
|
samplesCount := 0
|
|
|
|
i := 0
|
|
|
|
for i < len(tss) {
|
|
|
|
samplesCount += len(tss[i].Samples)
|
|
|
|
i++
|
|
|
|
if samplesCount > maxRowsPerBlock {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-03-03 18:58:45 +01:00
|
|
|
tssBlock := tss
|
2020-09-26 03:13:10 +02:00
|
|
|
if i < len(tss) {
|
|
|
|
tssBlock = tss[:i]
|
|
|
|
tss = tss[i:]
|
2020-03-03 18:58:45 +01:00
|
|
|
} else {
|
|
|
|
tss = nil
|
|
|
|
}
|
2020-07-02 18:42:12 +02:00
|
|
|
push(ctx, tssBlock)
|
2020-03-03 18:58:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-02 18:42:12 +02:00
|
|
|
func push(ctx *common.InsertCtx, tss []prompbmarshal.TimeSeries) {
|
2020-02-23 12:35:47 +01:00
|
|
|
rowsLen := 0
|
2020-03-03 18:58:45 +01:00
|
|
|
for i := range tss {
|
|
|
|
rowsLen += len(tss[i].Samples)
|
2020-02-23 12:35:47 +01:00
|
|
|
}
|
2020-07-02 18:42:12 +02:00
|
|
|
ctx.Reset(rowsLen)
|
2020-02-23 12:35:47 +01:00
|
|
|
rowsTotal := 0
|
2020-03-03 18:58:45 +01:00
|
|
|
for i := range tss {
|
|
|
|
ts := &tss[i]
|
2020-10-09 12:29:27 +02:00
|
|
|
rowsTotal += len(ts.Samples)
|
2020-07-02 18:42:12 +02:00
|
|
|
ctx.Labels = ctx.Labels[:0]
|
2020-02-23 12:35:47 +01:00
|
|
|
for j := range ts.Labels {
|
|
|
|
label := &ts.Labels[j]
|
2020-07-02 18:42:12 +02:00
|
|
|
ctx.AddLabel(label.Name, label.Value)
|
|
|
|
}
|
|
|
|
ctx.ApplyRelabeling()
|
|
|
|
if len(ctx.Labels) == 0 {
|
|
|
|
// Skip metric without labels.
|
|
|
|
continue
|
2020-02-23 12:35:47 +01:00
|
|
|
}
|
2021-03-31 22:12:56 +02:00
|
|
|
ctx.SortLabelsIfNeeded()
|
2020-02-23 12:35:47 +01:00
|
|
|
var metricNameRaw []byte
|
2020-07-24 22:19:49 +02:00
|
|
|
var err error
|
2020-02-23 12:35:47 +01:00
|
|
|
for i := range ts.Samples {
|
|
|
|
r := &ts.Samples[i]
|
2020-07-24 22:19:49 +02: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 12:35:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
rowsInserted.Add(rowsTotal)
|
|
|
|
rowsPerInsert.Update(float64(rowsTotal))
|
2020-07-02 18:42:12 +02:00
|
|
|
if err := ctx.FlushBufs(); err != nil {
|
2020-02-23 12:35:47 +01:00
|
|
|
logger.Errorf("cannot flush promscrape data to storage: %s", err)
|
|
|
|
}
|
|
|
|
}
|