tests and new -> original proto mapping util

Signed-off-by: Nicolás Pazos <npazosmendez@gmail.com>
This commit is contained in:
Nicolás Pazos 2023-09-26 13:29:59 -03:00
parent 91bdd93956
commit 005ba7ac97
2 changed files with 105 additions and 1 deletions

View file

@ -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
}

View file

@ -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{})