Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmccluskey committed Sep 12, 2023
1 parent 0dc7314 commit cafbbcd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
7 changes: 4 additions & 3 deletions sdks/go/container/tools/buffered_logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,18 @@ type BufferedLogger struct {
lastFlush time.Time
flushInterval time.Duration
periodicFlushContext context.Context
now func() time.Time
}

// NewBufferedLogger returns a new BufferedLogger type by reference.
func NewBufferedLogger(logger *Logger) *BufferedLogger {
return &BufferedLogger{logger: logger, lastFlush: time.Now(), flushInterval: time.Duration(math.MaxInt64), periodicFlushContext: context.Background()}
return &BufferedLogger{logger: logger, lastFlush: time.Now(), flushInterval: time.Duration(math.MaxInt64), periodicFlushContext: context.Background(), now: time.Now}
}

// NewBufferedLoggerWithFlushInterval returns a new BufferedLogger type by reference. This type will
// flush logs periodically on Write() calls as well as when Flush*() functions are called.
func NewBufferedLoggerWithFlushInterval(ctx context.Context, logger *Logger, interval time.Duration) *BufferedLogger {
return &BufferedLogger{logger: logger, lastFlush: time.Now(), flushInterval: interval, periodicFlushContext: ctx}
return &BufferedLogger{logger: logger, lastFlush: time.Now(), flushInterval: interval, periodicFlushContext: ctx, now: time.Now}
}

// Write implements the io.Writer interface, converting input to a string
Expand All @@ -62,7 +63,7 @@ func (b *BufferedLogger) Write(p []byte) (int, error) {
}
b.logs = append(b.logs, b.builder.String())
b.builder.Reset()
if time.Since(b.lastFlush) > b.flushInterval {
if b.now().Sub(b.lastFlush) > b.flushInterval {
b.FlushAtDebug(b.periodicFlushContext)
}
return n, err
Expand Down
28 changes: 22 additions & 6 deletions sdks/go/container/tools/buffered_logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,18 @@ func TestBufferedLogger(t *testing.T) {
t.Run("debug flush at interval", func(t *testing.T) {
catcher := &logCatcher{}
l := &Logger{client: catcher}
interval := time.Duration(5e9)
interval := 5 * time.Second
bl := NewBufferedLoggerWithFlushInterval(context.Background(), l, interval)

messages := []string{"foo", "bar", "baz"}
startTime := time.Now()
bl.now = func() time.Time { return startTime }

for _, message := range messages {
// Pause for four seconds before each message
time.Sleep(2e9)
messages := []string{"foo", "bar"}

for i, message := range messages {
if i > 1 {
bl.now = func() time.Time { return startTime.Add(6 * time.Second) }
}
messBytes := []byte(message)
n, err := bl.Write(messBytes)

Expand All @@ -208,9 +212,21 @@ func TestBufferedLogger(t *testing.T) {
}
}

lastMessage := "baz"
bl.now = func() time.Time { return startTime.Add(6 * time.Second) }
messBytes := []byte(lastMessage)
n, err := bl.Write(messBytes)

if err != nil {
t.Errorf("got error %v", err)
}
if got, want := n, len(messBytes); got != want {
t.Errorf("got %d bytes written, want %d", got, want)
}

// Type should have auto-flushed at debug after the third message
// (12 seconds > 10 second interval)
received := catcher.msgs[0].GetLogEntries()
messages = append(messages, lastMessage)

for i, message := range received {
if got, want := message.Message, messages[i]; got != want {
Expand Down
2 changes: 1 addition & 1 deletion sdks/python/container/piputil.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func isPackageInstalled(pkgName string) bool {
return true
}

const pipLogFlushInterval time.Duration = time.Duration(5e9)
const pipLogFlushInterval time.Duration = 5 * time.Second

// pipInstallPackage installs the given package, if present.
func pipInstallPackage(ctx context.Context, logger *tools.Logger, files []string, dir, name string, force, optional bool, extras []string) error {
Expand Down

0 comments on commit cafbbcd

Please sign in to comment.