Skip to content

Commit

Permalink
Fix replace missing adjacent capture groups
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan01252 committed Dec 3, 2021
1 parent 369a6ac commit 68709d6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion clients/pkg/logentry/stages/replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (r *replaceStage) getReplacedEntry(matchAllIndex [][]int, input string, td
return "", nil, err
}
st := buf.String()
if previousInputEndIndex == 0 || previousInputEndIndex < matchIndex[i] {
if previousInputEndIndex == 0 || previousInputEndIndex <= matchIndex[i] {
result += input[previousInputEndIndex:matchIndex[i]] + st
previousInputEndIndex = matchIndex[i+1]
}
Expand Down
16 changes: 16 additions & 0 deletions clients/pkg/logentry/stages/replace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,18 @@ pipeline_stages:
replace: ''
`

var testReplaceAdjacentCaptureGroups = `
---
pipeline_stages:
-
replace:
expression: '(a|b|c)'
replace: ''
`

var testReplaceLogLine = `11.11.11.11 - frank [25/Jan/2000:14:00:01 -0500] "GET /1986.js HTTP/1.1" 200 932 "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 GTB6"`
var testReplaceLogJSONLine = `{"time":"2019-01-01T01:00:00.000000001Z", "level": "info", "msg": "11.11.11.11 - \"POST /loki/api/push/ HTTP/1.1\" 200 932 \"-\" \"Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 GTB6\""}`
var testReplaceLogLineAdjacentCaptureGroups = `abc`

func TestPipeline_Replace(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -141,6 +151,12 @@ func TestPipeline_Replace(t *testing.T) {
map[string]interface{}{},
`11.11.11.11 - [25/Jan/2000:14:00:01 -0500] "GET /1986.js HTTP/1.1" 200 932 "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 GTB6"`,
},
"successfully run a pipeline with adjacent capture groups": {
testReplaceAdjacentCaptureGroups,
testReplaceLogLineAdjacentCaptureGroups,
map[string]interface{}{},
``,
},
}

for testName, testData := range tests {
Expand Down

0 comments on commit 68709d6

Please sign in to comment.