From 3d79471fb354bf81acafa01d20b0e98b502a3893 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Tue, 12 Jan 2021 15:13:16 +0200 Subject: [PATCH] lib/storage: inline marshalTags function and remove the code for handling duplicate tags from here This is a follow-up commit after c8ea697db8be2e427b5c1d0af5a67495d55cafda --- lib/storage/metric_name.go | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/lib/storage/metric_name.go b/lib/storage/metric_name.go index df2336e5fb..8dbd4b13a2 100644 --- a/lib/storage/metric_name.go +++ b/lib/storage/metric_name.go @@ -381,7 +381,13 @@ func (mn *MetricName) Marshal(dst []byte) []byte { dst = encoding.MarshalUint32(dst, mn.AccountID) dst = encoding.MarshalUint32(dst, mn.ProjectID) dst = marshalTagValue(dst, mn.MetricGroup) - dst = marshalTags(dst, mn.Tags) + + // Marshal tags. + tags := mn.Tags + for i := range tags { + t := &tags[i] + dst = t.Marshal(dst) + } return dst } @@ -420,7 +426,9 @@ func (mn *MetricName) Unmarshal(src []byte) error { // MarshalNoAccountIDProjectID appends marshaled mn without AccountID and ProjectID // to dst and returns the result. // -// The result must be unmarshaled with UnmarshalNoAccountIDProjectID +// The result must be unmarshaled with UnmarshalNoAccountIDProjectID. +// +// It is expected that mn.Tags are already sorted and de-duplicated with mn.sortTags. func (mn *MetricName) MarshalNoAccountIDProjectID(dst []byte) []byte { // Calculate the required size and pre-allocate space in dst dstLen := len(dst) @@ -433,7 +441,11 @@ func (mn *MetricName) MarshalNoAccountIDProjectID(dst []byte) []byte { dst = dst[:dstLen] dst = marshalTagValue(dst, mn.MetricGroup) - dst = marshalTags(dst, mn.Tags) + tags := mn.Tags + for i := range tags { + t := &tags[i] + dst = t.Marshal(dst) + } return dst } @@ -716,20 +728,6 @@ func (ts *canonicalTagsSort) Swap(i, j int) { x[i], x[j] = x[j], x[i] } -func marshalTags(dst []byte, tags []Tag) []byte { - var prevKey []byte - for i := range tags { - t := &tags[i] - if string(prevKey) == string(t.Key) { - // Skip duplicate keys, since they aren't allowed in Prometheus data model. - continue - } - prevKey = t.Key - dst = t.Marshal(dst) - } - return dst -} - func copyTags(dst, src []Tag) []Tag { dstLen := len(dst) if n := dstLen + len(src) - cap(dst); n > 0 {