Skip to content

Commit 3eacbb2

Browse files
Copilotcschleiden
andcommitted
Fix duplicate workflow instance and execution ID fields in logs
Co-authored-by: cschleiden <[email protected]>
1 parent 7c2865e commit 3eacbb2

File tree

3 files changed

+73
-6
lines changed

3 files changed

+73
-6
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package workflowstate
2+
3+
import (
4+
"bytes"
5+
"log/slog"
6+
"strings"
7+
"testing"
8+
9+
"github.com/benbjohnson/clock"
10+
"github.com/cschleiden/go-workflows/core"
11+
"github.com/cschleiden/go-workflows/internal/log"
12+
"github.com/stretchr/testify/assert"
13+
"go.opentelemetry.io/otel/trace/noop"
14+
)
15+
16+
func TestLogFieldDuplication(t *testing.T) {
17+
// Create a buffer to capture log output
18+
var buf bytes.Buffer
19+
20+
// Create JSON logger to easily see field duplication
21+
logger := slog.New(slog.NewJSONHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug}))
22+
23+
// Create an instance
24+
instance := &core.WorkflowInstance{
25+
InstanceID: "test-instance-123",
26+
ExecutionID: "test-execution-456",
27+
}
28+
29+
// This mimics what happens in executor.go NewExecutor
30+
logger = logger.With(
31+
slog.String(log.InstanceIDKey, instance.InstanceID),
32+
slog.String(log.ExecutionIDKey, instance.ExecutionID),
33+
)
34+
35+
tracer := noop.NewTracerProvider().Tracer("test")
36+
clock := clock.New()
37+
state := NewWorkflowState(instance, logger, tracer, clock)
38+
39+
// Log something using the workflow state logger (which currently duplicates fields)
40+
state.Logger().Debug("Test message from workflow state")
41+
42+
// Parse and analyze the log output
43+
logLines := strings.Split(strings.TrimSpace(buf.String()), "\n")
44+
45+
foundDuplicates := false
46+
for _, line := range logLines {
47+
if strings.TrimSpace(line) == "" {
48+
continue
49+
}
50+
51+
t.Logf("Log entry: %s", line)
52+
53+
// Count field occurrences in raw JSON string instead of unmarshaling
54+
// since JSON unmarshaling overwrites duplicate keys
55+
instanceCount := strings.Count(line, `"`+log.InstanceIDKey+`"`)
56+
executionCount := strings.Count(line, `"`+log.ExecutionIDKey+`"`)
57+
58+
t.Logf("Instance ID field count: %d", instanceCount)
59+
t.Logf("Execution ID field count: %d", executionCount)
60+
61+
if instanceCount > 1 || executionCount > 1 {
62+
foundDuplicates = true
63+
t.Logf("❌ DUPLICATE FIELDS DETECTED!")
64+
} else {
65+
t.Logf("✅ No duplicates found")
66+
}
67+
}
68+
69+
// After fix, we should NOT find duplicates
70+
assert.False(t, foundDuplicates, "Should not find duplicate fields after fix")
71+
}

internal/workflowstate/workflowstate.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/cschleiden/go-workflows/backend/payload"
1111
"github.com/cschleiden/go-workflows/core"
1212
"github.com/cschleiden/go-workflows/internal/command"
13-
"github.com/cschleiden/go-workflows/internal/log"
1413
"github.com/cschleiden/go-workflows/internal/sync"
1514
"go.opentelemetry.io/otel/trace"
1615
)
@@ -85,9 +84,7 @@ func NewWorkflowState(instance *core.WorkflowInstance, logger *slog.Logger, trac
8584
clock: clock,
8685
}
8786

88-
state.logger = NewReplayLogger(state, logger.With(
89-
log.InstanceIDKey, instance.InstanceID,
90-
log.ExecutionIDKey, instance.ExecutionID))
87+
state.logger = NewReplayLogger(state, logger)
9188

9289
return state
9390
}

workflow/executor/executor.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ func (e *executor) executeNewEvents(newEvents []*history.Event) ([]*history.Even
326326

327327
func (e *executor) Close() {
328328
if e.workflow != nil {
329-
e.logger.Debug("Stopping workflow executor", log.InstanceIDKey, e.workflowState.Instance().InstanceID)
329+
e.logger.Debug("Stopping workflow executor")
330330

331331
// End workflow if running to prevent leaking goroutines
332332
e.workflow.Close()
@@ -335,7 +335,6 @@ func (e *executor) Close() {
335335

336336
func (e *executor) executeEvent(event *history.Event) error {
337337
fields := []any{
338-
log.InstanceIDKey, e.workflowState.Instance().InstanceID,
339338
log.EventIDKey, event.ID,
340339
log.SeqIDKey, event.SequenceID,
341340
log.EventTypeKey, event.Type,

0 commit comments

Comments
 (0)