Skip to content

Commit

Permalink
Merge pull request prometheus#12180 from damnever/perf/relabel-add-label
Browse files Browse the repository at this point in the history
Optimize constant label pair adding from relabeling.
  • Loading branch information
bboreham authored Sep 24, 2024
2 parents c2bbabb + b3b5c00 commit a0f26fe
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
11 changes: 11 additions & 0 deletions model/relabel/relabel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 changes: 28 additions & 0 deletions model/relabel/relabel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a0f26fe

Please sign in to comment.