-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
utils.go
64 lines (56 loc) · 1.53 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package discoveryutils
import (
"reflect"
"regexp"
"strconv"
"strings"
"testing"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
)
// SanitizeLabelName replaces anything that doesn't match
// client_label.LabelNameRE with an underscore.
//
// This has been copied from Prometheus sources at util/strutil/strconv.go
func SanitizeLabelName(name string) string {
return labelNamesSanitizer.Transform(name)
}
var labelNamesSanitizer = bytesutil.NewFastStringTransformer(func(s string) string {
return invalidLabelCharRE.ReplaceAllString(s, "_")
})
var invalidLabelCharRE = regexp.MustCompile(`[^a-zA-Z0-9_]`)
// JoinHostPort returns host:port.
//
// Host may be dns name, ipv4 or ipv6 address.
func JoinHostPort(host string, port int) string {
bb := bbPool.Get()
b := bb.B[:0]
isIPv6 := strings.IndexByte(host, ':') >= 0
if isIPv6 {
b = append(b, '[')
}
b = append(b, host...)
if isIPv6 {
b = append(b, ']')
}
b = append(b, ':')
b = strconv.AppendInt(b, int64(port), 10)
s := bytesutil.InternBytes(b)
bb.B = b
bbPool.Put(bb)
return s
}
var bbPool bytesutil.ByteBufferPool
// TestEqualLabelss tests whether got are equal to want.
func TestEqualLabelss(t *testing.T, got, want []*promutils.Labels) {
t.Helper()
var gotCopy []*promutils.Labels
for _, labels := range got {
labels = labels.Clone()
labels.Sort()
gotCopy = append(gotCopy, labels)
}
if !reflect.DeepEqual(gotCopy, want) {
t.Fatalf("unexpected labels:\ngot\n%v\nwant\n%v", gotCopy, want)
}
}