check new line in target

Signed-off-by: Xiaochao Dong (@damnever) <the.xcdong@gmail.com>
This commit is contained in:
Xiaochao Dong (@damnever) 2023-03-29 11:20:59 +08:00
parent 2d0d333371
commit 1601b2a79e
2 changed files with 9 additions and 2 deletions

View file

@ -269,6 +269,7 @@ func relabel(cfg *Config, lb *labels.Builder) (keep bool) {
case Replace: case Replace:
// Fast path to add or delete label pair. // Fast path to add or delete label pair.
if val == "" && cfg.Regex == DefaultRelabelConfig.Regex && if val == "" && cfg.Regex == DefaultRelabelConfig.Regex &&
!containsNewLine(cfg.TargetLabel) &&
!varInRegexTemplate(cfg.TargetLabel) && !varInRegexTemplate(cfg.Replacement) { !varInRegexTemplate(cfg.TargetLabel) && !varInRegexTemplate(cfg.Replacement) {
if !model.LabelName(cfg.TargetLabel).IsValid() || cfg.Replacement == "" { if !model.LabelName(cfg.TargetLabel).IsValid() || cfg.Replacement == "" {
lb.Del(cfg.TargetLabel) lb.Del(cfg.TargetLabel)
@ -331,3 +332,7 @@ func relabel(cfg *Config, lb *labels.Builder) (keep bool) {
func varInRegexTemplate(template string) bool { func varInRegexTemplate(template string) bool {
return strings.Contains(template, "$") return strings.Contains(template, "$")
} }
func containsNewLine(s string) bool {
return strings.Contains(s, "\r\n") || strings.Contains(s, "\n")
}

View file

@ -858,6 +858,8 @@ func BenchmarkRelabel_ReplaceAddLabel(b *testing.B) {
"wwwwww": "wwwwww", "wwwwww": "wwwwww",
"xxxxxxxxx": "xxxxxxxxx", "xxxxxxxxx": "xxxxxxxxx",
"yyyyyyyyyyyy": "yyyyyyyyyyyy", "yyyyyyyyyyyy": "yyyyyyyyyyyy",
"new\nline1": "dropped",
"new\r\nline2": "dropped",
"${0}": "dropped", "${0}": "dropped",
"dropped": "${0}", "dropped": "${0}",
} { } {
@ -905,7 +907,7 @@ func BenchmarkRelabel_ReplaceAddLabel(b *testing.B) {
labels.Label{Name: "abcdefg12", Value: "hijklmn12"}, labels.Label{Name: "abcdefg12", Value: "hijklmn12"},
labels.Label{Name: "abcdefg13", Value: "hijklmn13"}, labels.Label{Name: "abcdefg13", Value: "hijklmn13"},
} }
_, _ = Process(lset, cfgs...) actual, _ := Process(lset, cfgs...)
// require.Equal(b, actual, expectLset) require.Equal(b, actual, expectLset)
} }
} }