Skip to content

Commit c1b70ef

Browse files
committed
Fix logging logic
Improves logging logic according to the review points raised by copilot
1 parent 93ba20e commit c1b70ef

1 file changed

Lines changed: 23 additions & 18 deletions

File tree

sdks/go/pkg/beam/runners/prism/internal/environments.go

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -254,27 +254,17 @@ func dockerEnvironment(ctx context.Context, logger *slog.Logger, dp *pipepb.Dock
254254
bgctx := context.Background()
255255

256256
// Wait for either context cancellation or container to stop
257-
done := make(chan struct{})
257+
type waitResult struct {
258+
err error
259+
}
260+
done := make(chan waitResult)
258261
go func() {
259262
result := cli.ContainerWait(bgctx, containerID, dcli.ContainerWaitOptions{
260263
Condition: container.WaitConditionNotRunning,
261264
})
262-
if result.Error != nil {
263-
logger.Error("docker container wait error", "error", result.Error)
264-
} else {
265-
logger.Info("docker container has self terminated")
266-
267-
rc, err := cli.ContainerLogs(bgctx, containerID, dcli.ContainerLogsOptions{Details: true, ShowStdout: true, ShowStderr: true})
268-
if err != nil {
269-
logger.Error("docker container logs error", "error", err)
270-
} else {
271-
defer rc.Close()
272-
var buf bytes.Buffer
273-
stdcopy.StdCopy(&buf, &buf, rc)
274-
logger.Error("container self terminated", "log", buf.String())
275-
}
276-
}
277-
close(done)
265+
// Error is a channel in the new API
266+
err := <-result.Error
267+
done <- waitResult{err: err}
278268
}()
279269

280270
select {
@@ -299,8 +289,23 @@ func dockerEnvironment(ctx context.Context, logger *slog.Logger, dp *pipepb.Dock
299289
if err != nil {
300290
logger.Error("docker container kill error", "error", err)
301291
}
302-
case <-done:
292+
case result := <-done:
303293
// Container stopped on its own
294+
if result.err != nil {
295+
logger.Error("docker container wait error", "error", result.err)
296+
// Fetch and log container output on error
297+
rc, err := cli.ContainerLogs(bgctx, containerID, dcli.ContainerLogsOptions{Details: true, ShowStdout: true, ShowStderr: true})
298+
if err != nil {
299+
logger.Error("failed to fetch container logs after wait error", "error", err)
300+
} else {
301+
defer rc.Close()
302+
var buf bytes.Buffer
303+
stdcopy.StdCopy(&buf, &buf, rc)
304+
logger.Error("container logs after wait error", "log", buf.String())
305+
}
306+
} else {
307+
logger.Info("container terminated on its own")
308+
}
304309
}
305310
}()
306311

0 commit comments

Comments
 (0)