Skip to content

Commit

Permalink
Adjust logger types (#3859)
Browse files Browse the repository at this point in the history
  • Loading branch information
anbraten authored Jul 1, 2024
1 parent 2fa9432 commit ba858d6
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 12 deletions.
4 changes: 3 additions & 1 deletion agent/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ const (
)

func (r *Runner) createLogger(_logger zerolog.Logger, uploads *sync.WaitGroup, workflow *rpc.Workflow) pipeline.Logger {
return func(step *backend.Step, rc io.Reader) error {
return func(step *backend.Step, rc io.ReadCloser) error {
defer rc.Close()

logger := _logger.With().
Str("image", step.Image).
Str("workflow_id", workflow.ID).
Expand Down
2 changes: 1 addition & 1 deletion cli/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ func convertPathForWindows(path string) string {
}

const maxLogLineLength = 1024 * 1024 // 1mb
var defaultLogger = pipeline.Logger(func(step *backendTypes.Step, rc io.Reader) error {
var defaultLogger = pipeline.Logger(func(step *backendTypes.Step, rc io.ReadCloser) error {
logWriter := NewLineWriter(step.Name, step.UUID)
return pipelineLog.CopyLineByLine(logWriter, rc, maxLogLineLength)
})
6 changes: 1 addition & 5 deletions pipeline/log/line_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type LineWriter struct {
}

// NewLineWriter returns a new line reader.
func NewLineWriter(peer rpc.Peer, stepUUID string, secret ...string) io.WriteCloser {
func NewLineWriter(peer rpc.Peer, stepUUID string, secret ...string) io.Writer {
lw := &LineWriter{
peer: peer,
stepUUID: stepUUID,
Expand Down Expand Up @@ -73,7 +73,3 @@ func (w *LineWriter) Write(p []byte) (n int, err error) {

return len(data), nil
}

func (w *LineWriter) Close() error {
return nil
}
1 change: 0 additions & 1 deletion pipeline/log/line_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func TestLineWriter(t *testing.T) {

secrets := []string{"world"}
lw := log.NewLineWriter(peer, "e9ea76a5-44a1-4059-9c4a-6956c478b26d", secrets...)
defer lw.Close()

_, err := lw.Write([]byte("hello world\n"))
assert.NoError(t, err)
Expand Down
5 changes: 2 additions & 3 deletions pipeline/log/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"io"
)

func writeChunks(dst io.WriteCloser, data []byte, size int) error {
func writeChunks(dst io.Writer, data []byte, size int) error {
if len(data) <= size {
_, err := dst.Write(data)
return err
Expand All @@ -41,9 +41,8 @@ func writeChunks(dst io.WriteCloser, data []byte, size int) error {
return nil
}

func CopyLineByLine(dst io.WriteCloser, src io.Reader, maxSize int) error {
func CopyLineByLine(dst io.Writer, src io.Reader, maxSize int) error {
r := bufio.NewReader(src)
defer dst.Close()

for {
// TODO: read til newline or maxSize directly
Expand Down
15 changes: 15 additions & 0 deletions pipeline/log/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,18 @@ func TestCopyLineByLineSizeLimit(t *testing.T) {

wg.Wait()
}

func TestStringReader(t *testing.T) {
r := io.NopCloser(strings.NewReader("123\n4567\n890"))

testWriter := &testWriter{
Mutex: &sync.Mutex{},
writes: make([]string, 0),
}

err := log.CopyLineByLine(testWriter, r, 1024)
assert.NoError(t, err)

writes := testWriter.GetWrites()
assert.Lenf(t, writes, 3, "expected 3 writes, got: %v", writes)
}
2 changes: 1 addition & 1 deletion pipeline/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ import (
)

// Logger handles the process logging.
type Logger func(*backend.Step, io.Reader) error
type Logger func(*backend.Step, io.ReadCloser) error

0 comments on commit ba858d6

Please sign in to comment.