From 20e3c295aea5992a4d8ea8e7e45ed9c9d6639813 Mon Sep 17 00:00:00 2001 From: "Xiaochao Dong (@damnever)" Date: Fri, 24 Mar 2023 17:18:24 +0800 Subject: [PATCH 1/4] Optimize constant label pair adding with relabel.Replace Signed-off-by: Xiaochao Dong (@damnever) --- model/relabel/relabel.go | 15 +++++++++ model/relabel/relabel_test.go | 60 +++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/model/relabel/relabel.go b/model/relabel/relabel.go index d29c3d07a..83b1a9147 100644 --- a/model/relabel/relabel.go +++ b/model/relabel/relabel.go @@ -267,6 +267,17 @@ 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) { + 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) // If there is no match no replacement must take place. if indexes == nil { @@ -316,3 +327,7 @@ func relabel(cfg *Config, lb *labels.Builder) (keep bool) { return true } + +func varInRegexTemplate(template string) bool { + return strings.Contains(template, "$") +} diff --git a/model/relabel/relabel_test.go b/model/relabel/relabel_test.go index 517b9b822..d3815afe6 100644 --- a/model/relabel/relabel_test.go +++ b/model/relabel/relabel_test.go @@ -15,6 +15,7 @@ package relabel import ( "fmt" + "sort" "testing" "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) + } +} From 2d0d3333712e596ec2f1f4e4596fdb7ae0466e6f Mon Sep 17 00:00:00 2001 From: "Xiaochao Dong (@damnever)" Date: Sat, 25 Mar 2023 10:42:20 +0800 Subject: [PATCH 2/4] Fix lint issue Signed-off-by: Xiaochao Dong (@damnever) --- model/relabel/relabel_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/model/relabel/relabel_test.go b/model/relabel/relabel_test.go index d3815afe6..bd51b9771 100644 --- a/model/relabel/relabel_test.go +++ b/model/relabel/relabel_test.go @@ -905,8 +905,7 @@ func BenchmarkRelabel_ReplaceAddLabel(b *testing.B) { labels.Label{Name: "abcdefg12", Value: "hijklmn12"}, labels.Label{Name: "abcdefg13", Value: "hijklmn13"}, } - actual, _ := Process(lset, cfgs...) - var _ = actual + _, _ = Process(lset, cfgs...) // require.Equal(b, actual, expectLset) } } From 1601b2a79e0116f9d3a3e30915a6899e73c96feb Mon Sep 17 00:00:00 2001 From: "Xiaochao Dong (@damnever)" Date: Wed, 29 Mar 2023 11:20:59 +0800 Subject: [PATCH 3/4] check new line in target Signed-off-by: Xiaochao Dong (@damnever) --- model/relabel/relabel.go | 5 +++++ model/relabel/relabel_test.go | 6 ++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/model/relabel/relabel.go b/model/relabel/relabel.go index 83b1a9147..7607138b5 100644 --- a/model/relabel/relabel.go +++ b/model/relabel/relabel.go @@ -269,6 +269,7 @@ func relabel(cfg *Config, lb *labels.Builder) (keep bool) { case Replace: // Fast path to add or delete label pair. if val == "" && cfg.Regex == DefaultRelabelConfig.Regex && + !containsNewLine(cfg.TargetLabel) && !varInRegexTemplate(cfg.TargetLabel) && !varInRegexTemplate(cfg.Replacement) { if !model.LabelName(cfg.TargetLabel).IsValid() || cfg.Replacement == "" { lb.Del(cfg.TargetLabel) @@ -331,3 +332,7 @@ func relabel(cfg *Config, lb *labels.Builder) (keep bool) { func varInRegexTemplate(template string) bool { return strings.Contains(template, "$") } + +func containsNewLine(s string) bool { + return strings.Contains(s, "\r\n") || strings.Contains(s, "\n") +} diff --git a/model/relabel/relabel_test.go b/model/relabel/relabel_test.go index bd51b9771..86844cf28 100644 --- a/model/relabel/relabel_test.go +++ b/model/relabel/relabel_test.go @@ -858,6 +858,8 @@ func BenchmarkRelabel_ReplaceAddLabel(b *testing.B) { "wwwwww": "wwwwww", "xxxxxxxxx": "xxxxxxxxx", "yyyyyyyyyyyy": "yyyyyyyyyyyy", + "new\nline1": "dropped", + "new\r\nline2": "dropped", "${0}": "dropped", "dropped": "${0}", } { @@ -905,7 +907,7 @@ func BenchmarkRelabel_ReplaceAddLabel(b *testing.B) { labels.Label{Name: "abcdefg12", Value: "hijklmn12"}, labels.Label{Name: "abcdefg13", Value: "hijklmn13"}, } - _, _ = Process(lset, cfgs...) - // require.Equal(b, actual, expectLset) + actual, _ := Process(lset, cfgs...) + require.Equal(b, actual, expectLset) } } From b3b5c0022e5c05088733519f104db2c8b5c54974 Mon Sep 17 00:00:00 2001 From: "Xiaochao Dong (@damnever)" Date: Mon, 25 Dec 2023 15:14:25 +0800 Subject: [PATCH 4/4] Simplify the logic as per the comments Signed-off-by: Xiaochao Dong (@damnever) --- model/relabel/relabel.go | 11 +---- model/relabel/relabel_test.go | 89 +++++++++++------------------------ 2 files changed, 29 insertions(+), 71 deletions(-) diff --git a/model/relabel/relabel.go b/model/relabel/relabel.go index 7607138b5..d169ed2f2 100644 --- a/model/relabel/relabel.go +++ b/model/relabel/relabel.go @@ -269,13 +269,8 @@ func relabel(cfg *Config, lb *labels.Builder) (keep bool) { case Replace: // Fast path to add or delete label pair. if val == "" && cfg.Regex == DefaultRelabelConfig.Regex && - !containsNewLine(cfg.TargetLabel) && !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) - } + lb.Set(cfg.TargetLabel, cfg.Replacement) break } @@ -332,7 +327,3 @@ func relabel(cfg *Config, lb *labels.Builder) (keep bool) { func varInRegexTemplate(template string) bool { return strings.Contains(template, "$") } - -func containsNewLine(s string) bool { - return strings.Contains(s, "\r\n") || strings.Contains(s, "\n") -} diff --git a/model/relabel/relabel_test.go b/model/relabel/relabel_test.go index 86844cf28..7652798f5 100644 --- a/model/relabel/relabel_test.go +++ b/model/relabel/relabel_test.go @@ -15,7 +15,6 @@ package relabel import ( "fmt" - "sort" "testing" "github.com/prometheus/common/model" @@ -838,6 +837,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) @@ -851,63 +878,3 @@ 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", - "new\nline1": "dropped", - "new\r\nline2": "dropped", - "${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...) - require.Equal(b, actual, expectLset) - } -}