Merge pull request #12180 from damnever/perf/relabel-add-label

Optimize constant label pair adding from relabeling.
This commit is contained in:
Bryan Boreham 2024-09-24 12:22:05 +01:00 committed by GitHub
commit a0f26febc2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 39 additions and 0 deletions

View file

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

View file

@ -861,6 +861,34 @@ func BenchmarkRelabel(b *testing.B) {
"__scrape_timeout__", "10s",
"job", "kubernetes-pods"),
},
{
name: "static label pair",
config: `
- replacement: wwwwww
target_label: wwwwww
- replacement: yyyyyyyyyyyy
target_label: xxxxxxxxx
- replacement: xxxxxxxxx
target_label: yyyyyyyyyyyy
- source_labels: ["something"]
target_label: with_source_labels
replacement: value
- replacement: dropped
target_label: ${0}
- replacement: ${0}
target_label: dropped`,
lbls: labels.FromStrings(
"abcdefg01", "hijklmn1",
"abcdefg02", "hijklmn2",
"abcdefg03", "hijklmn3",
"abcdefg04", "hijklmn4",
"abcdefg05", "hijklmn5",
"abcdefg06", "hijklmn6",
"abcdefg07", "hijklmn7",
"abcdefg08", "hijklmn8",
"job", "foo",
),
},
}
for i := range tests {
err := yaml.UnmarshalStrict([]byte(tests[i].config), &tests[i].cfgs)