meter: disable auto sorting labels
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
@@ -4,8 +4,8 @@ package meter
|
|||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -117,6 +117,39 @@ func BuildLabels(labels ...string) []string {
|
|||||||
return labels
|
return labels
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var spool = newStringsPool(500)
|
||||||
|
|
||||||
|
type stringsPool struct {
|
||||||
|
p *sync.Pool
|
||||||
|
c int
|
||||||
|
}
|
||||||
|
|
||||||
|
func newStringsPool(size int) *stringsPool {
|
||||||
|
p := &stringsPool{c: size}
|
||||||
|
p.p = &sync.Pool{
|
||||||
|
New: func() interface{} {
|
||||||
|
return &strings.Builder{}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *stringsPool) Cap() int {
|
||||||
|
return p.c
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *stringsPool) Get() *strings.Builder {
|
||||||
|
return p.p.Get().(*strings.Builder)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *stringsPool) Put(b *strings.Builder) {
|
||||||
|
if b.Cap() > p.c {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
b.Reset()
|
||||||
|
p.p.Put(b)
|
||||||
|
}
|
||||||
|
|
||||||
// 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 {
|
||||||
@@ -125,8 +158,6 @@ func BuildName(name string, labels ...string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(labels) > 2 {
|
if len(labels) > 2 {
|
||||||
sort.Sort(byKey(labels))
|
|
||||||
|
|
||||||
idx := 0
|
idx := 0
|
||||||
for {
|
for {
|
||||||
if labels[idx] == labels[idx+2] {
|
if labels[idx] == labels[idx+2] {
|
||||||
@@ -141,7 +172,9 @@ func BuildName(name string, labels ...string) string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var b strings.Builder
|
b := spool.Get()
|
||||||
|
defer spool.Put(b)
|
||||||
|
|
||||||
_, _ = b.WriteString(name)
|
_, _ = b.WriteString(name)
|
||||||
_, _ = b.WriteRune('{')
|
_, _ = b.WriteRune('{')
|
||||||
for idx := 0; idx < len(labels); idx += 2 {
|
for idx := 0; idx < len(labels); idx += 2 {
|
||||||
@@ -149,8 +182,9 @@ func BuildName(name string, labels ...string) string {
|
|||||||
_, _ = b.WriteRune(',')
|
_, _ = b.WriteRune(',')
|
||||||
}
|
}
|
||||||
_, _ = b.WriteString(labels[idx])
|
_, _ = b.WriteString(labels[idx])
|
||||||
_, _ = b.WriteString(`=`)
|
_, _ = b.WriteString(`="`)
|
||||||
_, _ = b.WriteString(strconv.Quote(labels[idx+1]))
|
_, _ = b.WriteString(labels[idx+1])
|
||||||
|
_, _ = b.WriteRune('"')
|
||||||
}
|
}
|
||||||
_, _ = b.WriteRune('}')
|
_, _ = b.WriteRune('}')
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user