logging: stamp trace_id and span_id on every log record #253

Closed
opened 2026-10-27 13:35:34 +00:00 by sam · 0 comments
Owner

No log record carries trace context. logging.FeedHandler.Handle (internal/logging/feed.go) receives ctx and only forwards it; FlattenAttrs reads record attrs only, and the inner JSON handler does not read ctx either. So trace ids must be added explicitly.

Design

slog.Handler wrapper in internal/logging; promote go.opentelemetry.io/otel/trace v1.47.0 to a direct require (no new module). Do NOT use contrib/bridges/otelslog: it forwards records to an OTel LoggerProvider/OTLP instead of adding attrs, so the dashboard feed would not see ids.

type traceHandler struct{ inner slog.Handler }
func (h traceHandler) Enabled(ctx context.Context, l slog.Level) bool { return h.inner.Enabled(ctx, l) }
func (h traceHandler) Handle(ctx context.Context, r slog.Record) error {
    if sc := trace.SpanContextFromContext(ctx); sc.IsValid() {
        r.AddAttrs(slog.String("trace_id", sc.TraceID().String()), slog.String("span_id", sc.SpanID().String()))
    }
    return h.inner.Handle(ctx, r)
}
func (h traceHandler) WithAttrs(a []slog.Attr) slog.Handler { return traceHandler{h.inner.WithAttrs(a)} }
func (h traceHandler) WithGroup(n string) slog.Handler      { return traceHandler{h.inner.WithGroup(n)} }

Wire it at every handler construction: servicekit.Logging, servicekit.Diagnostics (archie-ui, archie-messaging), archie-agent own handler (cmd/archie-agent), agentexec SystemLogHandler (whose Handle currently drops ctx). FeedHandler and the JSON handler never read ctx, so the wrapper must sit outermost. LogTable already renders arbitrary fields.

Traps: AddAttrs lands inside an active WithGroup, so ids nest under that group; keep groups off loggers whose output the dashboard reads. Without an installed SDK root spans have an invalid SpanContext, so ids only appear once the provider from fzb3x.3 exists; the wrapper is harmless before that.

Acceptance criteria

A log call made with a context holding a valid span shows trace_id and span_id in the file log and in the dashboard log viewer, for all six binaries including container logs relayed over NATS.

Close reason

logging.WithTrace wraps every handler construction; end-to-end check moved to fzb3x.3, which provides the spans


Imported from beads archie-core-fzb3x.1 · GitHub #1855 · type feature · priority P2 · created 2026-10-25 · closed 2026-10-25

No log record carries trace context. logging.FeedHandler.Handle (internal/logging/feed.go) receives ctx and only forwards it; FlattenAttrs reads record attrs only, and the inner JSON handler does not read ctx either. So trace ids must be added explicitly. ### Design slog.Handler wrapper in internal/logging; promote go.opentelemetry.io/otel/trace v1.47.0 to a direct require (no new module). Do NOT use contrib/bridges/otelslog: it forwards records to an OTel LoggerProvider/OTLP instead of adding attrs, so the dashboard feed would not see ids. type traceHandler struct{ inner slog.Handler } func (h traceHandler) Enabled(ctx context.Context, l slog.Level) bool { return h.inner.Enabled(ctx, l) } func (h traceHandler) Handle(ctx context.Context, r slog.Record) error { if sc := trace.SpanContextFromContext(ctx); sc.IsValid() { r.AddAttrs(slog.String("trace_id", sc.TraceID().String()), slog.String("span_id", sc.SpanID().String())) } return h.inner.Handle(ctx, r) } func (h traceHandler) WithAttrs(a []slog.Attr) slog.Handler { return traceHandler{h.inner.WithAttrs(a)} } func (h traceHandler) WithGroup(n string) slog.Handler { return traceHandler{h.inner.WithGroup(n)} } Wire it at every handler construction: servicekit.Logging, servicekit.Diagnostics (archie-ui, archie-messaging), archie-agent own handler (cmd/archie-agent), agentexec SystemLogHandler (whose Handle currently drops ctx). FeedHandler and the JSON handler never read ctx, so the wrapper must sit outermost. LogTable already renders arbitrary fields. Traps: AddAttrs lands inside an active WithGroup, so ids nest under that group; keep groups off loggers whose output the dashboard reads. Without an installed SDK root spans have an invalid SpanContext, so ids only appear once the provider from fzb3x.3 exists; the wrapper is harmless before that. ### Acceptance criteria A log call made with a context holding a valid span shows trace_id and span_id in the file log and in the dashboard log viewer, for all six binaries including container logs relayed over NATS. ### Close reason logging.WithTrace wraps every handler construction; end-to-end check moved to fzb3x.3, which provides the spans --- _Imported from beads `archie-core-fzb3x.1` · GitHub #1855 · type feature · priority P2 · created 2026-10-25 · closed 2026-10-25_
sam added the mediumfeature labels 2026-10-27 13:35:34 +00:00
sam self-assigned this 2026-10-27 13:35:34 +00:00
sam closed this issue 2026-10-27 13:35:34 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: sam/archie#253