From a396fc65ea1a22848fe20bb21a7cfffd3c8f0665 Mon Sep 17 00:00:00 2001 From: Callum Styan Date: Mon, 6 Nov 2023 16:41:34 -0800 Subject: [PATCH] add functions for translating between new proto formats symbol table and actual prometheus labels MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Marco Pracucci Signed-off-by: Callum Styan Signed-off-by: Nicolás Pazos --- storage/remote/codec.go | 43 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/storage/remote/codec.go b/storage/remote/codec.go index 7ea8a9227..c4a4a319a 100644 --- a/storage/remote/codec.go +++ b/storage/remote/codec.go @@ -817,6 +817,40 @@ func labelsToLabelRefsProto(lbls labels.Labels, pool *lookupPool, buf []prompb.L return result } +func labelsToUint32Slice(lbls labels.Labels, symbolTable *rwSymbolTable, buf []uint32) []uint32 { + result := buf[:0] + // ensure slice capacity + if cap(result)-len(result) < len(lbls)*2 { + result = append(make([]uint32, 0, len(lbls)*2), result...) + } + + lbls.Range(func(l labels.Label) { + result = append(result, symbolTable.Ref(l.Name)) + result = append(result, symbolTable.Ref(l.Value)) + }) + return result +} + +func Uint32RefToLabels(symbols string, minLabels []uint32) labels.Labels { + ls := make(labels.Labels, 0, len(minLabels)/2) + + labelIdx := 0 + for labelIdx < len(minLabels) { + // todo, check for overflow? + offset, length := unpackRef(minLabels[labelIdx]) + + name := symbols[offset : offset+length] + // todo, check for overflow? + offset, length = unpackRef(minLabels[labelIdx+1]) + + value := symbols[offset : offset+length] + ls = append(ls, labels.Label{Name: name, Value: value}) + labelIdx += 2 + } + + return ls +} + // metricTypeToMetricTypeProto transforms a Prometheus metricType into prompb metricType. Since the former is a string we need to transform it to an enum. func metricTypeToMetricTypeProto(t textparse.MetricType) prompb.MetricMetadata_MetricType { mt := strings.ToUpper(string(t)) @@ -957,3 +991,12 @@ func ReducedWriteRequestToWriteRequest(redReq *prompb.WriteRequestWithRefs) (*pr } return req, nil } + +// for use with minimized remote write proto format +func packRef(offset, length int) uint32 { + return uint32((offset&0xFFFFF)<<12 | (length & 0xFFF)) +} + +func unpackRef(ref uint32) (offset, length int) { + return int(ref>>12) & 0xFFFFF, int(ref & 0xFFF) +}