From 72f833db093696ea514da727b9bb21f96e19da6c Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sun, 25 Oct 2026 22:55:57 +1100 Subject: [PATCH] feat(logging): stamp trace and span ids on log records --- cmd/archie-agent/main.go | 3 +- go.mod | 2 +- internal/app/agentworker/worker.go | 3 +- internal/app/servicekit/config.go | 2 +- internal/logging/logging.go | 4 +-- internal/logging/trace.go | 45 ++++++++++++++++++++++++++++++ 6 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 internal/logging/trace.go diff --git a/cmd/archie-agent/main.go b/cmd/archie-agent/main.go index 52dbfe344..2e786d5cb 100644 --- a/cmd/archie-agent/main.go +++ b/cmd/archie-agent/main.go @@ -16,6 +16,7 @@ import ( "github.com/samcharles93/archie-core/internal/app/agentworker" "github.com/samcharles93/archie-core/internal/infrastructure/egress" + "github.com/samcharles93/archie-core/internal/logging" ) type workerRunner func(context.Context, agentworker.Settings, *slog.Logger) error @@ -68,7 +69,7 @@ func runCommand(args []string, getenv func(string) string, stderr io.Writer, run return 1 } - log := slog.New(slog.NewJSONHandler(stderr, nil)) + log := slog.New(logging.WithTrace(slog.NewJSONHandler(stderr, nil))) ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) defer stop() diff --git a/go.mod b/go.mod index 807c30165..f26e50114 100644 --- a/go.mod +++ b/go.mod @@ -59,7 +59,6 @@ require ( go.opentelemetry.io/otel v1.47.0 // indirect go.opentelemetry.io/otel/log v1.47.0 // indirect go.opentelemetry.io/otel/metric v1.47.0 // indirect - go.opentelemetry.io/otel/trace v1.47.0 // indirect go.uber.org/multierr v1.11.0 // indirect go.yaml.in/yaml/v3 v3.0.5 // indirect golang.org/x/exp v0.0.0-20261013213113-2ab7f3cbc5fb // indirect @@ -100,6 +99,7 @@ require ( go.lsp.dev/jsonrpc2 v1.0.1 go.lsp.dev/protocol v1.0.1 go.lsp.dev/uri v1.0.1 + go.opentelemetry.io/otel/trace v1.47.0 golang.org/x/crypto v0.58.0 golang.org/x/mod v0.42.0 golang.org/x/net v0.61.0 diff --git a/internal/app/agentworker/worker.go b/internal/app/agentworker/worker.go index 5567b7c99..608c1284f 100644 --- a/internal/app/agentworker/worker.go +++ b/internal/app/agentworker/worker.go @@ -22,6 +22,7 @@ import ( "github.com/samcharles93/archie-core/internal/infrastructure/readiness" "github.com/samcharles93/archie-core/internal/infrastructure/workflowsteps" "github.com/samcharles93/archie-core/internal/installtype" + "github.com/samcharles93/archie-core/internal/logging" "github.com/samcharles93/archie-core/internal/storage" "github.com/samcharles93/archie-core/internal/taskrun" "github.com/samcharles93/archie-core/internal/tools" @@ -158,7 +159,7 @@ func run(ctx context.Context, settings Settings, log *slog.Logger, dependencies return &StartupError{Operation: operation, Err: err} } defer transport.Close() - log = slog.New(agentexec.NewSystemLogHandler(log.Handler(), transport.LogPublisher(), taskID)) + log = slog.New(logging.WithTrace(agentexec.NewSystemLogHandler(log.Handler(), transport.LogPublisher(), taskID))) log.Info("system log publisher attached", "task", taskID) subscription, err := transport.SubscribeTasks(ctx, taskID, func(ctx context.Context, request taskrun.Request) (*taskrun.Response, error) { diff --git a/internal/app/servicekit/config.go b/internal/app/servicekit/config.go index f60faf9fd..1ea09433d 100644 --- a/internal/app/servicekit/config.go +++ b/internal/app/servicekit/config.go @@ -62,5 +62,5 @@ func Logging(cfg config.Config, component string) Logs { // Diagnostics captures a service's stderr log for its recent-log contract. func Diagnostics(log *slog.Logger, service string) (*slog.Logger, *logging.Feed) { feed := logging.NewFeed(1000) - return slog.New(logging.NewFeedHandler(log.Handler(), feed)).With("service", service), feed + return slog.New(logging.WithTrace(logging.NewFeedHandler(log.Handler(), feed))).With("service", service), feed } diff --git a/internal/logging/logging.go b/internal/logging/logging.go index 14d37ffe9..fceea159c 100644 --- a/internal/logging/logging.go +++ b/internal/logging/logging.go @@ -62,9 +62,9 @@ func New(opts Options) (*slog.Logger, io.Closer, error) { func withFeed(handler slog.Handler, feed *Feed) slog.Handler { if feed == nil { - return handler + return WithTrace(handler) } - return NewFeedHandler(handler, feed) + return WithTrace(NewFeedHandler(handler, feed)) } func parseLevel(s string) slog.Level { diff --git a/internal/logging/trace.go b/internal/logging/trace.go new file mode 100644 index 000000000..5ab42f770 --- /dev/null +++ b/internal/logging/trace.go @@ -0,0 +1,45 @@ +package logging + +import ( + "context" + "log/slog" + + "go.opentelemetry.io/otel/trace" +) + +// stampedKey marks a context whose record already carries the ids, so a +// traceHandler nested under another one does not stamp them twice. +type stampedKey struct{} + +// traceHandler stamps the trace and span ids of ctx's span on each record. +// It must wrap every other handler: none of them reads ctx. +type traceHandler struct{ inner slog.Handler } + +// WithTrace wraps handler so records logged under a valid span carry +// trace_id and span_id. +func WithTrace(handler slog.Handler) slog.Handler { + return traceHandler{inner: handler} +} + +func (h traceHandler) Enabled(ctx context.Context, level slog.Level) bool { + return h.inner.Enabled(ctx, level) +} + +func (h traceHandler) Handle(ctx context.Context, record slog.Record) error { + if sc := trace.SpanContextFromContext(ctx); sc.IsValid() && ctx.Value(stampedKey{}) == nil { + record.AddAttrs( + slog.String("trace_id", sc.TraceID().String()), + slog.String("span_id", sc.SpanID().String()), + ) + ctx = context.WithValue(ctx, stampedKey{}, true) + } + return h.inner.Handle(ctx, record) +} + +func (h traceHandler) WithAttrs(attrs []slog.Attr) slog.Handler { + return traceHandler{inner: h.inner.WithAttrs(attrs)} +} + +func (h traceHandler) WithGroup(name string) slog.Handler { + return traceHandler{inner: h.inner.WithGroup(name)} +}