optimize cri redirect logs

Signed-off-by: Zhiyu Li <payall4u@qq.com>
This commit is contained in:
payall4u 2021-03-30 11:51:55 +08:00
parent d064140369
commit 4bc8f692fc

View File

@ -122,17 +122,29 @@ func redirectLogs(path string, rc io.ReadCloser, w io.Writer, s StreamType, maxL
buf [][]byte buf [][]byte
length int length int
bufSize = defaultBufSize bufSize = defaultBufSize
timeBuffer = make([]byte, len(timestampFormat))
lineBuffer = bytes.Buffer{}
) )
// Make sure bufSize <= maxLen // Make sure bufSize <= maxLen
if maxLen > 0 && maxLen < bufSize { if maxLen > 0 && maxLen < bufSize {
bufSize = maxLen bufSize = maxLen
} }
r := bufio.NewReaderSize(rc, bufSize) r := bufio.NewReaderSize(rc, bufSize)
writeLine := func(tag, line []byte) { writeLineBuffer := func(tag []byte, lineBytes [][]byte) {
timestamp := time.Now().AppendFormat(nil, timestampFormat) timeBuffer = time.Now().AppendFormat(timeBuffer[:0], timestampFormat)
data := bytes.Join([][]byte{timestamp, stream, tag, line}, delimiter) headers := [][]byte{timeBuffer, stream, tag}
data = append(data, eol)
if _, err := w.Write(data); err != nil { lineBuffer.Reset()
for _, h := range headers {
lineBuffer.Write(h)
lineBuffer.Write(delimiter)
}
for _, l := range lineBytes {
lineBuffer.Write(l)
}
lineBuffer.WriteByte(eol)
if _, err := lineBuffer.WriteTo(w); err != nil {
logrus.WithError(err).Errorf("Fail to write %q log to log file %q", s, path) logrus.WithError(err).Errorf("Fail to write %q log to log file %q", s, path)
// Continue on write error to drain the container output. // Continue on write error to drain the container output.
} }
@ -171,7 +183,7 @@ func redirectLogs(path string, rc io.ReadCloser, w io.Writer, s StreamType, maxL
panic("exceed length should <= last buffer size") panic("exceed length should <= last buffer size")
} }
buf[len(buf)-1] = last[:len(last)-exceedLen] buf[len(buf)-1] = last[:len(last)-exceedLen]
writeLine(partial, bytes.Join(buf, nil)) writeLineBuffer(partial, buf)
buf = [][]byte{last[len(last)-exceedLen:]} buf = [][]byte{last[len(last)-exceedLen:]}
length = exceedLen length = exceedLen
} }
@ -182,9 +194,9 @@ func redirectLogs(path string, rc io.ReadCloser, w io.Writer, s StreamType, maxL
// readLine only returns error when the message doesn't // readLine only returns error when the message doesn't
// end with a newline, in that case it should be treated // end with a newline, in that case it should be treated
// as a partial line. // as a partial line.
writeLine(partial, bytes.Join(buf, nil)) writeLineBuffer(partial, buf)
} else { } else {
writeLine(full, bytes.Join(buf, nil)) writeLineBuffer(full, buf)
} }
buf = nil buf = nil
length = 0 length = 0