Merge pull request #12180 from damnever/perf/relabel-add-label
Optimize constant label pair adding from relabeling.
This commit is contained in:
commit
a0f26febc2
@ -277,6 +277,13 @@ 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) {
|
||||||
|
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 {
|
||||||
@ -326,3 +333,7 @@ func relabel(cfg *Config, lb *labels.Builder) (keep bool) {
|
|||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func varInRegexTemplate(template string) bool {
|
||||||
|
return strings.Contains(template, "$")
|
||||||
|
}
|
||||||
|
@ -861,6 +861,34 @@ func BenchmarkRelabel(b *testing.B) {
|
|||||||
"__scrape_timeout__", "10s",
|
"__scrape_timeout__", "10s",
|
||||||
"job", "kubernetes-pods"),
|
"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 {
|
for i := range tests {
|
||||||
err := yaml.UnmarshalStrict([]byte(tests[i].config), &tests[i].cfgs)
|
err := yaml.UnmarshalStrict([]byte(tests[i].config), &tests[i].cfgs)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user