From 638c4d1f5a2a0b78b90cd44c3f549188ee437c85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Brauer?= Date: Thu, 19 May 2022 15:04:40 +0000 Subject: [PATCH] feat: add pre/post stage identifier fields to json log output Co-authored-by: Markus Wolf --- pkg/runner/job_executor.go | 15 ++++++++------- pkg/runner/logger.go | 3 ++- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/pkg/runner/job_executor.go b/pkg/runner/job_executor.go index 2d9f22e9845..44dc0e85961 100644 --- a/pkg/runner/job_executor.go +++ b/pkg/runner/job_executor.go @@ -56,10 +56,10 @@ func newJobExecutor(info jobInfo, sf stepFactory, rc *RunContext) common.Executo return common.NewErrorExecutor(err) } - preSteps = append(preSteps, useStepLogger(rc, stepModel, step.pre())) + preSteps = append(preSteps, useStepLogger(rc, stepModel, stepStagePre, step.pre())) stepExec := step.main() - steps = append(steps, useStepLogger(rc, stepModel, func(ctx context.Context) error { + steps = append(steps, useStepLogger(rc, stepModel, stepStageMain, func(ctx context.Context) error { logger := common.Logger(ctx) err := stepExec(ctx) if err != nil { @@ -72,11 +72,12 @@ func newJobExecutor(info jobInfo, sf stepFactory, rc *RunContext) common.Executo return nil })) - // run the post exector in reverse order + postExec := useStepLogger(rc, stepModel, stepStagePost, step.post()) if postExecutor != nil { - postExecutor = useStepLogger(rc, stepModel, step.post()).Finally(postExecutor) + // run the post exector in reverse order + postExecutor = postExec.Finally(postExecutor) } else { - postExecutor = useStepLogger(rc, stepModel, step.post()) + postExecutor = postExec } } @@ -108,9 +109,9 @@ func newJobExecutor(info jobInfo, sf stepFactory, rc *RunContext) common.Executo Finally(info.closeContainer()) } -func useStepLogger(rc *RunContext, stepModel *model.Step, executor common.Executor) common.Executor { +func useStepLogger(rc *RunContext, stepModel *model.Step, stage stepStage, executor common.Executor) common.Executor { return func(ctx context.Context) error { - ctx = withStepLogger(ctx, stepModel.ID, stepModel.String()) + ctx = withStepLogger(ctx, stepModel.ID, stepModel.String(), stage.String()) rawLogger := common.Logger(ctx).WithField("raw_output", true) logWriter := common.NewLineWriter(rc.commandHandler(ctx), func(s string) bool { diff --git a/pkg/runner/logger.go b/pkg/runner/logger.go index 8297e5f4182..77c34ea9e7f 100644 --- a/pkg/runner/logger.go +++ b/pkg/runner/logger.go @@ -96,10 +96,11 @@ func WithCompositeLogger(ctx context.Context, masks *[]string) context.Context { return common.WithLogger(ctx, common.Logger(ctx).WithFields(logrus.Fields{}).WithContext(ctx)) } -func withStepLogger(ctx context.Context, stepID string, stepName string) context.Context { +func withStepLogger(ctx context.Context, stepID string, stepName string, stageName string) context.Context { rtn := common.Logger(ctx).WithFields(logrus.Fields{ "step": stepName, "stepID": stepID, + "stage": stageName, }) return common.WithLogger(ctx, rtn) }