VictoriaMetrics/app/vminsert/prometheus/request_handler.go

112 lines
2.9 KiB
Go
Raw Normal View History

2019-05-22 21:16:55 +00:00
package prometheus
import (
"fmt"
"net/http"
"runtime"
"sync"
"github.com/VictoriaMetrics/VictoriaMetrics/app/vminsert/concurrencylimiter"
2019-05-22 21:23:23 +00:00
"github.com/VictoriaMetrics/VictoriaMetrics/app/vminsert/netstorage"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/auth"
2019-05-22 21:16:55 +00:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompb"
2019-05-22 21:23:23 +00:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/storage"
2019-05-22 21:16:55 +00:00
"github.com/VictoriaMetrics/metrics"
)
var rowsInserted = metrics.NewCounter(`vm_rows_inserted_total{type="prometheus"}`)
// InsertHandler processes remote write for prometheus.
2019-05-22 21:23:23 +00:00
func InsertHandler(at *auth.Token, r *http.Request, maxSize int64) error {
2019-05-22 21:16:55 +00:00
return concurrencylimiter.Do(func() error {
2019-05-22 21:23:23 +00:00
return insertHandlerInternal(at, r, maxSize)
2019-05-22 21:16:55 +00:00
})
}
2019-05-22 21:23:23 +00:00
func insertHandlerInternal(at *auth.Token, r *http.Request, maxSize int64) error {
2019-05-22 21:16:55 +00:00
ctx := getPushCtx()
defer putPushCtx(ctx)
if err := ctx.Read(r, maxSize); err != nil {
return err
}
2019-05-22 21:23:23 +00:00
2019-05-22 21:16:55 +00:00
ic := &ctx.Common
2019-05-22 21:23:23 +00:00
ic.Reset()
timeseries := ctx.req.Timeseries
2019-05-22 21:16:55 +00:00
for i := range timeseries {
ts := &timeseries[i]
2019-05-22 21:23:23 +00:00
storageNodeIdx := ic.GetStorageNodeIdx(at, ts.Labels)
ic.MetricNameBuf = ic.MetricNameBuf[:0]
2019-05-22 21:16:55 +00:00
for i := range ts.Samples {
r := &ts.Samples[i]
2019-05-22 21:23:23 +00:00
if len(ic.MetricNameBuf) == 0 {
ic.MetricNameBuf = storage.MarshalMetricNameRaw(ic.MetricNameBuf[:0], at.AccountID, at.ProjectID, ts.Labels)
}
if err := ic.WriteDataPointExt(at, storageNodeIdx, ic.MetricNameBuf, r.Timestamp, r.Value); err != nil {
return err
}
2019-05-22 21:16:55 +00:00
}
rowsInserted.Add(len(ts.Samples))
}
return ic.FlushBufs()
}
type pushCtx struct {
2019-05-22 21:23:23 +00:00
Common netstorage.InsertCtx
2019-05-22 21:16:55 +00:00
req prompb.WriteRequest
reqBuf []byte
}
func (ctx *pushCtx) reset() {
2019-05-22 21:23:23 +00:00
ctx.Common.Reset()
2019-05-22 21:16:55 +00:00
ctx.req.Reset()
ctx.reqBuf = ctx.reqBuf[:0]
}
func (ctx *pushCtx) Read(r *http.Request, maxSize int64) error {
prometheusReadCalls.Inc()
var err error
ctx.reqBuf, err = prompb.ReadSnappy(ctx.reqBuf[:0], r.Body, maxSize)
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))