Skip to content

Commit 242e37e

Browse files
authored
impl: structured logging in controller and substrate harness (#70)
1 parent 6ce9531 commit 242e37e

2 files changed

Lines changed: 23 additions & 7 deletions

File tree

internal/controller2/controller.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package controller2
1919
import (
2020
"context"
2121
"fmt"
22-
"log"
22+
"log/slog"
2323

2424
"github.com/google/ax/internal/controller/executor"
2525
"github.com/google/ax/internal/harness/harnesstest"
@@ -76,7 +76,11 @@ func (d *Controller) Exec(ctx context.Context, req *proto.ExecRequest, handler E
7676
h, err := d.registry.Harness(req.AgentId)
7777
if err != nil {
7878
// Fallback to test harness
79-
log.Printf("WARNING: harness %s not found in registry, falling back to test harness: %v", req.AgentId, err)
79+
slog.WarnContext(ctx, "Harness not found in registry, falling back to test harness",
80+
slog.String("agent_id", req.AgentId),
81+
slog.String("conversation_id", req.ConversationId),
82+
slog.Any("error", err),
83+
)
8084
h = harnesstest.New()
8185
}
8286
exec, err := h.Start(ctx, req.ConversationId)
@@ -128,7 +132,10 @@ func (a *harnessHandler) OnMessage(ctx context.Context, execID string, msg *prot
128132
}
129133
// TODO(anj): The harness should send the full input sent to get this particular response.
130134
if _, err := a.eventLog.Append(ctx, event); err != nil {
131-
log.Printf("WARNING: failed to log streamed message: %v", err)
135+
slog.WarnContext(ctx, "Failed to log streamed message to event log",
136+
slog.String("conversation_id", a.conversationID),
137+
slog.Any("error", err),
138+
)
132139
}
133140

134141
if a.execHandler == nil {
@@ -147,7 +154,10 @@ func (a *harnessHandler) OnComplete(ctx context.Context, execID string) error {
147154
State: proto.State_STATE_COMPLETED,
148155
}
149156
if _, err := a.eventLog.Append(ctx, event); err != nil {
150-
log.Printf("WARNING: failed to log completion event: %v", err)
157+
slog.WarnContext(ctx, "Failed to log completion event to event log",
158+
slog.String("conversation_id", a.conversationID),
159+
slog.Any("error", err),
160+
)
151161
}
152162
return nil
153163
}

internal/harness/substrate.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"errors"
2121
"fmt"
2222
"io"
23-
"log"
23+
"log/slog"
2424
"sync"
2525
"time"
2626

@@ -245,11 +245,17 @@ func (e *substrateExecution) Close(ctx context.Context) error {
245245
}
246246

247247
// Suspend actor to return resource to standard standby pool
248-
log.Printf("Suspending SubstrATE actor for conversation %s (execution %s)", e.conversationID, e.execID)
248+
slog.InfoContext(ctx, "Suspending SubstrATE actor",
249+
slog.String("conversation_id", e.conversationID),
250+
slog.String("exec_id", e.execID),
251+
)
249252
suspendCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
250253
defer cancel()
251254
if _, err := e.harness.ateClient.SuspendActor(suspendCtx, e.conversationID); err != nil {
252-
log.Printf("Failed to suspend actor %s: %v", e.conversationID, err)
255+
slog.ErrorContext(ctx, "Failed to suspend SubstrATE actor",
256+
slog.String("conversation_id", e.conversationID),
257+
slog.Any("error", err),
258+
)
253259
}
254260

255261
return nil

0 commit comments

Comments
 (0)