lib/prompb: change type of Label.Name and Label.Value from []byte to string

This makes it more consistent with lib/prompbmarshal.Label
This commit is contained in:
Aliaksandr Valialkin 2024-01-14 22:33:19 +02:00
parent f405384c8c
commit f2229c2e42
No known key found for this signature in database
GPG Key ID: 52C003EE2BCDB9EB
11 changed files with 50 additions and 47 deletions

View File

@ -98,7 +98,7 @@ func addLabel(dst []prompb.Label, key, value string) []prompb.Label {
dst = append(dst, prompb.Label{}) dst = append(dst, prompb.Label{})
} }
lb := &dst[len(dst)-1] lb := &dst[len(dst)-1]
lb.Name = bytesutil.ToUnsafeBytes(key) lb.Name = key
lb.Value = bytesutil.ToUnsafeBytes(value) lb.Value = value
return dst return dst
} }

View File

@ -6,7 +6,6 @@ import (
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmagent/common" "github.com/VictoriaMetrics/VictoriaMetrics/app/vmagent/common"
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmagent/remotewrite" "github.com/VictoriaMetrics/VictoriaMetrics/app/vmagent/remotewrite"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/auth" "github.com/VictoriaMetrics/VictoriaMetrics/lib/auth"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompb" "github.com/VictoriaMetrics/VictoriaMetrics/lib/prompb"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal" "github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
parserCommon "github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/common" parserCommon "github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/common"
@ -48,8 +47,8 @@ func insertRows(at *auth.Token, timeseries []prompb.TimeSeries, extraLabels []pr
for i := range ts.Labels { for i := range ts.Labels {
label := &ts.Labels[i] label := &ts.Labels[i]
labels = append(labels, prompbmarshal.Label{ labels = append(labels, prompbmarshal.Label{
Name: bytesutil.ToUnsafeString(label.Name), Name: label.Name,
Value: bytesutil.ToUnsafeString(label.Value), Value: label.Value,
}) })
} }
labels = append(labels, extraLabels...) labels = append(labels, extraLabels...)

View File

@ -266,10 +266,16 @@ func fillStorage(series []vm.TimeSeries) error {
for _, series := range series { for _, series := range series {
var labels []prompb.Label var labels []prompb.Label
for _, lp := range series.LabelPairs { for _, lp := range series.LabelPairs {
labels = append(labels, prompb.Label{Name: []byte(lp.Name), Value: []byte(lp.Value)}) labels = append(labels, prompb.Label{
Name: lp.Name,
Value: lp.Value,
})
} }
if series.Name != "" { if series.Name != "" {
labels = append(labels, prompb.Label{Name: []byte("__name__"), Value: []byte(series.Name)}) labels = append(labels, prompb.Label{
Name: "__name__",
Value: series.Name,
})
} }
mr := storage.MetricRow{} mr := storage.MetricRow{}
mr.MetricNameRaw = storage.MarshalMetricNameRaw(mr.MetricNameRaw[:0], labels) mr.MetricNameRaw = storage.MarshalMetricNameRaw(mr.MetricNameRaw[:0], labels)

View File

@ -27,12 +27,11 @@ type InsertCtx struct {
// Reset resets ctx for future fill with rowsLen rows. // Reset resets ctx for future fill with rowsLen rows.
func (ctx *InsertCtx) Reset(rowsLen int) { func (ctx *InsertCtx) Reset(rowsLen int) {
for i := range ctx.Labels { labels := ctx.Labels
label := &ctx.Labels[i] for i := range labels {
label.Name = nil labels[i] = prompb.Label{}
label.Value = nil
} }
ctx.Labels = ctx.Labels[:0] ctx.Labels = labels[:0]
mrs := ctx.mrs mrs := ctx.mrs
for i := range mrs { for i := range mrs {
@ -112,8 +111,8 @@ func (ctx *InsertCtx) AddLabelBytes(name, value []byte) {
ctx.Labels = append(ctx.Labels, prompb.Label{ ctx.Labels = append(ctx.Labels, prompb.Label{
// Do not copy name and value contents for performance reasons. // Do not copy name and value contents for performance reasons.
// This reduces GC overhead on the number of objects and allocations. // This reduces GC overhead on the number of objects and allocations.
Name: name, Name: bytesutil.ToUnsafeString(name),
Value: value, Value: bytesutil.ToUnsafeString(value),
}) })
} }
@ -130,8 +129,8 @@ func (ctx *InsertCtx) AddLabel(name, value string) {
ctx.Labels = append(ctx.Labels, prompb.Label{ ctx.Labels = append(ctx.Labels, prompb.Label{
// Do not copy name and value contents for performance reasons. // Do not copy name and value contents for performance reasons.
// This reduces GC overhead on the number of objects and allocations. // This reduces GC overhead on the number of objects and allocations.
Name: bytesutil.ToUnsafeBytes(name), Name: name,
Value: bytesutil.ToUnsafeBytes(value), Value: value,
}) })
} }

View File

@ -160,11 +160,9 @@ func (ctx *pushCtx) reset() {
originLabels := ctx.originLabels originLabels := ctx.originLabels
for i := range originLabels { for i := range originLabels {
label := &originLabels[i] originLabels[i] = prompb.Label{}
label.Name = nil
label.Value = nil
} }
ctx.originLabels = ctx.originLabels[:0] ctx.originLabels = originLabels[:0]
} }
func getPushCtx() *pushCtx { func getPushCtx() *pushCtx {

View File

@ -46,7 +46,7 @@ func insertRows(timeseries []prompb.TimeSeries, extraLabels []prompbmarshal.Labe
ctx.Labels = ctx.Labels[:0] ctx.Labels = ctx.Labels[:0]
srcLabels := ts.Labels srcLabels := ts.Labels
for _, srcLabel := range srcLabels { for _, srcLabel := range srcLabels {
ctx.AddLabelBytes(srcLabel.Name, srcLabel.Value) ctx.AddLabel(srcLabel.Name, srcLabel.Value)
} }
for j := range extraLabels { for j := range extraLabels {
label := &extraLabels[j] label := &extraLabels[j]

View File

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"sync/atomic" "sync/atomic"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fasttime" "github.com/VictoriaMetrics/VictoriaMetrics/lib/fasttime"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger" "github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/procutil" "github.com/VictoriaMetrics/VictoriaMetrics/lib/procutil"
@ -118,11 +117,11 @@ func (ctx *Ctx) ApplyRelabeling(labels []prompb.Label) []prompb.Label {
// Convert labels to prompbmarshal.Label format suitable for relabeling. // Convert labels to prompbmarshal.Label format suitable for relabeling.
tmpLabels := ctx.tmpLabels[:0] tmpLabels := ctx.tmpLabels[:0]
for _, label := range labels { for _, label := range labels {
name := bytesutil.ToUnsafeString(label.Name) name := label.Name
if len(name) == 0 { if name == "" {
name = "__name__" name = "__name__"
} }
value := bytesutil.ToUnsafeString(label.Value) value := label.Value
tmpLabels = append(tmpLabels, prompbmarshal.Label{ tmpLabels = append(tmpLabels, prompbmarshal.Label{
Name: name, Name: name,
Value: value, Value: value,
@ -155,11 +154,11 @@ func (ctx *Ctx) ApplyRelabeling(labels []prompb.Label) []prompb.Label {
// Return back labels to the desired format. // Return back labels to the desired format.
dst := labels[:0] dst := labels[:0]
for _, label := range tmpLabels { for _, label := range tmpLabels {
name := bytesutil.ToUnsafeBytes(label.Name) name := label.Name
if label.Name == "__name__" { if label.Name == "__name__" {
name = nil name = ""
} }
value := bytesutil.ToUnsafeBytes(label.Value) value := label.Value
dst = append(dst, prompb.Label{ dst = append(dst, prompb.Label{
Name: name, Name: name,
Value: value, Value: value,

View File

@ -123,13 +123,13 @@ func registerMetrics(startTime time.Time, w http.ResponseWriter, r *http.Request
// Convert parsed metric and tags to labels. // Convert parsed metric and tags to labels.
labels = append(labels[:0], prompb.Label{ labels = append(labels[:0], prompb.Label{
Name: []byte("__name__"), Name: "__name__",
Value: []byte(row.Metric), Value: row.Metric,
}) })
for _, tag := range row.Tags { for _, tag := range row.Tags {
labels = append(labels, prompb.Label{ labels = append(labels, prompb.Label{
Name: []byte(tag.Key), Name: tag.Key,
Value: []byte(tag.Value), Value: tag.Value,
}) })
} }

View File

@ -7,6 +7,8 @@ import (
"fmt" "fmt"
"io" "io"
"math" "math"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
) )
// Sample is a timeseries sample. // Sample is a timeseries sample.
@ -23,8 +25,8 @@ type TimeSeries struct {
// Label is a timeseries label // Label is a timeseries label
type Label struct { type Label struct {
Name []byte Name string
Value []byte Value string
} }
// Unmarshal unmarshals sample from dAtA. // Unmarshal unmarshals sample from dAtA.
@ -296,7 +298,7 @@ func (m *Label) Unmarshal(dAtA []byte) error {
if postIndex > l { if postIndex > l {
return io.ErrUnexpectedEOF return io.ErrUnexpectedEOF
} }
m.Name = dAtA[iNdEx:postIndex] m.Name = bytesutil.ToUnsafeString(dAtA[iNdEx:postIndex])
iNdEx = postIndex iNdEx = postIndex
case 2: case 2:
if wireType != 2 { if wireType != 2 {
@ -325,7 +327,7 @@ func (m *Label) Unmarshal(dAtA []byte) error {
if postIndex > l { if postIndex > l {
return io.ErrUnexpectedEOF return io.ErrUnexpectedEOF
} }
m.Value = dAtA[iNdEx:postIndex] m.Value = bytesutil.ToUnsafeString(dAtA[iNdEx:postIndex])
iNdEx = postIndex iNdEx = postIndex
default: default:
iNdEx = preIndex iNdEx = preIndex

View File

@ -3,23 +3,17 @@ package prompb
// Reset resets wr. // Reset resets wr.
func (wr *WriteRequest) Reset() { func (wr *WriteRequest) Reset() {
for i := range wr.Timeseries { for i := range wr.Timeseries {
ts := &wr.Timeseries[i] wr.Timeseries[i] = TimeSeries{}
ts.Labels = nil
ts.Samples = nil
} }
wr.Timeseries = wr.Timeseries[:0] wr.Timeseries = wr.Timeseries[:0]
for i := range wr.labelsPool { for i := range wr.labelsPool {
lb := &wr.labelsPool[i] wr.labelsPool[i] = Label{}
lb.Name = nil
lb.Value = nil
} }
wr.labelsPool = wr.labelsPool[:0] wr.labelsPool = wr.labelsPool[:0]
for i := range wr.samplesPool { for i := range wr.samplesPool {
s := &wr.samplesPool[i] wr.samplesPool[i] = Sample{}
s.Value = 0
s.Timestamp = 0
} }
wr.samplesPool = wr.samplesPool[:0] wr.samplesPool = wr.samplesPool[:0]
} }

View File

@ -546,8 +546,8 @@ func MarshalMetricNameRaw(dst []byte, labels []prompb.Label) []byte {
// Skip labels without values, since they have no sense in prometheus. // Skip labels without values, since they have no sense in prometheus.
continue continue
} }
dst = marshalBytesFast(dst, label.Name) dst = marshalStringFast(dst, label.Name)
dst = marshalBytesFast(dst, label.Value) dst = marshalStringFast(dst, label.Value)
} }
return dst return dst
} }
@ -659,6 +659,12 @@ func (mn *MetricName) UnmarshalRaw(src []byte) error {
return nil return nil
} }
func marshalStringFast(dst []byte, s string) []byte {
dst = encoding.MarshalUint16(dst, uint16(len(s)))
dst = append(dst, s...)
return dst
}
func marshalBytesFast(dst []byte, s []byte) []byte { func marshalBytesFast(dst []byte, s []byte) []byte {
dst = encoding.MarshalUint16(dst, uint16(len(s))) dst = encoding.MarshalUint16(dst, uint16(len(s)))
dst = append(dst, s...) dst = append(dst, s...)