From 98fb9dfddf64b293dea67717c1035109d1ea563e Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sun, 8 Nov 2026 17:00:41 +1100 Subject: [PATCH] feat(workflow): task, workflow, step and model attributes on run, step and agent spans (#1803) --- internal/domain/workflow/trace.go | 92 ++++++++++++++++++++++++++++ internal/domain/workflow/workflow.go | 12 +++- 2 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 internal/domain/workflow/trace.go diff --git a/internal/domain/workflow/trace.go b/internal/domain/workflow/trace.go new file mode 100644 index 000000000..e15d4a386 --- /dev/null +++ b/internal/domain/workflow/trace.go @@ -0,0 +1,92 @@ +package workflow + +import ( + "context" + "errors" + "strconv" + + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/codes" + "go.opentelemetry.io/otel/trace" + + "github.com/samcharles93/archie-core/internal/domain/agentrun" +) + +// Attribute names are stable: a span-metrics generator groups by them, so keep +// their values bounded (ids and names, never free text). +const ( + attrTask = attribute.Key("archie.task.id") + attrWorkflow = attribute.Key("archie.workflow") + attrStep = attribute.Key("archie.step") + attrAttempt = attribute.Key("archie.attempt") + attrOutcome = attribute.Key("archie.outcome") +) + +func tracer() trace.Tracer { + return otel.Tracer("github.com/samcharles93/archie-core/internal/domain/workflow") +} + +func taskAttrs(tc *TaskContext, workflow string) []attribute.KeyValue { + return []attribute.KeyValue{ + attrTask.String(strconv.FormatInt(tc.Task.ID, 10)), + attrWorkflow.String(workflow), + attrAttempt.Int(tc.Task.Attempt), + } +} + +// startRunSpan opens the span for one workflow run attempt. +func startRunSpan(ctx context.Context, tc *TaskContext, workflow string) (context.Context, trace.Span) { + return tracer().Start(ctx, "workflow.run", trace.WithAttributes(taskAttrs(tc, workflow)...)) +} + +// startStepSpan opens the span for one top-level step. +func startStepSpan(ctx context.Context, tc *TaskContext, step string) (context.Context, trace.Span) { + return tracer().Start(ctx, "workflow.step", + trace.WithAttributes(append(taskAttrs(tc, tc.Task.Workflow), attrStep.String(step))...)) +} + +// startAgentSpan opens the span for one model-backed agent call. +func startAgentSpan(ctx context.Context, tc *TaskContext, step, modelRef string) (context.Context, trace.Span) { + return tracer().Start(ctx, "agent.run", trace.WithAttributes( + append(taskAttrs(tc, tc.Task.Workflow), attrStep.String(step), attribute.String("gen_ai.request.model", modelRef))...)) +} + +// endSpan records the outcome, marks a failure as an error, and ends span. +func endSpan(span trace.Span, outcome string, err error) { + span.SetAttributes(attrOutcome.String(outcome)) + if err != nil { + span.RecordError(err) + span.SetStatus(codes.Error, err.Error()) + } + span.End() +} + +// setUsage records the call's token counts under the gen_ai conventions. +func setUsage(span trace.Span, u agentrun.Usage) { + span.SetAttributes( + attribute.Int("gen_ai.usage.input_tokens", u.PromptTokens), + attribute.Int("gen_ai.usage.output_tokens", u.CompletionTokens)) +} + +// endRunSpan ends the run's span with the run's outcome; a park is an error. +func endRunSpan(span trace.Span, o Outcome) { + status := o.Status + if status == "" { + status = "interrupted" + } + var err error + if o.Status == StatusParked { + err = errors.New(o.Detail) + } + endSpan(span, status, err) +} + +// runStageSpan runs stage inside its own span. +func runStageSpan(ctx context.Context, tc *TaskContext, stage Stage) error { + ctx, span := startStepSpan(ctx, tc, stage.Name) + err := stage.Run(ctx, tc) + outcome, _ := stageOutcome(tc.skippedWhen, err) + endSpan(span, string(outcome), err) + return err +} diff --git a/internal/domain/workflow/workflow.go b/internal/domain/workflow/workflow.go index d51ccb529..04e4454bc 100644 --- a/internal/domain/workflow/workflow.go +++ b/internal/domain/workflow/workflow.go @@ -356,6 +356,9 @@ func Run(ctx context.Context, wf Workflow, tc *TaskContext) { tc.runInterface, tc.runInterfaceSet = wf.Interface, true log := tc.Log.With("workflow", wf.Name, "repo", tc.Repo.FullName(), "issue", t.IssueNumber) + ctx, runSpan := startRunSpan(ctx, tc, wf.Name) + defer func() { endRunSpan(runSpan, tc.Outcome) }() + stages, err := tc.resumeStages(wf.Stages) if err != nil { park(ctx, tc, err.Error()) @@ -387,7 +390,7 @@ func Run(ctx context.Context, wf Workflow, tc *TaskContext) { tc.Log = stageLog stageLog.InfoContext(ctx, "stage starting") - err = stage.Run(ctx, tc) + err = runStageSpan(ctx, tc, stage) tc.Log = previousLog // Shutdown is not a failure: record the step as interrupted and return @@ -669,7 +672,14 @@ func (tc *TaskContext) RunAgentChild(ctx context.Context, name, alias, modelRef if err != nil { return agentrun.Result{}, err } + _, span := startAgentSpan(ctx, tc, name, modelRef) res, runErr := run() + setUsage(span, res.Usage) + agentOutcome := taskstate.StepSucceeded + if runErr != nil { + agentOutcome = taskstate.StepFailed + } + endSpan(span, string(agentOutcome), runErr) tc.recordUsage(ctx, name, alias, modelRef, res.Usage) to, detail := taskstate.StepSucceeded, res.Summary if runErr != nil {