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 8cb138e8df
commit 7d40506744
No known key found for this signature in database
GPG key ID: 52C003EE2BCDB9EB
9 changed files with 49 additions and 53 deletions

View file

@ -6,7 +6,6 @@ import (
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmagent/common"
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmagent/remotewrite"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/auth"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompb"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
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 {
label := &ts.Labels[i]
labels = append(labels, prompbmarshal.Label{
Name: bytesutil.ToUnsafeString(label.Name),
Value: bytesutil.ToUnsafeString(label.Value),
Name: label.Name,
Value: label.Value,
})
}
labels = append(labels, extraLabels...)

View file

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

View file

@ -57,12 +57,13 @@ func (br *bufRows) pushTo(snb *storageNodesBucket, sn *storageNode) error {
// Reset resets ctx.
func (ctx *InsertCtx) Reset() {
ctx.snb = getStorageNodesBucket()
for i := range ctx.Labels {
label := &ctx.Labels[i]
label.Name = nil
label.Value = nil
labels := ctx.Labels
for i := range labels {
labels[i] = prompb.Label{}
}
ctx.Labels = ctx.Labels[:0]
ctx.Labels = labels[:0]
ctx.MetricNameBuf = ctx.MetricNameBuf[:0]
if ctx.bufRowss == nil || len(ctx.bufRowss) != len(ctx.snb.sns) {
@ -89,8 +90,8 @@ func (ctx *InsertCtx) AddLabelBytes(name, value []byte) {
ctx.Labels = append(ctx.Labels, prompb.Label{
// Do not copy name and value contents for performance reasons.
// This reduces GC overhead on the number of objects and allocations.
Name: name,
Value: value,
Name: bytesutil.ToUnsafeString(name),
Value: bytesutil.ToUnsafeString(value),
})
}
@ -107,8 +108,8 @@ func (ctx *InsertCtx) AddLabel(name, value string) {
ctx.Labels = append(ctx.Labels, prompb.Label{
// Do not copy name and value contents for performance reasons.
// This reduces GC overhead on the number of objects and allocations.
Name: bytesutil.ToUnsafeBytes(name),
Value: bytesutil.ToUnsafeBytes(value),
Name: name,
Value: value,
})
}
@ -174,8 +175,8 @@ func (ctx *InsertCtx) GetStorageNodeIdx(at *auth.Token, labels []prompb.Label) i
buf = encoding.MarshalUint32(buf, at.ProjectID)
for i := range labels {
label := &labels[i]
buf = marshalBytesFast(buf, label.Name)
buf = marshalBytesFast(buf, label.Value)
buf = marshalStringFast(buf, label.Name)
buf = marshalStringFast(buf, label.Value)
}
h := xxhash.Sum64(buf)
ctx.labelsBuf = buf
@ -185,7 +186,7 @@ func (ctx *InsertCtx) GetStorageNodeIdx(at *auth.Token, labels []prompb.Label) i
return idx
}
func marshalBytesFast(dst []byte, s []byte) []byte {
func marshalStringFast(dst []byte, s string) []byte {
dst = encoding.MarshalUint16(dst, uint16(len(s)))
dst = append(dst, s...)
return dst
@ -222,17 +223,14 @@ func (ctx *InsertCtx) GetLocalAuthToken(at *auth.Token) *auth.Token {
}
cleanLabels := ctx.Labels[len(tmpLabels):]
for i := range cleanLabels {
label := &cleanLabels[i]
label.Name = nil
label.Value = nil
cleanLabels[i] = prompb.Label{}
}
ctx.Labels = tmpLabels
ctx.at.Set(accountID, projectID)
return &ctx.at
}
func parseUint32(b []byte) uint32 {
s := bytesutil.ToUnsafeString(b)
func parseUint32(s string) uint32 {
n, err := strconv.ParseUint(s, 10, 32)
if err != nil {
return 0

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -615,8 +615,8 @@ func MarshalMetricNameRaw(dst []byte, accountID, projectID uint32, labels []prom
// Skip labels without values, since they have no sense in prometheus.
continue
}
dst = marshalBytesFast(dst, label.Name)
dst = marshalBytesFast(dst, label.Value)
dst = marshalStringFast(dst, label.Name)
dst = marshalStringFast(dst, label.Value)
}
return dst
}
@ -686,8 +686,8 @@ func labelsToString(labels []prompb.Label) string {
// MarshalMetricLabelRaw marshals label to dst.
func MarshalMetricLabelRaw(dst []byte, label *prompb.Label) []byte {
dst = marshalBytesFast(dst, label.Name)
dst = marshalBytesFast(dst, label.Value)
dst = marshalStringFast(dst, label.Name)
dst = marshalStringFast(dst, label.Value)
return dst
}
@ -747,6 +747,12 @@ func (mn *MetricName) UnmarshalRaw(src []byte) error {
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 {
dst = encoding.MarshalUint16(dst, uint16(len(s)))
dst = append(dst, s...)