Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: log more information about Chromium's run #46

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 35 additions & 14 deletions crocochrome.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,23 +312,37 @@ func (s *Supervisor) launch(ctx context.Context, sessionID string) error {
}()

err = cmd.Run()

attrs := make([]slog.Attr, 0, 9)

if err != nil {
attrs = append(attrs, slog.Attr{Key: "err", Value: slog.AnyValue(err)})
}

if cmd.ProcessState != nil {
attrs = append(attrs,
slog.Attr{Key: "pid", Value: slog.IntValue(cmd.ProcessState.Pid())},
slog.Attr{Key: "exitCode", Value: slog.IntValue(cmd.ProcessState.ExitCode())},
slog.Attr{Key: "processState", Value: slog.StringValue(cmd.ProcessState.String())},
slog.Attr{Key: "systemTime", Value: slog.DurationValue(cmd.ProcessState.SystemTime())},
slog.Attr{Key: "userTime", Value: slog.DurationValue(cmd.ProcessState.UserTime())},
slog.Attr{Key: "sysUsage", Value: slog.AnyValue(cmd.ProcessState.SysUsage())},
)
}

if err != nil && !errors.Is(ctx.Err(), context.Canceled) {
s.metrics.ChromiumExecutions.With(map[string]string{
metrics.ExecutionState: metrics.ExecutionStateFailed,
}).Inc()

logger.Error("running chromium", "err", err)
logger.Error("chromium output", "stdout", stdout.String())
logger.Error("chromium output", "stderr", stderr.String())

if ps := cmd.ProcessState; ps != nil {
logger.Debug(
"chromium process finished",
"state", ps.String(),
"exitCode", ps.ExitCode(),
"rss", ps.SysUsage().(*syscall.Rusage).Maxrss,
)
}
// Append stdout and stderr to the log if there was an error,
// as the log message may be useful for debugging what happened.
attrs = append(attrs,
slog.Attr{Key: "stdout", Value: slog.StringValue(stdout.String())},
slog.Attr{Key: "stderr", Value: slog.StringValue(stderr.String())},
)

logger.LogAttrs(ctx, slog.LevelError, "chromium process finished", attrs...)

return err
}
Expand All @@ -337,8 +351,15 @@ func (s *Supervisor) launch(ctx context.Context, sessionID string) error {
metrics.ExecutionState: metrics.ExecutionStateFinished,
}).Inc()

logger.Debug("chromium output", "stdout", stdout.String())
logger.Debug("chromium output", "stderr", stderr.String())
// Avoid spamming the logs with chromium's output, unless the logging level is debug.
if logger.Enabled(ctx, slog.LevelDebug) {
attrs = append(attrs,
slog.Attr{Key: "stdout", Value: slog.StringValue(stdout.String())},
slog.Attr{Key: "stderr", Value: slog.StringValue(stderr.String())},
)
}

logger.LogAttrs(ctx, slog.LevelInfo, "chromium process finished", attrs...)

return nil
}
Expand Down