diff --git a/filebeat/reader/docker_json/docker_json.go b/filebeat/reader/docker_json/docker_json.go index daace2aebd1..d8c6a8b7da8 100644 --- a/filebeat/reader/docker_json/docker_json.go +++ b/filebeat/reader/docker_json/docker_json.go @@ -20,6 +20,7 @@ package docker_json import ( "bytes" "encoding/json" + "runtime" "time" "github.com/elastic/beats/filebeat/reader" @@ -42,6 +43,8 @@ type Reader struct { // parse CRI flags criflags bool + + stripNewLine func(msg *reader.Message) } type logLine struct { @@ -54,13 +57,21 @@ type logLine struct { // New creates a new reader renaming a field func New(r reader.Reader, stream string, partial bool, forceCRI bool, CRIFlags bool) *Reader { - return &Reader{ + reader := Reader{ stream: stream, partial: partial, reader: r, forceCRI: forceCRI, criflags: CRIFlags, } + + if runtime.GOOS == "windows" { + reader.stripNewLine = stripNewLineWin + } else { + reader.stripNewLine = stripNewLine + } + + return &reader } // parseCRILog parses logs in CRI log format. @@ -112,7 +123,7 @@ func (p *Reader) parseCRILog(message *reader.Message, msg *logLine) error { // Remove \n ending for partial messages message.Content = log[i] if partial { - stripNewLine(message) + p.stripNewLine(message) } return nil @@ -192,3 +203,16 @@ func (p *Reader) Next() (reader.Message, error) { return message, err } } + +func stripNewLine(msg *reader.Message) { + l := len(msg.Content) + if l > 0 && msg.Content[l-1] == '\n' { + msg.Content = msg.Content[:l-1] + } +} + +func stripNewLineWin(msg *reader.Message) { + msg.Content = bytes.TrimRightFunc(msg.Content, func(r rune) bool { + return r == '\n' || r == '\r' + }) +} diff --git a/filebeat/reader/docker_json/docker_json_unix.go b/filebeat/reader/docker_json/docker_json_unix.go deleted file mode 100644 index 9466155c6de..00000000000 --- a/filebeat/reader/docker_json/docker_json_unix.go +++ /dev/null @@ -1,30 +0,0 @@ -// Licensed to Elasticsearch B.V. under one or more contributor -// license agreements. See the NOTICE file distributed with -// this work for additional information regarding copyright -// ownership. Elasticsearch B.V. licenses this file to you under -// the Apache License, Version 2.0 (the "License"); you may -// not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. -// +build linux darwin - -package docker_json - -import ( - "github.com/elastic/beats/filebeat/reader" -) - -func stripNewLine(msg *reader.Message) { - l := len(msg.Content) - if l > 0 && msg.Content[l-1] == '\n' { - msg.Content = msg.Content[:l-1] - } -} diff --git a/filebeat/reader/docker_json/docker_json_windows.go b/filebeat/reader/docker_json/docker_json_windows.go deleted file mode 100644 index a6d80a1f218..00000000000 --- a/filebeat/reader/docker_json/docker_json_windows.go +++ /dev/null @@ -1,30 +0,0 @@ -// Licensed to Elasticsearch B.V. under one or more contributor -// license agreements. See the NOTICE file distributed with -// this work for additional information regarding copyright -// ownership. Elasticsearch B.V. licenses this file to you under -// the Apache License, Version 2.0 (the "License"); you may -// not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. - -package docker_json - -import ( - "bytes" - - "github.com/elastic/beats/filebeat/reader" -) - -func stripNewLine(msg *reader.Message) { - msg.Content = bytes.TrimRightFunc(msg.Content, func(r rune) bool { - return r == '\n' || r == '\r' - }) -}