Optimize constant label pair adding with relabel.Replace

Signed-off-by: Xiaochao Dong (@damnever) <the.xcdong@gmail.com>
This commit is contained in:
Xiaochao Dong (@damnever) 2023-03-24 17:18:24 +08:00
parent 0763ec841b
commit 20e3c295ae
2 changed files with 75 additions and 0 deletions

View file

@ -267,6 +267,17 @@ func relabel(cfg *Config, lb *labels.Builder) (keep bool) {
return false return false
} }
case Replace: case Replace:
// Fast path to add or delete label pair.
if val == "" && cfg.Regex == DefaultRelabelConfig.Regex &&
!varInRegexTemplate(cfg.TargetLabel) && !varInRegexTemplate(cfg.Replacement) {
if !model.LabelName(cfg.TargetLabel).IsValid() || cfg.Replacement == "" {
lb.Del(cfg.TargetLabel)
} else {
lb.Set(cfg.TargetLabel, cfg.Replacement)
}
break
}
indexes := cfg.Regex.FindStringSubmatchIndex(val) indexes := cfg.Regex.FindStringSubmatchIndex(val)
// If there is no match no replacement must take place. // If there is no match no replacement must take place.
if indexes == nil { if indexes == nil {
@ -316,3 +327,7 @@ func relabel(cfg *Config, lb *labels.Builder) (keep bool) {
return true return true
} }
func varInRegexTemplate(template string) bool {
return strings.Contains(template, "$")
}

View file

@ -15,6 +15,7 @@ package relabel
import ( import (
"fmt" "fmt"
"sort"
"testing" "testing"
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
@ -850,3 +851,62 @@ func BenchmarkRelabel(b *testing.B) {
}) })
} }
} }
func BenchmarkRelabel_ReplaceAddLabel(b *testing.B) {
cfgs := []*Config{}
for k, v := range map[string]string{
"wwwwww": "wwwwww",
"xxxxxxxxx": "xxxxxxxxx",
"yyyyyyyyyyyy": "yyyyyyyyyyyy",
"${0}": "dropped",
"dropped": "${0}",
} {
cfgs = append(cfgs, &Config{
Action: DefaultRelabelConfig.Action,
Separator: DefaultRelabelConfig.Separator,
Regex: DefaultRelabelConfig.Regex,
TargetLabel: k,
Replacement: v,
})
}
expectLset := labels.Labels{
labels.Label{Name: "abcdefg01", Value: "hijklmn1"},
labels.Label{Name: "abcdefg02", Value: "hijklmn2"},
labels.Label{Name: "abcdefg03", Value: "hijklmn3"},
labels.Label{Name: "abcdefg04", Value: "hijklmn4"},
labels.Label{Name: "abcdefg05", Value: "hijklmn5"},
labels.Label{Name: "abcdefg06", Value: "hijklmn6"},
labels.Label{Name: "abcdefg07", Value: "hijklmn7"},
labels.Label{Name: "abcdefg08", Value: "hijklmn8"},
labels.Label{Name: "abcdefg09", Value: "hijklmn9"},
labels.Label{Name: "abcdefg10", Value: "hijklmn10"},
labels.Label{Name: "abcdefg11", Value: "hijklmn11"},
labels.Label{Name: "abcdefg12", Value: "hijklmn12"},
labels.Label{Name: "abcdefg13", Value: "hijklmn13"},
labels.Label{Name: "wwwwww", Value: "wwwwww"},
labels.Label{Name: "xxxxxxxxx", Value: "xxxxxxxxx"},
labels.Label{Name: "yyyyyyyyyyyy", Value: "yyyyyyyyyyyy"},
}
sort.Sort(expectLset)
for i := 0; i < b.N; i++ {
lset := labels.Labels{
labels.Label{Name: "abcdefg01", Value: "hijklmn1"},
labels.Label{Name: "abcdefg02", Value: "hijklmn2"},
labels.Label{Name: "abcdefg03", Value: "hijklmn3"},
labels.Label{Name: "abcdefg04", Value: "hijklmn4"},
labels.Label{Name: "abcdefg05", Value: "hijklmn5"},
labels.Label{Name: "abcdefg06", Value: "hijklmn6"},
labels.Label{Name: "abcdefg07", Value: "hijklmn7"},
labels.Label{Name: "abcdefg08", Value: "hijklmn8"},
labels.Label{Name: "abcdefg09", Value: "hijklmn9"},
labels.Label{Name: "abcdefg10", Value: "hijklmn10"},
labels.Label{Name: "abcdefg11", Value: "hijklmn11"},
labels.Label{Name: "abcdefg12", Value: "hijklmn12"},
labels.Label{Name: "abcdefg13", Value: "hijklmn13"},
}
actual, _ := Process(lset, cfgs...)
var _ = actual
// require.Equal(b, actual, expectLset)
}
}