Browse Source

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

Optimize constant label pair adding from relabeling.
pull/14970/head
Bryan Boreham 2 months ago committed by GitHub
parent
commit
a0f26febc2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 11
      model/relabel/relabel.go
  2. 28
      model/relabel/relabel_test.go

11
model/relabel/relabel.go

@ -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, "$")
}

28
model/relabel/relabel_test.go

@ -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)

Loading…
Cancel
Save