Skip to content
This repository was archived by the owner on Jul 19, 2023. It is now read-only.

Commit 7818c72

Browse files
authored
Merge pull request #357 from grafana/20221024_sort-merge-results
Sort stacktraces by function names
2 parents 7de0c86 + 717368b commit 7818c72

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

pkg/model/stacktraces.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package model
22

33
import (
44
"encoding/binary"
5+
"sort"
6+
"strings"
57

68
"github.com/cespare/xxhash/v2"
79

@@ -90,6 +92,9 @@ func MergeBatchMergeStacktraces(responses ...*ingestv1.MergeProfilesStacktracesR
9092
result = &ingestv1.MergeProfilesStacktracesResult{}
9193
}
9294

95+
// sort stacktraces by function name
96+
sortStacktraces(result)
97+
9398
return result
9499
}
95100

@@ -114,3 +119,29 @@ func (h StacktracesHasher) Hashes(fnIds []int32) uint64 {
114119
}
115120
return h.hash.Sum64()
116121
}
122+
123+
// sortStacktraces sorts the stacktraces by function name
124+
func sortStacktraces(r *ingestv1.MergeProfilesStacktracesResult) {
125+
sort.Slice(r.Stacktraces, func(i, j int) bool {
126+
pos := 0
127+
for {
128+
// check slice lengths
129+
if pos >= len(r.Stacktraces[i].FunctionIds) {
130+
break
131+
}
132+
if pos >= len(r.Stacktraces[j].FunctionIds) {
133+
return false
134+
}
135+
136+
if diff := strings.Compare(r.FunctionNames[r.Stacktraces[i].FunctionIds[pos]], r.FunctionNames[r.Stacktraces[j].FunctionIds[pos]]); diff < 0 {
137+
break
138+
} else if diff > 0 {
139+
return false
140+
}
141+
pos++
142+
}
143+
144+
// when we get here, i is less than j
145+
return true
146+
})
147+
}

pkg/model/stacktraces_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,20 +93,20 @@ func TestMergeBatchResponse(t *testing.T) {
9393
expected: &ingestv1.MergeProfilesStacktracesResult{
9494
Stacktraces: []*ingestv1.StacktraceSample{
9595
{
96-
FunctionIds: []int32{0, 1},
97-
Value: 2,
98-
},
99-
{
100-
FunctionIds: []int32{0, 1, 2},
101-
Value: 6,
96+
FunctionIds: []int32{4},
97+
Value: 5,
10298
},
10399
{
104100
FunctionIds: []int32{3},
105101
Value: 4,
106102
},
107103
{
108-
FunctionIds: []int32{4},
109-
Value: 5,
104+
FunctionIds: []int32{0, 1},
105+
Value: 2,
106+
},
107+
{
108+
FunctionIds: []int32{0, 1, 2},
109+
Value: 6,
110110
},
111111
},
112112
FunctionNames: []string{"my", "other", "stack", "foo", "bar"},

0 commit comments

Comments
 (0)