2020-04-23 08:34:04 +00:00
|
|
|
package discoveryutils
|
|
|
|
|
|
|
|
import (
|
2022-11-30 05:22:12 +00:00
|
|
|
"reflect"
|
2020-04-23 08:34:04 +00:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
2022-11-30 05:22:12 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2020-04-24 14:50:21 +00:00
|
|
|
|
2022-09-28 07:39:01 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
2022-11-30 05:22:12 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
|
2020-04-23 08:34:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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 {
|
2022-09-28 07:39:01 +00:00
|
|
|
return labelNamesSanitizer.Transform(name)
|
2020-04-23 08:34:04 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 07:39:01 +00:00
|
|
|
var labelNamesSanitizer = bytesutil.NewFastStringTransformer(func(s string) string {
|
2024-07-17 10:18:38 +00:00
|
|
|
return invalidLabelCharRE.ReplaceAllLiteralString(s, "_")
|
2022-09-28 07:39:01 +00:00
|
|
|
})
|
2022-08-26 21:12:39 +00:00
|
|
|
|
2022-09-28 07:39:01 +00:00
|
|
|
var invalidLabelCharRE = regexp.MustCompile(`[^a-zA-Z0-9_]`)
|
2022-08-26 21:12:39 +00:00
|
|
|
|
2020-04-23 08:34:04 +00:00
|
|
|
// JoinHostPort returns host:port.
|
|
|
|
//
|
|
|
|
// Host may be dns name, ipv4 or ipv6 address.
|
|
|
|
func JoinHostPort(host string, port int) string {
|
2022-11-30 05:22:12 +00:00
|
|
|
bb := bbPool.Get()
|
|
|
|
b := bb.B[:0]
|
|
|
|
isIPv6 := strings.IndexByte(host, ':') >= 0
|
|
|
|
if isIPv6 {
|
|
|
|
b = append(b, '[')
|
2021-10-19 10:19:18 +00:00
|
|
|
}
|
2022-11-30 05:22:12 +00:00
|
|
|
b = append(b, host...)
|
|
|
|
if isIPv6 {
|
|
|
|
b = append(b, ']')
|
2020-04-24 14:50:21 +00:00
|
|
|
}
|
2022-11-30 05:22:12 +00:00
|
|
|
b = append(b, ':')
|
|
|
|
b = strconv.AppendInt(b, int64(port), 10)
|
2023-01-04 06:14:20 +00:00
|
|
|
s := bytesutil.InternBytes(b)
|
2022-11-30 05:22:12 +00:00
|
|
|
bb.B = b
|
|
|
|
bbPool.Put(bb)
|
|
|
|
return s
|
2020-04-24 14:50:21 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 05:22:12 +00:00
|
|
|
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)
|
2020-04-24 14:50:21 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-04 09:36:21 +00:00
|
|
|
|
|
|
|
// AddTagsToLabels adds <prefix>_tags (separated with tagSeparator) to labels
|
|
|
|
// and exposes individual tags via <prefix>_tag_* labels, so users could move all the tags
|
|
|
|
// into the discovered scrape target with the following relabeling rule in the way similar to kubernetes_sd_configs:
|
|
|
|
//
|
|
|
|
// - action: labelmap
|
|
|
|
// regex: <prefix>_tag_(.+)
|
|
|
|
//
|
|
|
|
// This solves https://stackoverflow.com/questions/44339461/relabeling-in-prometheus
|
|
|
|
func AddTagsToLabels(m *promutils.Labels, tags []string, prefix, tagSeparator string) {
|
|
|
|
// We surround the separated list with the separator as well. This way regular expressions
|
|
|
|
// in relabeling rules don't have to consider tag positions.
|
|
|
|
m.Add(prefix+"tags", tagSeparator+strings.Join(tags, tagSeparator)+tagSeparator)
|
|
|
|
|
|
|
|
for _, tag := range tags {
|
|
|
|
k := tag
|
|
|
|
v := ""
|
|
|
|
if n := strings.IndexByte(tag, '='); n >= 0 {
|
|
|
|
k = tag[:n]
|
|
|
|
v = tag[n+1:]
|
|
|
|
}
|
|
|
|
m.Add(SanitizeLabelName(prefix+"tag_"+k), v)
|
|
|
|
m.Add(SanitizeLabelName(prefix+"tagpresent_"+k), "true")
|
|
|
|
}
|
|
|
|
}
|