Part of #453 (task-observability epic #450). archie-agent logged to os.Stderr only, and its container is created with AutoRemove: true (internal/container/pool.go), so all of it disappeared the moment a task's container exited -- there was no way to see why, say, a gate command failed inside one. Adds agentexec.SystemLogHandler: a slog.Handler that tees to whatever it wraps (kept for an operator attached to a live container) and additionally publishes each record, JSON-encoded as a logging.Entry, to SubjectForSystem(taskID) -- a subject that already existed, documented for exactly this ("log dumps, health... observability"), with zero publishers anywhere in the codebase. cmd/archie-agent/main.go wires it in only when this container was spawned for one specific task (bootTaskID / .git/task.json present); shared queue-group mode serves many unrelated tasks per process, so there's no single task subject to ship to and keeps the stderr-only logger. Building this surfaced a real pre-existing bug: FeedHandler's attr flattening applied the group chain active at Handle-time to *all* accumulated attrs, including ones added via WithAttrs before a later WithGroup call -- nesting them under a group they were never actually inside, contrary to slog's own documented Handler contract. It went unnoticed because nothing in the codebase called WithGroup until this change needed to. Extracting the shared flattening logic into exported FlattenAttrs/PrefixAttrs (so SystemLogHandler doesn't duplicate it) is what surfaced it; fixed in the same change since I was the one making it public API. Docs on the wire format's own trap for the daemon-side consumer (#454, not yet written): the published bytes are a marshaled logging.Entry, meant for json.Unmarshal, not logging.decode() -- decode() parses raw slog JSON *log lines* off disk, a different and incompatible shape. See the doc comment on SystemLogHandler.
84 lines
2.7 KiB
Go
84 lines
2.7 KiB
Go
package logging
|
|
|
|
import (
|
|
"log/slog"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// TestFlattenAttrs pins the shape FeedHandler and any other slog.Handler
|
|
// producing Entry.Fields must agree on: handler-bound attrs, group prefixes,
|
|
// and the record's own attrs all merge into one flat map, dotted by group.
|
|
func TestFlattenAttrs(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
base []slog.Attr
|
|
groups []string
|
|
record func() slog.Record
|
|
want map[string]any
|
|
}{
|
|
{
|
|
name: "record attrs only",
|
|
record: func() slog.Record { return newTestRecord("component", "gate") },
|
|
want: map[string]any{"component": "gate"},
|
|
},
|
|
{
|
|
name: "base attrs merge with record attrs",
|
|
base: []slog.Attr{slog.String("task", "42")},
|
|
record: func() slog.Record { return newTestRecord("component", "gate") },
|
|
want: map[string]any{"task": "42", "component": "gate"},
|
|
},
|
|
{
|
|
name: "groups prefix every key",
|
|
groups: []string{"agent"},
|
|
record: func() slog.Record { return newTestRecord("component", "gate") },
|
|
want: map[string]any{"agent.component": "gate"},
|
|
},
|
|
{
|
|
name: "no attrs at all yields nil, not an empty map",
|
|
record: func() slog.Record { return newTestRecord() },
|
|
want: nil,
|
|
},
|
|
{
|
|
// base is what WithAttrs accumulates; by the time it reaches
|
|
// FlattenAttrs it must already carry whatever group prefix was
|
|
// active when it was added (see prefixAttrs) -- groups here is
|
|
// the chain active *now*, for the record's own attrs, and must
|
|
// not be re-applied to base. Callers that instead pass raw,
|
|
// un-prefixed attrs as base alongside a non-empty groups chain
|
|
// are the historical bug this case pins: it would wrongly nest
|
|
// "task" under "agent" too.
|
|
name: "groups only apply to the record's own attrs, not to base",
|
|
base: []slog.Attr{slog.String("task", "42")}, // pre-grouped by the caller; here, not grouped at all
|
|
groups: []string{"agent"},
|
|
record: func() slog.Record { return newTestRecord("component", "gate") },
|
|
want: map[string]any{"task": "42", "agent.component": "gate"},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := FlattenAttrs(tt.record(), tt.base, tt.groups)
|
|
if len(got) != len(tt.want) {
|
|
t.Fatalf("FlattenAttrs() = %#v, want %#v", got, tt.want)
|
|
}
|
|
for k, v := range tt.want {
|
|
if got[k] != v {
|
|
t.Errorf("FlattenAttrs()[%q] = %v, want %v", k, got[k], v)
|
|
}
|
|
}
|
|
if tt.want == nil && got != nil {
|
|
t.Errorf("FlattenAttrs() = %#v, want nil", got)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func newTestRecord(kvs ...string) slog.Record {
|
|
r := slog.NewRecord(time.Now(), slog.LevelInfo, "msg", 0)
|
|
for i := 0; i+1 < len(kvs); i += 2 {
|
|
r.AddAttrs(slog.String(kvs[i], kvs[i+1]))
|
|
}
|
|
return r
|
|
}
|