2019-05-22 23:16:55 +02:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-08-23 08:46:45 +02:00
|
|
|
"net/http"
|
2019-05-22 23:16:55 +02:00
|
|
|
|
2020-07-02 18:42:12 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vminsert/relabel"
|
2019-05-22 23:16:55 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmstorage"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
2019-08-23 08:46:45 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/httpserver"
|
2019-05-22 23:16:55 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompb"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
// InsertCtx contains common bits for data points insertion.
|
|
|
|
type InsertCtx struct {
|
|
|
|
Labels []prompb.Label
|
|
|
|
|
|
|
|
mrs []storage.MetricRow
|
|
|
|
metricNamesBuf []byte
|
2020-07-02 18:42:12 +02:00
|
|
|
|
|
|
|
relabelCtx relabel.Ctx
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reset resets ctx for future fill with rowsLen rows.
|
|
|
|
func (ctx *InsertCtx) Reset(rowsLen int) {
|
2020-07-02 18:42:12 +02:00
|
|
|
for i := range ctx.Labels {
|
|
|
|
label := &ctx.Labels[i]
|
2019-05-22 23:16:55 +02:00
|
|
|
label.Name = nil
|
|
|
|
label.Value = nil
|
|
|
|
}
|
|
|
|
ctx.Labels = ctx.Labels[:0]
|
|
|
|
|
|
|
|
for i := range ctx.mrs {
|
|
|
|
mr := &ctx.mrs[i]
|
|
|
|
mr.MetricNameRaw = nil
|
|
|
|
}
|
|
|
|
ctx.mrs = ctx.mrs[:0]
|
|
|
|
if n := rowsLen - cap(ctx.mrs); n > 0 {
|
|
|
|
ctx.mrs = append(ctx.mrs[:cap(ctx.mrs)], make([]storage.MetricRow, n)...)
|
|
|
|
}
|
2019-05-28 15:08:01 +02:00
|
|
|
ctx.mrs = ctx.mrs[:0]
|
2019-05-22 23:16:55 +02:00
|
|
|
ctx.metricNamesBuf = ctx.metricNamesBuf[:0]
|
2020-07-02 18:42:12 +02:00
|
|
|
|
|
|
|
ctx.relabelCtx.Reset()
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *InsertCtx) marshalMetricNameRaw(prefix []byte, labels []prompb.Label) []byte {
|
|
|
|
start := len(ctx.metricNamesBuf)
|
|
|
|
ctx.metricNamesBuf = append(ctx.metricNamesBuf, prefix...)
|
|
|
|
ctx.metricNamesBuf = storage.MarshalMetricNameRaw(ctx.metricNamesBuf, labels)
|
|
|
|
metricNameRaw := ctx.metricNamesBuf[start:]
|
|
|
|
return metricNameRaw[:len(metricNameRaw):len(metricNameRaw)]
|
|
|
|
}
|
|
|
|
|
2019-12-09 19:58:19 +01:00
|
|
|
// WriteDataPoint writes (timestamp, value) with the given prefix and labels into ctx buffer.
|
2019-05-22 23:16:55 +02:00
|
|
|
func (ctx *InsertCtx) WriteDataPoint(prefix []byte, labels []prompb.Label, timestamp int64, value float64) {
|
|
|
|
metricNameRaw := ctx.marshalMetricNameRaw(prefix, labels)
|
|
|
|
ctx.addRow(metricNameRaw, timestamp, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteDataPointExt writes (timestamp, value) with the given metricNameRaw and labels into ctx buffer.
|
|
|
|
//
|
|
|
|
// It returns metricNameRaw for the given labels if len(metricNameRaw) == 0.
|
|
|
|
func (ctx *InsertCtx) WriteDataPointExt(metricNameRaw []byte, labels []prompb.Label, timestamp int64, value float64) []byte {
|
|
|
|
if len(metricNameRaw) == 0 {
|
|
|
|
metricNameRaw = ctx.marshalMetricNameRaw(nil, labels)
|
|
|
|
}
|
|
|
|
ctx.addRow(metricNameRaw, timestamp, value)
|
|
|
|
return metricNameRaw
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *InsertCtx) addRow(metricNameRaw []byte, timestamp int64, value float64) {
|
|
|
|
mrs := ctx.mrs
|
|
|
|
if cap(mrs) > len(mrs) {
|
|
|
|
mrs = mrs[:len(mrs)+1]
|
|
|
|
} else {
|
|
|
|
mrs = append(mrs, storage.MetricRow{})
|
|
|
|
}
|
|
|
|
mr := &mrs[len(mrs)-1]
|
|
|
|
ctx.mrs = mrs
|
|
|
|
mr.MetricNameRaw = metricNameRaw
|
|
|
|
mr.Timestamp = timestamp
|
|
|
|
mr.Value = value
|
|
|
|
}
|
|
|
|
|
2019-12-09 19:58:19 +01:00
|
|
|
// AddLabelBytes adds (name, value) label to ctx.Labels.
|
|
|
|
//
|
|
|
|
// name and value must exist until ctx.Labels is used.
|
|
|
|
func (ctx *InsertCtx) AddLabelBytes(name, value []byte) {
|
|
|
|
labels := ctx.Labels
|
|
|
|
if cap(labels) > len(labels) {
|
|
|
|
labels = labels[:len(labels)+1]
|
|
|
|
} else {
|
|
|
|
labels = append(labels, prompb.Label{})
|
|
|
|
}
|
|
|
|
label := &labels[len(labels)-1]
|
|
|
|
|
|
|
|
// Do not copy name and value contents for performance reasons.
|
|
|
|
// This reduces GC overhead on the number of objects and allocations.
|
|
|
|
label.Name = name
|
|
|
|
label.Value = value
|
|
|
|
|
|
|
|
ctx.Labels = labels
|
|
|
|
}
|
|
|
|
|
2019-05-22 23:16:55 +02:00
|
|
|
// AddLabel adds (name, value) label to ctx.Labels.
|
|
|
|
//
|
|
|
|
// name and value must exist until ctx.Labels is used.
|
|
|
|
func (ctx *InsertCtx) AddLabel(name, value string) {
|
|
|
|
labels := ctx.Labels
|
|
|
|
if cap(labels) > len(labels) {
|
|
|
|
labels = labels[:len(labels)+1]
|
|
|
|
} else {
|
|
|
|
labels = append(labels, prompb.Label{})
|
|
|
|
}
|
|
|
|
label := &labels[len(labels)-1]
|
|
|
|
|
|
|
|
// Do not copy name and value contents for performance reasons.
|
|
|
|
// This reduces GC overhead on the number of objects and allocations.
|
|
|
|
label.Name = bytesutil.ToUnsafeBytes(name)
|
|
|
|
label.Value = bytesutil.ToUnsafeBytes(value)
|
|
|
|
|
|
|
|
ctx.Labels = labels
|
|
|
|
}
|
|
|
|
|
2020-07-02 18:42:12 +02:00
|
|
|
// ApplyRelabeling applies relabeling to ic.Labels.
|
|
|
|
func (ctx *InsertCtx) ApplyRelabeling() {
|
|
|
|
ctx.Labels = ctx.relabelCtx.ApplyRelabeling(ctx.Labels)
|
|
|
|
}
|
|
|
|
|
2019-05-22 23:16:55 +02:00
|
|
|
// FlushBufs flushes buffered rows to the underlying storage.
|
|
|
|
func (ctx *InsertCtx) FlushBufs() error {
|
|
|
|
if err := vmstorage.AddRows(ctx.mrs); err != nil {
|
2019-08-23 08:46:45 +02:00
|
|
|
return &httpserver.ErrorWithStatusCode{
|
2020-06-30 21:58:18 +02:00
|
|
|
Err: fmt.Errorf("cannot store metrics: %w", err),
|
2019-08-23 08:46:45 +02:00
|
|
|
StatusCode: http.StatusServiceUnavailable,
|
|
|
|
}
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|