From 005ba7ac9785f1f3464cc1e6a14a22670e4adfc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Pazos?= Date: Tue, 26 Sep 2023 13:29:59 -0300 Subject: [PATCH] tests and new -> original proto mapping util MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nicolás Pazos --- storage/remote/codec.go | 34 +++++++++++++++++ storage/remote/codec_test.go | 72 +++++++++++++++++++++++++++++++++++- 2 files changed, 105 insertions(+), 1 deletion(-) diff --git a/storage/remote/codec.go b/storage/remote/codec.go index 291d04188..09c44f033 100644 --- a/storage/remote/codec.go +++ b/storage/remote/codec.go @@ -888,3 +888,37 @@ func DecodeReducedWriteRequest(r io.Reader) (*prompb.WriteRequestWithRefs, error return &req, nil } + +func ReducedWriteRequestToWriteRequest(redReq *prompb.WriteRequestWithRefs) (*prompb.WriteRequest, error) { + req := &prompb.WriteRequest{ + Timeseries: make([]prompb.TimeSeries, len(redReq.Timeseries)), + Metadata: redReq.Metadata, + } + + for i, rts := range redReq.Timeseries { + + lbls := make([]prompb.Label, len(rts.Labels)) + for j, l := range rts.Labels { + lbls[j].Name = redReq.StringSymbolTable[l.NameRef] + lbls[j].Value = redReq.StringSymbolTable[l.ValueRef] + } + + exemplars := make([]prompb.Exemplar, len(rts.Exemplars)) + for j, e := range rts.Exemplars { + exemplars[j].Value = e.Value + exemplars[j].Timestamp = e.Timestamp + exemplars[j].Labels = make([]prompb.Label, len(e.Labels)) + for k, l := range e.Labels { + exemplars[j].Labels[k].Name = redReq.StringSymbolTable[l.NameRef] + exemplars[j].Labels[k].Value = redReq.StringSymbolTable[l.ValueRef] + } + } + + req.Timeseries[i].Labels = lbls + req.Timeseries[i].Samples = rts.Samples + req.Timeseries[i].Exemplars = exemplars + req.Timeseries[i].Histograms = rts.Histograms + + } + return req, nil +} diff --git a/storage/remote/codec_test.go b/storage/remote/codec_test.go index d2a7d45be..5da07e626 100644 --- a/storage/remote/codec_test.go +++ b/storage/remote/codec_test.go @@ -74,6 +74,59 @@ var writeRequestFixture = &prompb.WriteRequest{ }, } +// writeRequestWithRefsFixture represents the same request as writeRequestFixture, but using the reduced representation. +var writeRequestWithRefsFixture = &prompb.WriteRequestWithRefs{ + StringSymbolTable: map[uint64]string{ + // Names + 0: "__name__", + 2: "b", + 4: "baz", + 6: "d", + 8: "foo", + 10: "f", + 12: "h", + // Values + 1: "test_metric1", + 3: "c", + 5: "qux", + 7: "e", + 9: "bar", + 11: "g", + 13: "i", + }, + + Timeseries: []prompb.ReducedTimeSeries{ + { + Labels: []prompb.LabelRef{ + {NameRef: 0, ValueRef: 1}, + {NameRef: 2, ValueRef: 3}, + {NameRef: 4, ValueRef: 5}, + {NameRef: 6, ValueRef: 7}, + {NameRef: 8, ValueRef: 9}, + }, + Samples: []prompb.Sample{{Value: 1, Timestamp: 0}}, + Exemplars: []prompb.ExemplarRef{{Labels: []prompb.LabelRef{ + {NameRef: 10, ValueRef: 11}, + }, Value: 1, Timestamp: 0}}, + Histograms: []prompb.Histogram{HistogramToHistogramProto(0, &testHistogram)}, + }, + { + Labels: []prompb.LabelRef{ + {NameRef: 0, ValueRef: 1}, + {NameRef: 2, ValueRef: 3}, + {NameRef: 4, ValueRef: 5}, + {NameRef: 6, ValueRef: 7}, + {NameRef: 8, ValueRef: 9}, + }, + Samples: []prompb.Sample{{Value: 2, Timestamp: 1}}, + Exemplars: []prompb.ExemplarRef{{Labels: []prompb.LabelRef{ + {NameRef: 12, ValueRef: 13}, + }, Value: 2, Timestamp: 1}}, + Histograms: []prompb.Histogram{HistogramToHistogramProto(1, &testHistogram)}, + }, + }, +} + func TestValidateLabelsAndMetricName(t *testing.T) { tests := []struct { input []prompb.Label @@ -525,7 +578,24 @@ func TestDecodeWriteRequest(t *testing.T) { require.Equal(t, writeRequestFixture, actual) } -func TestNilHistogramProto(*testing.T) { +func TestDecodeReducedWriteRequest(t *testing.T) { + buf, _, err := buildReducedWriteRequest(writeRequestWithRefsFixture.Timeseries, writeRequestWithRefsFixture.StringSymbolTable, nil, nil) + + require.NoError(t, err) + + actual, err := DecodeReducedWriteRequest(bytes.NewReader(buf)) + require.NoError(t, err) + require.Equal(t, writeRequestWithRefsFixture, actual) +} + +func TestReducedWriteRequestToWriteRequest(t *testing.T) { + actual, err := ReducedWriteRequestToWriteRequest(writeRequestWithRefsFixture) + require.NoError(t, err) + + require.Equal(t, writeRequestFixture, actual) +} + +func TestNilHistogramProto(t *testing.T) { // This function will panic if it impromperly handles nil // values, causing the test to fail. HistogramProtoToHistogram(prompb.Histogram{})