feat(natsrpc): propagate trace context across NATS and into task runs

This commit is contained in:
sam committed 2026-10-25 23:03:55 +11:00
1 parent 4a889991bd
commit 0880e6a01c
37 files changed
+245 -117

No files matched your search

+19 -11
View File
@@ -17,6 +17,9 @@ import (
"sync"
"time"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
"github.com/samcharles93/archie-core/internal/agentexec"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/container"
@@ -36,6 +39,7 @@ import (
"github.com/samcharles93/archie-core/internal/forge"
agentnats "github.com/samcharles93/archie-core/internal/infrastructure/agenttransport/nats"
"github.com/samcharles93/archie-core/internal/logging"
"github.com/samcharles93/archie-core/internal/natsrpc"
"github.com/samcharles93/archie-core/internal/storage"
"github.com/samcharles93/archie-core/internal/taskrun"
"github.com/samcharles93/archie-core/internal/taskstate"
@@ -54,7 +58,7 @@ type TaskBus interface {
// authorizes the task's own State Store calls and the push of its branch.
// It lapses at the task's time limit plus a margin if never revoked.
type RunCredentialIssuer interface {
Issue(task *workflow.Task, limit time.Duration) (token string, revoke func(), err error)
Issue(ctx context.Context, task *workflow.Task, limit time.Duration) (token string, revoke func(), err error)
}
// NATSEndpoint is the broker address and credential archied connected with at
@@ -268,9 +272,9 @@ func (d *Daemon) sourceTag(source IssueSource) string {
}
// emit publishes an observability event; safe on a nil bus.
func (d *Daemon) emit(e events.Event) {
func (d *Daemon) emit(ctx context.Context, e events.Event) {
if d.Bus != nil {
d.Bus.Publish(e)
d.Bus.Publish(ctx, e)
}
}
@@ -1371,7 +1375,7 @@ func (d *Daemon) acknowledge(ctx context.Context, fg forge.Forge, cfg config.Con
d.Log.Warn("ack reaction failed", "issue", is.Number, "err", err)
}
}
d.emit(events.Event{
d.emit(ctx, events.Event{
Kind: events.KindTaskQueued, Repo: repo.FullName(),
Issue: is.Number, Detail: is.Title,
})
@@ -1399,6 +1403,8 @@ func (d *Daemon) acceptTaskPublisher(ctx context.Context, msg eventbus.Message,
// and runs the workflow. The message is ack'd on terminal (park is a valid
// outcome). Nak on transient errors so NATS redelivers.
func (d *Daemon) processNATSTask(ctx context.Context, msg eventbus.Message, hold *taskHold) {
ctx, span := natsrpc.StartConsumer(ctx, msg.Subject(), msg.Header())
defer span.End()
tm, err := workintake.DecodeTask(msg.Data())
if err != nil {
d.Log.Error("nats decode failed", "err", err)
@@ -1504,7 +1510,7 @@ func (d *Daemon) reconcilePRs(ctx context.Context) {
"Closed by #%d, merged by archie.", t.PRNumber,
))
d.Log.Info("PR merged", "repo", t.Owner+"/"+t.Repo, "pr", t.PRNumber)
d.emit(events.Event{
d.emit(ctx, events.Event{
Kind: events.KindPRMerged, TaskID: t.ID, Attempt: attempt,
Repo: t.Owner + "/" + t.Repo, Issue: t.IssueNumber,
Data: map[string]any{"pr": t.PRNumber},
@@ -1513,7 +1519,7 @@ func (d *Daemon) reconcilePRs(ctx context.Context) {
_ = d.Store.Transition(ctx, t.ID, workflow.StatusPROpen, workflow.StatusRejected, "PR closed without merge")
_ = trees.Cleanup(t.Owner, t.Repo, t.IssueNumber)
d.Log.Info("PR rejected", "repo", t.Owner+"/"+t.Repo, "pr", t.PRNumber)
d.emit(events.Event{
d.emit(ctx, events.Event{
Kind: events.KindPRRejected, TaskID: t.ID, Attempt: attempt,
Repo: t.Owner + "/" + t.Repo, Issue: t.IssueNumber,
Data: map[string]any{"pr": t.PRNumber},
@@ -1805,7 +1811,7 @@ func (d *Daemon) acquireTaskContainer(
return nil, "", nil, false
}
credential, revokeCredential, err := d.runCredential(task)
credential, revokeCredential, err := d.runCredential(ctx, task)
if err != nil {
d.teardownStorage(ctx, task, repo, workDir)
park("run credential failed", err)
@@ -1903,7 +1909,7 @@ func (d *Daemon) parkBlockedTask(ctx context.Context, task *workflow.Task, reaso
// parked would be a lie when another actor moved it out of running first
// -- the same race the parked event is withheld for.
d.recordPark(ctx, task.ID, reason)
d.emit(events.Event{
d.emit(ctx, events.Event{
Kind: events.KindParked, TaskID: task.ID, Attempt: task.Attempt,
Repo: strings.Trim(task.Owner+"/"+task.Repo, "/"), Issue: task.IssueNumber, Workflow: task.Workflow,
Detail: reason, Data: map[string]any{"park_class": class},
@@ -1955,6 +1961,8 @@ func (d *Daemon) runViaAgent(ctx context.Context, task *workflow.Task, repo conf
Tools: d.toolAllowlist(ctx, task, profile.Tools),
Harness: harness,
}
req.Trace = map[string]string{}
otel.GetTextMapPropagator().Inject(ctx, propagation.MapCarrier(req.Trace))
data, err := json.Marshal(req)
if err != nil {
d.Log.Error("taskrun encode failed", "task", task.ID, "err", err)
@@ -2375,14 +2383,14 @@ func (d *Daemon) containerEnv(task *workflow.Task, stateStoreToken, stateStoreUR
// runCredential issues the task's run credential, or "" when no State Store
// is configured. It fails when no issuer is wired.
func (d *Daemon) runCredential(task *workflow.Task) (string, func(), error) {
func (d *Daemon) runCredential(ctx context.Context, task *workflow.Task) (string, func(), error) {
if d.ConnectedStateStore.URL == "" {
return "", func() {}, nil
}
if d.RunCredentials == nil {
return "", nil, fmt.Errorf("state store is configured but no run credential issuer is wired")
}
return d.RunCredentials.Issue(task, d.configFor(task).Budgets.TaskWallClock.Std())
return d.RunCredentials.Issue(ctx, task, d.configFor(task).Budgets.TaskWallClock.Std())
}
func (d *Daemon) configFor(task *workflow.Task) config.Config {
@@ -2429,7 +2437,7 @@ func (d *Daemon) captureAttemptConfig(ctx context.Context, task *workflow.Task,
// The assigned ID tells the event sink to broadcast without inserting a
// duplicate row.
event.ID = id
d.emit(event)
d.emit(ctx, event)
}
// identityMayAct reports whether a task's identity may act, and the park
+1 -1
View File
@@ -40,7 +40,7 @@ func (d *Daemon) runKitTask(ctx context.Context, task *workflow.Task, repo confi
park("task.json write failed", err)
return
}
credential, revokeCredential, err := d.runCredential(task)
credential, revokeCredential, err := d.runCredential(ctx, task)
if err != nil {
park("run credential failed", err)
return
+3
View File
@@ -12,6 +12,7 @@ import (
"github.com/samcharles93/archie-core/internal/domain/workintake"
"github.com/samcharles93/archie-core/internal/eventbus"
"github.com/samcharles93/archie-core/internal/events"
"github.com/samcharles93/archie-core/internal/natsrpc"
)
// ReactionSource is the pull surface the reaction consumer needs: fetch one
@@ -85,6 +86,8 @@ func (c *reactionConsumer) drain(ctx context.Context, src ReactionSource, limit
// path (drop, dedup, or work queued), Nak only on a transient store failure
// the next delivery can succeed against.
func (c *reactionConsumer) handle(ctx context.Context, msg eventbus.Message) {
ctx, span := natsrpc.StartConsumer(ctx, msg.Subject(), msg.Header())
defer span.End()
envelope, err := workintake.DecodeReviewComment(msg.Data())
if err != nil {
c.log.Error("review reaction decode failed", "subject", msg.Subject(), "err", err)