Files
archie/internal/domain/workflow/trace.go
T
sam 98fb9dfddf
deploy / gate (push) Successful in 16m25s
deploy / agent-image (push) Successful in 22s
deploy / dist-zip (push) Skipped
deploy / archied-image (push) Successful in 7m25s
feat(workflow): task, workflow, step and model attributes on run, step and agent spans (#1803)
2026-11-08 17:00:42 +11:00

93 lines
3.0 KiB
Go

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
}