Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promtail: Fix replace missing adjacent capture groups #4874

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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