Add context to log statements.

This commit is contained in:
Maximilian Paß
2023-04-11 19:24:06 +01:00
parent 43221c717e
commit 0c8fa9ccfa
16 changed files with 97 additions and 88 deletions

View File

@ -94,7 +94,7 @@ func (nc *nomadAPIClient) Execute(runnerID string,
ctx context.Context, cmd string, tty bool,
stdin io.Reader, stdout, stderr io.Writer,
) (int, error) {
log.WithField("command", strings.ReplaceAll(cmd, "\n", "")).Trace("Requesting Nomad Exec")
log.WithContext(ctx).WithField("command", strings.ReplaceAll(cmd, "\n", "")).Trace("Requesting Nomad Exec")
var allocations []*nomadApi.AllocationListStub
var err error
logging.StartSpan("nomad.execute.list", "List Allocations for id", ctx, func(_ context.Context) {
@ -132,10 +132,10 @@ func (nc *nomadAPIClient) Execute(runnerID string,
case err == nil:
return exitCode, nil
case websocket.IsCloseError(errors.Unwrap(err), websocket.CloseNormalClosure):
log.WithField("runnerID", runnerID).WithError(err).Info("The exit code could not be received.")
log.WithContext(ctx).WithField("runnerID", runnerID).WithError(err).Info("The exit code could not be received.")
return 0, nil
case errors.Is(err, context.Canceled):
log.WithField("runnerID", runnerID).Debug("Execution canceled by context")
log.WithContext(ctx).WithField("runnerID", runnerID).Debug("Execution canceled by context")
return 0, nil
default:
return 1, fmt.Errorf("error executing command in allocation: %w", err)

View File

@ -482,7 +482,7 @@ func (a *APIClient) executeCommandInteractivelyWithStderr(allocationID string, c
exit, err := a.Execute(allocationID, ctx, prepareCommandTTYStdErr(currentNanoTime, privilegedExecution), true,
nullio.Reader{Ctx: readingContext}, stderr, io.Discard)
if err != nil {
log.WithError(err).WithField("runner", allocationID).Warn("Stderr task finished with error")
log.WithContext(ctx).WithError(err).WithField("runner", allocationID).Warn("Stderr task finished with error")
}
stderrExitChan <- exit
})

View File

@ -56,7 +56,7 @@ func (s *SentryDebugWriter) Write(p []byte) (n int, err error) {
match := matchAndMapTimeDebugMessage(p)
if match == nil {
log.WithField("data", p).Warn("Exec debug message could not be read completely")
log.WithContext(s.Ctx).WithField("data", p).Warn("Exec debug message could not be read completely")
return 0, nil
}
@ -93,7 +93,7 @@ func (s *SentryDebugWriter) Close(exitCode int) {
func (s *SentryDebugWriter) handleTimeDebugMessage(match map[string][]byte) {
timestamp, err := strconv.ParseInt(string(match["time"]), 10, 64)
if err != nil {
log.WithField("match", match).Warn("Could not parse Unix timestamp")
log.WithContext(s.Ctx).WithField("match", match).Warn("Could not parse Unix timestamp")
return
}