From ac91c77f5403150f459820e3c187ed2cbafc0534 Mon Sep 17 00:00:00 2001 From: Arvind Iyengar Date: Wed, 14 Dec 2022 12:06:39 -0800 Subject: [PATCH] Ensure prompb responses from read request are always sorted --- pkg/agent/http_test.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/pkg/agent/http_test.go b/pkg/agent/http_test.go index 5442f5a..e224993 100644 --- a/pkg/agent/http_test.go +++ b/pkg/agent/http_test.go @@ -541,6 +541,8 @@ func (v ScenarioValidator) validateProtoBody(t *testing.T, res *httptest.Respons t.Fatal(err) } + sortReadResponse(&protoRes) + if got, want := protoRes.Results, v.Scenario.RespBody; !reflect.DeepEqual(got, want) { t.Errorf("[%s] [%s] token %q scenario %q: got body\n%v\n, want\n%v\n", v.Type, v.Method, v.Token, v.Name, got, want) } @@ -580,6 +582,39 @@ func jsonResponseBody(body interface{}) string { return string(respBytes) } +type SortableTimeSeries []*prompb.TimeSeries + +func (s SortableTimeSeries) Len() int { + return len(s) +} + +func (s SortableTimeSeries) Less(i, j int) bool { + k := 0 + for k < len(s[i].Labels) && k < len(s[j].Labels) { + // compare keys + if s[i].Labels[k].Name != s[j].Labels[k].Name { + return s[i].Labels[k].Name < s[j].Labels[k].Name + } + // compare values + if s[i].Labels[k].Value != s[j].Labels[k].Value { + return s[i].Labels[k].Value < s[j].Labels[k].Value + } + k += 1 + } + // default to preserving order + return true +} + +func (s SortableTimeSeries) Swap(i, j int) { + s[i], s[j] = s[j], s[i] +} + +func sortReadResponse(rr *prompb.ReadResponse) { + for _, q := range rr.Results { + sort.Sort(SortableTimeSeries(q.Timeseries)) + } +} + type fakeOwnedNamespaces struct { token2Namespaces map[string]data.Set }