diff --git a/app/victoria-metrics/self_scraper.go b/app/victoria-metrics/self_scraper.go index e8540eddd..931e5a517 100644 --- a/app/victoria-metrics/self_scraper.go +++ b/app/victoria-metrics/self_scraper.go @@ -98,7 +98,7 @@ func addLabel(dst []prompb.Label, key, value string) []prompb.Label { dst = append(dst, prompb.Label{}) } lb := &dst[len(dst)-1] - lb.Name = bytesutil.ToUnsafeBytes(key) - lb.Value = bytesutil.ToUnsafeBytes(value) + lb.Name = key + lb.Value = value return dst } diff --git a/app/vmagent/promremotewrite/request_handler.go b/app/vmagent/promremotewrite/request_handler.go index 657c717d3..02f758c5d 100644 --- a/app/vmagent/promremotewrite/request_handler.go +++ b/app/vmagent/promremotewrite/request_handler.go @@ -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...) diff --git a/app/vmctl/vm_native_test.go b/app/vmctl/vm_native_test.go index 217df70aa..78c35612c 100644 --- a/app/vmctl/vm_native_test.go +++ b/app/vmctl/vm_native_test.go @@ -266,10 +266,16 @@ func fillStorage(series []vm.TimeSeries) error { for _, series := range series { var labels []prompb.Label 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 != "" { - 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.MetricNameRaw = storage.MarshalMetricNameRaw(mr.MetricNameRaw[:0], labels) diff --git a/app/vminsert/common/insert_ctx.go b/app/vminsert/common/insert_ctx.go index ef4ded190..27ac496a6 100644 --- a/app/vminsert/common/insert_ctx.go +++ b/app/vminsert/common/insert_ctx.go @@ -27,12 +27,11 @@ type InsertCtx struct { // Reset resets ctx for future fill with rowsLen rows. func (ctx *InsertCtx) Reset(rowsLen int) { - 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] mrs := ctx.mrs for i := range mrs { @@ -112,8 +111,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), }) } @@ -130,8 +129,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, }) } diff --git a/app/vminsert/influx/request_handler.go b/app/vminsert/influx/request_handler.go index 572b8f71f..39108a188 100644 --- a/app/vminsert/influx/request_handler.go +++ b/app/vminsert/influx/request_handler.go @@ -160,11 +160,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 { diff --git a/app/vminsert/promremotewrite/request_handler.go b/app/vminsert/promremotewrite/request_handler.go index dc01ec72e..0749d4dc4 100644 --- a/app/vminsert/promremotewrite/request_handler.go +++ b/app/vminsert/promremotewrite/request_handler.go @@ -46,7 +46,7 @@ func insertRows(timeseries []prompb.TimeSeries, extraLabels []prompbmarshal.Labe 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] diff --git a/app/vminsert/relabel/relabel.go b/app/vminsert/relabel/relabel.go index 3508f154b..17a969445 100644 --- a/app/vminsert/relabel/relabel.go +++ b/app/vminsert/relabel/relabel.go @@ -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, diff --git a/app/vmselect/graphite/tags_api.go b/app/vmselect/graphite/tags_api.go index 75da7878d..d558388a0 100644 --- a/app/vmselect/graphite/tags_api.go +++ b/app/vmselect/graphite/tags_api.go @@ -123,13 +123,13 @@ func registerMetrics(startTime time.Time, w http.ResponseWriter, r *http.Request // 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, }) } diff --git a/lib/prompb/types.pb.go b/lib/prompb/types.pb.go index c13292663..8fd98b216 100644 --- a/lib/prompb/types.pb.go +++ b/lib/prompb/types.pb.go @@ -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 diff --git a/lib/prompb/util.go b/lib/prompb/util.go index ffda7168e..a606ff581 100644 --- a/lib/prompb/util.go +++ b/lib/prompb/util.go @@ -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] } diff --git a/lib/storage/metric_name.go b/lib/storage/metric_name.go index c7d8e3146..e2551f294 100644 --- a/lib/storage/metric_name.go +++ b/lib/storage/metric_name.go @@ -546,8 +546,8 @@ func MarshalMetricNameRaw(dst []byte, labels []prompb.Label) []byte { // 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 } @@ -659,6 +659,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...)