From 1601b2a79e0116f9d3a3e30915a6899e73c96feb Mon Sep 17 00:00:00 2001 From: "Xiaochao Dong (@damnever)" Date: Wed, 29 Mar 2023 11:20:59 +0800 Subject: [PATCH] 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) } }