mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-11-21 14:44:00 +00:00
lib/promrelabel: optimize labeldrop
and labelkeep
relabeling for prefix.*
and prefix.+
regexps
This commit is contained in:
parent
0144b164c7
commit
f8baf1a76d
3 changed files with 129 additions and 5 deletions
|
@ -334,7 +334,21 @@ func (prc *parsedRelabelConfig) matchString(s string) bool {
|
|||
if complete {
|
||||
return prefix == s
|
||||
}
|
||||
return strings.HasPrefix(s, prefix) && prc.Regex.MatchString(s)
|
||||
if !strings.HasPrefix(s, prefix) {
|
||||
return false
|
||||
}
|
||||
reStr := prc.regexOriginal.String()
|
||||
if strings.HasPrefix(reStr, prefix) {
|
||||
// Fast path for `foo.*` and `bar.+` regexps
|
||||
reSuffix := reStr[len(prefix):]
|
||||
switch reSuffix {
|
||||
case ".+", "(.+)":
|
||||
return len(s) > len(prefix)
|
||||
case ".*", "(.*)":
|
||||
return true
|
||||
}
|
||||
}
|
||||
return prc.Regex.MatchString(s)
|
||||
}
|
||||
|
||||
func (prc *parsedRelabelConfig) expandCaptureGroups(template, source string, match []int) string {
|
||||
|
|
|
@ -713,7 +713,7 @@ func TestApplyRelabelConfigs(t *testing.T) {
|
|||
},
|
||||
})
|
||||
})
|
||||
t.Run("labeldrop-regexp", func(t *testing.T) {
|
||||
t.Run("labeldrop-prefix", func(t *testing.T) {
|
||||
f(`
|
||||
- action: labeldrop
|
||||
regex: "dropme.*"
|
||||
|
@ -730,7 +730,49 @@ func TestApplyRelabelConfigs(t *testing.T) {
|
|||
})
|
||||
f(`
|
||||
- action: labeldrop
|
||||
regex: "dropme.*"
|
||||
regex: "dropme(.+)"
|
||||
`, []prompbmarshal.Label{
|
||||
{
|
||||
Name: "xxx",
|
||||
Value: "yyy",
|
||||
},
|
||||
{
|
||||
Name: "dropme-please",
|
||||
Value: "aaa",
|
||||
},
|
||||
{
|
||||
Name: "foo",
|
||||
Value: "bar",
|
||||
},
|
||||
}, false, []prompbmarshal.Label{
|
||||
{
|
||||
Name: "foo",
|
||||
Value: "bar",
|
||||
},
|
||||
{
|
||||
Name: "xxx",
|
||||
Value: "yyy",
|
||||
},
|
||||
})
|
||||
})
|
||||
t.Run("labeldrop-regexp", func(t *testing.T) {
|
||||
f(`
|
||||
- action: labeldrop
|
||||
regex: ".*dropme.*"
|
||||
`, []prompbmarshal.Label{
|
||||
{
|
||||
Name: "aaa",
|
||||
Value: "bbb",
|
||||
},
|
||||
}, true, []prompbmarshal.Label{
|
||||
{
|
||||
Name: "aaa",
|
||||
Value: "bbb",
|
||||
},
|
||||
})
|
||||
f(`
|
||||
- action: labeldrop
|
||||
regex: ".*dropme.*"
|
||||
`, []prompbmarshal.Label{
|
||||
{
|
||||
Name: "xxx",
|
||||
|
|
|
@ -495,10 +495,44 @@ func BenchmarkApplyRelabelConfigs(b *testing.B) {
|
|||
}
|
||||
})
|
||||
})
|
||||
b.Run("labeldrop-match-regexp", func(b *testing.B) {
|
||||
b.Run("labeldrop-match-prefix", func(b *testing.B) {
|
||||
pcs := mustParseRelabelConfigs(`
|
||||
- action: labeldrop
|
||||
regex: "id.*"
|
||||
`)
|
||||
labelsOrig := []prompbmarshal.Label{
|
||||
{
|
||||
Name: "__name__",
|
||||
Value: "metric",
|
||||
},
|
||||
{
|
||||
Name: "id",
|
||||
Value: "foobar-random-string-here",
|
||||
},
|
||||
}
|
||||
b.ReportAllocs()
|
||||
b.SetBytes(1)
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
var labels []prompbmarshal.Label
|
||||
for pb.Next() {
|
||||
labels = append(labels[:0], labelsOrig...)
|
||||
labels = pcs.Apply(labels, 0, true)
|
||||
if len(labels) != 1 {
|
||||
panic(fmt.Errorf("unexpected number of labels; got %d; want %d; labels:\n%#v", len(labels), 1, labels))
|
||||
}
|
||||
if labels[0].Name != "__name__" {
|
||||
panic(fmt.Errorf("unexpected label name; got %q; want %q", labels[0].Name, "__name__"))
|
||||
}
|
||||
if labels[0].Value != "metric" {
|
||||
panic(fmt.Errorf("unexpected label value; got %q; want %q", labels[0].Value, "metric"))
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
b.Run("labeldrop-match-regexp", func(b *testing.B) {
|
||||
pcs := mustParseRelabelConfigs(`
|
||||
- action: labeldrop
|
||||
regex: ".*id.*"
|
||||
`)
|
||||
labelsOrig := []prompbmarshal.Label{
|
||||
{
|
||||
|
@ -591,10 +625,44 @@ func BenchmarkApplyRelabelConfigs(b *testing.B) {
|
|||
}
|
||||
})
|
||||
})
|
||||
b.Run("labelkeep-match-regexp", func(b *testing.B) {
|
||||
b.Run("labelkeep-match-prefix", func(b *testing.B) {
|
||||
pcs := mustParseRelabelConfigs(`
|
||||
- action: labelkeep
|
||||
regex: "id.*"
|
||||
`)
|
||||
labelsOrig := []prompbmarshal.Label{
|
||||
{
|
||||
Name: "__name__",
|
||||
Value: "metric",
|
||||
},
|
||||
{
|
||||
Name: "id",
|
||||
Value: "foobar-random-string-here",
|
||||
},
|
||||
}
|
||||
b.ReportAllocs()
|
||||
b.SetBytes(1)
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
var labels []prompbmarshal.Label
|
||||
for pb.Next() {
|
||||
labels = append(labels[:0], labelsOrig...)
|
||||
labels = pcs.Apply(labels, 0, true)
|
||||
if len(labels) != 1 {
|
||||
panic(fmt.Errorf("unexpected number of labels; got %d; want %d; labels:\n%#v", len(labels), 1, labels))
|
||||
}
|
||||
if labels[0].Name != "id" {
|
||||
panic(fmt.Errorf("unexpected label name; got %q; want %q", labels[0].Name, "id"))
|
||||
}
|
||||
if labels[0].Value != "foobar-random-string-here" {
|
||||
panic(fmt.Errorf("unexpected label value; got %q; want %q", labels[0].Value, "foobar-random-string-here"))
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
b.Run("labelkeep-match-regexp", func(b *testing.B) {
|
||||
pcs := mustParseRelabelConfigs(`
|
||||
- action: labelkeep
|
||||
regex: ".*id.*"
|
||||
`)
|
||||
labelsOrig := []prompbmarshal.Label{
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue