2019-05-22 21:16:55 +00:00
|
|
|
package prometheus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"runtime"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vminsert/common"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vminsert/concurrencylimiter"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompb"
|
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
)
|
|
|
|
|
2019-07-27 10:20:47 +00:00
|
|
|
var (
|
|
|
|
rowsInserted = metrics.NewCounter(`vm_rows_inserted_total{type="prometheus"}`)
|
|
|
|
rowsPerInsert = metrics.NewSummary(`vm_rows_per_insert{type="prometheus"}`)
|
|
|
|
)
|
2019-05-22 21:16:55 +00:00
|
|
|
|
|
|
|
// InsertHandler processes remote write for prometheus.
|
2020-01-16 11:03:28 +00:00
|
|
|
func InsertHandler(r *http.Request) error {
|
2019-05-22 21:16:55 +00:00
|
|
|
return concurrencylimiter.Do(func() error {
|
2020-01-16 11:03:28 +00:00
|
|
|
return insertHandlerInternal(r)
|
2019-05-22 21:16:55 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-16 11:03:28 +00:00
|
|
|
func insertHandlerInternal(r *http.Request) error {
|
2019-05-22 21:16:55 +00:00
|
|
|
ctx := getPushCtx()
|
|
|
|
defer putPushCtx(ctx)
|
2020-01-16 11:03:28 +00:00
|
|
|
if err := ctx.Read(r); err != nil {
|
2019-05-22 21:16:55 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
timeseries := ctx.req.Timeseries
|
|
|
|
rowsLen := 0
|
|
|
|
for i := range timeseries {
|
|
|
|
rowsLen += len(timeseries[i].Samples)
|
|
|
|
}
|
|
|
|
ic := &ctx.Common
|
|
|
|
ic.Reset(rowsLen)
|
2019-07-27 10:20:47 +00:00
|
|
|
rowsTotal := 0
|
2019-05-22 21:16:55 +00:00
|
|
|
for i := range timeseries {
|
|
|
|
ts := ×eries[i]
|
|
|
|
var metricNameRaw []byte
|
|
|
|
for i := range ts.Samples {
|
|
|
|
r := &ts.Samples[i]
|
|
|
|
metricNameRaw = ic.WriteDataPointExt(metricNameRaw, ts.Labels, r.Timestamp, r.Value)
|
|
|
|
}
|
2019-07-27 10:20:47 +00:00
|
|
|
rowsTotal += len(ts.Samples)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2019-07-27 10:20:47 +00:00
|
|
|
rowsInserted.Add(rowsTotal)
|
|
|
|
rowsPerInsert.Update(float64(rowsTotal))
|
2019-05-22 21:16:55 +00:00
|
|
|
return ic.FlushBufs()
|
|
|
|
}
|
|
|
|
|
|
|
|
type pushCtx struct {
|
|
|
|
Common common.InsertCtx
|
|
|
|
|
|
|
|
req prompb.WriteRequest
|
|
|
|
reqBuf []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *pushCtx) reset() {
|
|
|
|
ctx.Common.Reset(0)
|
|
|
|
ctx.req.Reset()
|
|
|
|
ctx.reqBuf = ctx.reqBuf[:0]
|
|
|
|
}
|
|
|
|
|
2020-01-16 11:03:28 +00:00
|
|
|
func (ctx *pushCtx) Read(r *http.Request) error {
|
2019-05-22 21:16:55 +00:00
|
|
|
prometheusReadCalls.Inc()
|
|
|
|
|
|
|
|
var err error
|
2020-01-16 11:03:28 +00:00
|
|
|
ctx.reqBuf, err = prompb.ReadSnappy(ctx.reqBuf[:0], r.Body)
|
2019-05-22 21:16:55 +00:00
|
|
|
if err != nil {
|
|
|
|
prometheusReadErrors.Inc()
|
|
|
|
return fmt.Errorf("cannot read prompb.WriteRequest: %s", err)
|
|
|
|
}
|
|
|
|
if err = ctx.req.Unmarshal(ctx.reqBuf); err != nil {
|
|
|
|
prometheusUnmarshalErrors.Inc()
|
|
|
|
return fmt.Errorf("cannot unmarshal prompb.WriteRequest with size %d bytes: %s", len(ctx.reqBuf), err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
prometheusReadCalls = metrics.NewCounter(`vm_read_calls_total{name="prometheus"}`)
|
|
|
|
prometheusReadErrors = metrics.NewCounter(`vm_read_errors_total{name="prometheus"}`)
|
|
|
|
prometheusUnmarshalErrors = metrics.NewCounter(`vm_unmarshal_errors_total{name="prometheus"}`)
|
|
|
|
)
|
|
|
|
|
|
|
|
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))
|