Skip to content

Commit 3c6be6d

Browse files
authored
ruler/decoder: ensure labels order (#6682)
Ensure that the labels are in order because we are iterating over map[string]string which has a random sorting order. I think the consequence of not sorting this is that when using a frontend directly, the write requests might have unsorted labels in them. Signed-off-by: Giedrius Statkevičius <[email protected]>
1 parent 92787cb commit 3c6be6d

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

pkg/ruler/frontend_decoder.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8+
"sort"
89

910
"github.com/prometheus/common/model"
1011
"github.com/prometheus/prometheus/model/labels"
@@ -82,6 +83,9 @@ func (j JsonDecoder) vectorToPromQLVector(vector model.Vector) promql.Vector {
8283
Value: string(v),
8384
})
8485
}
86+
sort.Slice(metric, func(i, j int) bool {
87+
return metric[i].Name < metric[j].Name
88+
})
8589
v = append(v, promql.Sample{
8690
T: int64(sample.Timestamp),
8791
F: float64(sample.Value),

pkg/ruler/frontend_decoder_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"testing"
66

7+
"github.com/prometheus/common/model"
78
"github.com/prometheus/prometheus/model/histogram"
89
"github.com/prometheus/prometheus/model/labels"
910
"github.com/prometheus/prometheus/promql"
@@ -13,6 +14,28 @@ import (
1314
"github.com/cortexproject/cortex/pkg/querier/tripperware"
1415
)
1516

17+
func TestDecodeLabelOrder(t *testing.T) {
18+
j := JsonDecoder{}
19+
20+
decoded := j.vectorToPromQLVector(
21+
model.Vector{
22+
{
23+
Metric: model.Metric{
24+
"foo": "bar",
25+
"a": "b",
26+
"b": "b",
27+
},
28+
Timestamp: 1724146338123,
29+
Value: 1.234,
30+
},
31+
},
32+
)
33+
require.Equal(t, 1, len(decoded))
34+
require.Equal(t, int64(1724146338123), decoded[0].T)
35+
require.Equal(t, 1.234, decoded[0].F)
36+
require.Equal(t, labels.FromStrings("a", "b", "b", "b", "foo", "bar"), decoded[0].Metric)
37+
}
38+
1639
func TestProtoDecode(t *testing.T) {
1740
tests := []struct {
1841
description string

0 commit comments

Comments
 (0)