meter: add BuildLabels func that sorts and deletes duplicates
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
c6ba2a91e6
commit
650d167313
@ -88,6 +88,16 @@ func (k byKey) Swap(i, j int) {
|
|||||||
k[i*2+1], k[j*2+1] = k[j*2+1], k[i*2+1]
|
k[i*2+1], k[j*2+1] = k[j*2+1], k[i*2+1]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BuildLables used to sort labels and delete duplicates.
|
||||||
|
// Last value wins in case of duplicate label keys.
|
||||||
|
func BuildLabels(labels ...string) []string {
|
||||||
|
if len(labels)%2 == 1 {
|
||||||
|
labels = labels[:len(labels)-1]
|
||||||
|
}
|
||||||
|
sort.Sort(byKey(labels))
|
||||||
|
return labels
|
||||||
|
}
|
||||||
|
|
||||||
// BuildName used to combine metric with labels.
|
// BuildName used to combine metric with labels.
|
||||||
// If labels count is odd, drop last element
|
// If labels count is odd, drop last element
|
||||||
func BuildName(name string, labels ...string) string {
|
func BuildName(name string, labels ...string) string {
|
||||||
|
@ -14,12 +14,44 @@ func TestNoopMeter(t *testing.T) {
|
|||||||
cnt.Inc()
|
cnt.Inc()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testEq(a, b []string) bool {
|
||||||
|
if len(a) != len(b) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for i := range a {
|
||||||
|
if a[i] != b[i] {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBuildLabels(t *testing.T) {
|
||||||
|
type testData struct {
|
||||||
|
src []string
|
||||||
|
dst []string
|
||||||
|
}
|
||||||
|
|
||||||
|
data := []testData{
|
||||||
|
testData{
|
||||||
|
src: []string{"zerolabel", "value3", "firstlabel", "value2"},
|
||||||
|
dst: []string{"firstlabel", "value2", "zerolabel", "value3"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, d := range data {
|
||||||
|
if !testEq(d.dst, BuildLabels(d.src...)) {
|
||||||
|
t.Fatalf("slices not properly sorted: %v %v", d.dst, d.src)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestBuildName(t *testing.T) {
|
func TestBuildName(t *testing.T) {
|
||||||
data := map[string][]string{
|
data := map[string][]string{
|
||||||
// `my_metric{firstlabel="value2",zerolabel="value3"}`: []string{
|
`my_metric{firstlabel="value2",zerolabel="value3"}`: []string{
|
||||||
// "my_metric",
|
"my_metric",
|
||||||
// "zerolabel", "value3", "firstlabel", "value2",
|
"zerolabel", "value3", "firstlabel", "value2",
|
||||||
// },
|
},
|
||||||
`my_metric{broker="broker2",register="mdns",server="tcp"}`: []string{
|
`my_metric{broker="broker2",register="mdns",server="tcp"}`: []string{
|
||||||
"my_metric",
|
"my_metric",
|
||||||
"broker", "broker1", "broker", "broker2", "server", "http", "server", "tcp", "register", "mdns",
|
"broker", "broker1", "broker", "broker2", "server", "http", "server", "tcp", "register", "mdns",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user