2019-12-09 18:58:19 +00:00
|
|
|
package vmimport
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"runtime"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vminsert/common"
|
2020-02-23 11:35:47 +00:00
|
|
|
parser "github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/vmimport"
|
2019-12-09 18:58:19 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/storage"
|
2020-02-23 11:35:47 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/writeconcurrencylimiter"
|
2019-12-09 18:58:19 +00:00
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
rowsInserted = metrics.NewCounter(`vm_rows_inserted_total{type="vmimport"}`)
|
2020-02-23 11:35:47 +00:00
|
|
|
rowsPerInsert = metrics.NewHistogram(`vm_rows_per_insert{type="vmimport"}`)
|
2019-12-09 18:58:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// InsertHandler processes `/api/v1/import` request.
|
|
|
|
//
|
|
|
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/6
|
|
|
|
func InsertHandler(req *http.Request) error {
|
2020-02-23 11:35:47 +00:00
|
|
|
return writeconcurrencylimiter.Do(func() error {
|
|
|
|
return parser.ParseStream(req, insertRows)
|
2019-12-09 18:58:19 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-23 11:35:47 +00:00
|
|
|
func insertRows(rows []parser.Row) error {
|
2019-12-09 18:58:19 +00:00
|
|
|
ctx := getPushCtx()
|
|
|
|
defer putPushCtx(ctx)
|
|
|
|
|
|
|
|
rowsLen := 0
|
|
|
|
for i := range rows {
|
|
|
|
rowsLen += len(rows[i].Values)
|
|
|
|
}
|
|
|
|
ic := &ctx.Common
|
|
|
|
ic.Reset(rowsLen)
|
|
|
|
rowsTotal := 0
|
|
|
|
for i := range rows {
|
|
|
|
r := &rows[i]
|
|
|
|
ic.Labels = ic.Labels[:0]
|
|
|
|
for j := range r.Tags {
|
|
|
|
tag := &r.Tags[j]
|
|
|
|
ic.AddLabelBytes(tag.Key, tag.Value)
|
|
|
|
}
|
2020-07-02 16:42:12 +00:00
|
|
|
ic.ApplyRelabeling()
|
|
|
|
if len(ic.Labels) == 0 {
|
|
|
|
// Skip metric without labels.
|
|
|
|
continue
|
|
|
|
}
|
2019-12-09 18:58:19 +00:00
|
|
|
ctx.metricNameBuf = storage.MarshalMetricNameRaw(ctx.metricNameBuf[:0], ic.Labels)
|
|
|
|
values := r.Values
|
|
|
|
timestamps := r.Timestamps
|
|
|
|
_ = timestamps[len(values)-1]
|
|
|
|
for j, value := range values {
|
|
|
|
timestamp := timestamps[j]
|
|
|
|
ic.WriteDataPoint(ctx.metricNameBuf, nil, timestamp, value)
|
|
|
|
}
|
|
|
|
rowsTotal += len(values)
|
|
|
|
}
|
|
|
|
rowsInserted.Add(rowsTotal)
|
|
|
|
rowsPerInsert.Update(float64(rowsTotal))
|
|
|
|
return ic.FlushBufs()
|
|
|
|
}
|
|
|
|
|
|
|
|
type pushCtx struct {
|
2020-02-23 11:35:47 +00:00
|
|
|
Common common.InsertCtx
|
2019-12-09 18:58:19 +00:00
|
|
|
metricNameBuf []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *pushCtx) reset() {
|
|
|
|
ctx.Common.Reset(0)
|
|
|
|
ctx.metricNameBuf = ctx.metricNameBuf[:0]
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPushCtx() *pushCtx {
|
|
|
|
select {
|
|
|
|
case ctx := <-pushCtxPoolCh:
|
|
|
|
return ctx
|
|
|
|
default:
|
|
|
|
if v := pushCtxPool.Get(); v != nil {
|
|
|
|
return v.(*pushCtx)
|
|
|
|
}
|
|
|
|
return &pushCtx{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func putPushCtx(ctx *pushCtx) {
|
|
|
|
ctx.reset()
|
|
|
|
select {
|
|
|
|
case pushCtxPoolCh <- ctx:
|
|
|
|
default:
|
|
|
|
pushCtxPool.Put(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var pushCtxPool sync.Pool
|
|
|
|
var pushCtxPoolCh = make(chan *pushCtx, runtime.GOMAXPROCS(-1))
|