Skip to content

Commit 31532ec

Browse files
authored
logging more attributes
1 parent bcd44f2 commit 31532ec

File tree

1 file changed

+48
-6
lines changed

1 file changed

+48
-6
lines changed

internal/workflow/executor.go

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/cschleiden/go-workflows/internal/payload"
1717
"github.com/cschleiden/go-workflows/internal/sync"
1818
"github.com/cschleiden/go-workflows/internal/task"
19+
"github.com/cschleiden/go-workflows/internal/workflowerrors"
1920
"github.com/cschleiden/go-workflows/internal/workflowstate"
2021
"github.com/cschleiden/go-workflows/internal/workflowtracer"
2122
"github.com/cschleiden/go-workflows/log"
@@ -49,6 +50,7 @@ type executor struct {
4950
workflowState *workflowstate.WfState
5051
workflowCtx sync.Context
5152
workflowCtxCancel sync.CancelFunc
53+
cv converter.Converter
5254
clock clock.Clock
5355
logger log.Logger
5456
tracer trace.Tracer
@@ -96,6 +98,7 @@ func NewExecutor(
9698
workflowState: s,
9799
workflowCtx: wfCtx,
98100
workflowCtxCancel: cancel,
101+
cv: cv,
99102
clock: clock,
100103
logger: logger,
101104
tracer: tracer,
@@ -279,14 +282,21 @@ func (e *executor) Close() {
279282
}
280283

281284
func (e *executor) executeEvent(event *history.Event) error {
282-
e.logger.Debug("Executing event",
285+
fields := []any{
283286
log.InstanceIDKey, e.workflowState.Instance().InstanceID,
284287
log.EventIDKey, event.ID,
285288
log.SeqIDKey, event.SequenceID,
286289
log.EventTypeKey, event.Type,
287290
log.ScheduleEventIDKey, event.ScheduleEventID,
288291
log.IsReplayingKey, e.workflowState.Replaying(),
289-
)
292+
}
293+
294+
attributesFields := getAttributesLoggingFields(event)
295+
if attributesFields != nil {
296+
fields = append(fields, attributesFields...)
297+
}
298+
299+
e.logger.Debug("Executing event", fields)
290300

291301
var err error
292302

@@ -423,7 +433,8 @@ func (e *executor) handleActivityFailed(event *history.Event, a *history.Activit
423433
return errors.New("no pending future for activity failed event")
424434
}
425435

426-
if err := f(nil, errors.New(a.Reason)); err != nil {
436+
actErr := workflowerrors.ToError(a.Error)
437+
if err := f(nil, actErr); err != nil {
427438
return fmt.Errorf("setting activity failed result: %w", err)
428439
}
429440

@@ -561,7 +572,9 @@ func (e *executor) handleSubWorkflowFailed(event *history.Event, a *history.SubW
561572
return errors.New("no pending future found for sub workflow failed event")
562573
}
563574

564-
if err := f(nil, errors.New(a.Error)); err != nil {
575+
wfErr := workflowerrors.ToError(a.Error)
576+
577+
if err := f(nil, wfErr); err != nil {
565578
return fmt.Errorf("setting sub workflow failed result: %w", err)
566579
}
567580

@@ -641,11 +654,13 @@ func (e *executor) handleSideEffectResult(event *history.Event, a *history.SideE
641654
return e.workflow.Continue()
642655
}
643656

644-
func (e *executor) workflowCompleted(result payload.Payload, err error) {
657+
func (e *executor) workflowCompleted(result payload.Payload, wfErr error) error {
645658
eventId := e.workflowState.GetNextScheduleEventID()
646659

647-
cmd := command.NewCompleteWorkflowCommand(eventId, e.workflowState.Instance(), result, err)
660+
cmd := command.NewCompleteWorkflowCommand(eventId, e.workflowState.Instance(), result, workflowerrors.FromError(wfErr))
648661
e.workflowState.AddCommand(cmd)
662+
663+
return nil
649664
}
650665

651666
func (e *executor) workflowRestarted(result payload.Payload, continueAsNew *continueasnew.Error) {
@@ -668,3 +683,30 @@ func (e *executor) createNewEvent(eventType history.EventType, attributes interf
668683
opts...,
669684
)
670685
}
686+
687+
func getAttributesLoggingFields(event *history.Event) []any {
688+
switch event.Type {
689+
case history.EventType_WorkflowExecutionStarted:
690+
attributes := event.Attributes.(*history.ExecutionStartedAttributes)
691+
return []any{
692+
log.WorkflowNameKey, attributes.Name,
693+
}
694+
case history.EventType_SubWorkflowScheduled:
695+
attributes := event.Attributes.(*history.SubWorkflowScheduledAttributes)
696+
return []any{
697+
log.WorkflowNameKey, attributes.Name,
698+
}
699+
case history.EventType_SignalReceived:
700+
attributes := event.Attributes.(*history.SignalReceivedAttributes)
701+
return []any{
702+
log.SignalNameKey, attributes.Name,
703+
}
704+
case history.EventType_ActivityScheduled:
705+
attributes := event.Attributes.(*history.ActivityScheduledAttributes)
706+
return []any{
707+
log.ActivityNameKey, attributes.Name,
708+
}
709+
default:
710+
return nil
711+
}
712+
}

0 commit comments

Comments
 (0)