carlos logs names a rootfs instance's process (#366)
The Task 11 run on r10r/box-1 found every rootfs process's output in `carlos logs` as a bare [app] line: web, the release and each worker were indistinguishable. Each capture site already knows the name, so it now rides in Fields["process"]: web on a rootfs row's run-role listener (prepareKeyedAt), the worker's declared name on its listener (prepareWorker), release on releaseAppLog. applog.Sink.AppProcess sets it after ParseAppLine, so the platform's name wins over a "process" key in the app's own JSON. The stream stays app; StreamRelease keeps meaning ship/promote history. `--field process=web` filters on it with no CLI change. Binary instances are unchanged: their nested run role and the flat teeStd path name no process (TestBinaryAppLinesNameNoProcess). Witnesses: the activator test writes at the sockets and writer production hands each unit and reads a real applog.Sink; the root e2e ships a real sink's drain as logshipFlush does and reads it through the console with runLogs, including --field process=<name>.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
8 files changed,
+319
−73
internal/activator/logsink.go+26 −0internal/activator/prepare.go+12 −7internal/activator/rootfs_process_logs_test.go+112 −0internal/activator/rootfsrelease.go+3 −7internal/applog/record.go+5 −0internal/applog/sink.go+15 −0internal/applog/sink_test.go+26 −0logs_e2e_test.go+120 −59
diff --git a/internal/activator/logsink.go b/internal/activator/logsink.go| index 431364dca..75944a932 100644 |
| --- a/internal/activator/logsink.go |
| +++ b/internal/activator/logsink.go |
| @@ -12,6 +12,7 @@ import ( |
| "regexp" |
| "strings" |
| + "github.com/carlosframework/platform/internal/applog" |
| "github.com/carlosframework/platform/internal/cgroup" |
| "github.com/carlosframework/platform/internal/registry" |
| "github.com/carlosframework/platform/internal/release" |
| @@ -29,6 +30,31 @@ type EventSink interface { |
| Event(acct, app, host, level, msg string) |
| } |
| +// processSink is an EventSink that can name the process an app line came |
| +// from (applog.Sink.AppProcess). It is asked for by assertion, not added to |
| +// EventSink, so the package's EventSink fakes stay as they are; the |
| +// assertion below keeps the real sink holding it. |
| +type processSink interface { |
| + AppProcess(acct, app, host, process, line string) |
| +} |
| + |
| +var _ processSink = (*applog.Sink)(nil) |
| + |
| +// appLineEmitter is the emit function for rt's app output, or nil when |
| +// sink is nil. A non-empty process (a rootfs instance's web, release or |
| +// worker name) is passed to a sink that can name it; otherwise the line |
| +// goes through App as before. |
| +func appLineEmitter(sink EventSink, rt registry.Route, process string) func(string) { |
| + if sink == nil { |
| + return nil |
| + } |
| + acct, app, host := release.RouteAccount(rt), rt.App, rt.Host |
| + if ps, ok := sink.(processSink); ok && process != "" { |
| + return func(line string) { ps.AppProcess(acct, app, host, process, line) } |
| + } |
| + return func(line string) { sink.App(acct, app, host, line) } |
| +} |
| + |
| // emitEvent publishes one platform event for rt's app, tolerating a nil |
| // sink. level is an applog.Level* value ("info"/"warn"/"error" — string |
| // here to keep the dependency one-way). |
diff --git a/internal/activator/prepare.go b/internal/activator/prepare.go| index e921303f8..47c54aef9 100644 |
| --- a/internal/activator/prepare.go |
| +++ b/internal/activator/prepare.go |
| @@ -179,7 +179,13 @@ func (b *ExecBackend) prepareKeyedAt(rt registry.Route, role instancectl.Role, s |
| var logs *logListener |
| if role != instancectl.RoleCmd { |
| var err error |
| - logs, err = listenLogs(b.logSocketFor(rt.Host, role, gen), b.logEmitter(rt)) |
| + // A rootfs row's run role is its web process; a binary row's |
| + // lines name no process. |
| + process := "" |
| + if rt.Rootfs() && role == instancectl.RoleRun { |
| + process = release.ProcessWeb |
| + } |
| + logs, err = listenLogs(b.logSocketFor(rt.Host, role, gen), b.logEmitter(rt, process)) |
| if err != nil { |
| if ctl != nil { |
| ctl.Close() |
| @@ -222,7 +228,7 @@ func (b *ExecBackend) prepareWorker(rt registry.Route, name string) (instancePat |
| } |
| } |
| } |
| - logs, err := listenLogs(sock, b.logEmitter(rt)) |
| + logs, err := listenLogs(sock, b.logEmitter(rt, name)) |
| if err != nil { |
| return p, nil, fmt.Errorf("log listener for %s worker %s: %w", rt.Host, name, err) |
| } |
| @@ -314,11 +320,10 @@ func (b *ExecBackend) logSocketNamed(host, name string) string { |
| } |
| // logEmitter is the same sink teeStd feeds, or nil when Logs is unset. |
| -func (b *ExecBackend) logEmitter(rt registry.Route) func(string) { |
| - if b.Logs == nil { |
| - return nil |
| - } |
| - return func(line string) { b.Logs.App(release.RouteAccount(rt), rt.App, rt.Host, line) } |
| +// process names the process the lines come from ("" for none): |
| +// appLineEmitter. |
| +func (b *ExecBackend) logEmitter(rt registry.Route, process string) func(string) { |
| + return appLineEmitter(b.Logs, rt, process) |
| } |
| // writeLaunch is writeLaunchFor for the original templates. |
diff --git a/internal/activator/rootfs_process_logs_test.go b/internal/activator/rootfs_process_logs_test.go| new file mode 100644 |
| index 000000000..46695953a |
| --- /dev/null |
| +++ b/internal/activator/rootfs_process_logs_test.go |
| @@ -0,0 +1,112 @@ |
| +package activator |
| + |
| +import ( |
| + "context" |
| + "net" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/applog" |
| + "github.com/carlosframework/platform/internal/instancectl" |
| + "github.com/carlosframework/platform/internal/release" |
| +) |
| + |
| +// A rootfs instance's output reaches `carlos logs` naming the process that |
| +// wrote it (#366 Task 11 follow-up): web through the generation's log |
| +// listener, a worker through its own, the release through releaseAppLog. |
| +// Each line goes in at the socket or writer production hands the unit, and |
| +// comes out of a real applog.Sink, so the wiring at each capture site is |
| +// what is checked. A process key in the app's own JSON loses to the |
| +// platform's. |
| +func TestRootfsAppLinesNameTheirProcess(t *testing.T) { |
| + t.Parallel() |
| + b, ctl, rt, units := newWorkerBackend(t) |
| + sink := applog.NewSink("box1", time.Now) |
| + b.Logs = sink |
| + web, g := startWorkerWeb(t, b, ctl, rt, units, "") |
| + units.set(instancectl.WorkerUnitName(rt.Host, "jobs"), "active", "inv1", true) |
| + wp, err := b.LaunchRootfsWorker(context.Background(), rt, "jobs", g, web) |
| + if err != nil { |
| + t.Fatalf("LaunchRootfsWorker: %v", err) |
| + } |
| + t.Cleanup(func() { stopWorker(t, wp) }) |
| + |
| + writeUnitLine(t, b.logSocketFor(rt.Host, instancectl.RoleRun, g.N), `{"msg":"web line","process":"spoof"}`) |
| + writeUnitLine(t, b.workerLogSocket(rt.Host, "jobs"), "worker line") |
| + a := &Service{Logs: sink} |
| + if _, err := a.releaseAppLog(rt).Write([]byte("release line")); err != nil { |
| + t.Fatal(err) |
| + } |
| + |
| + got := map[string]string{} |
| + for _, r := range drainAppLines(t, sink, 3) { |
| + got[r.Msg] = r.Fields["process"] |
| + if r.Stream != applog.StreamApp { |
| + t.Errorf("%q is on stream %q, want app", r.Msg, r.Stream) |
| + } |
| + } |
| + want := map[string]string{"web line": release.ProcessWeb, "worker line": "jobs", "release line": release.ProcessRelease} |
| + for msg, p := range want { |
| + if got[msg] != p { |
| + t.Errorf("%q has process %q, want %q (all: %v)", msg, got[msg], p, got) |
| + } |
| + } |
| +} |
| + |
| +// A binary instance's nested run role is unchanged: its lines carry no |
| +// process field. |
| +func TestBinaryAppLinesNameNoProcess(t *testing.T) { |
| + t.Parallel() |
| + b, _ := newRootfsBackend(t) |
| + rt := rootfsRoute(b) |
| + rt.Runtime = "" |
| + sink := applog.NewSink("box1", time.Now) |
| + b.Logs = sink |
| + _, ctl, logs, _, _, err := b.prepareKeyedAt(rt, instancectl.RoleRun, rt.Socket, 0, nil) |
| + if err != nil { |
| + t.Fatalf("prepareKeyedAt: %v", err) |
| + } |
| + if ctl != nil { |
| + t.Cleanup(func() { ctl.Close() }) |
| + } |
| + t.Cleanup(func() { logs.Close() }) |
| + writeUnitLine(t, b.logSocketFor(rt.Host, instancectl.RoleRun, 0), "binary line") |
| + for _, r := range drainAppLines(t, sink, 1) { |
| + if p, ok := r.Fields["process"]; ok { |
| + t.Errorf("%q carries process %q, want none", r.Msg, p) |
| + } |
| + } |
| +} |
| + |
| +// writeUnitLine is a unit's stdout: one connection to its log socket, one |
| +// line, closed. |
| +func writeUnitLine(t *testing.T, sock, line string) { |
| + t.Helper() |
| + c, err := net.Dial("unix", sock) |
| + if err != nil { |
| + t.Fatalf("dial %s: %v", sock, err) |
| + } |
| + defer c.Close() |
| + if _, err := c.Write([]byte(line + "\n")); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |
| + |
| +// drainAppLines drains sink until n app-stream records have arrived. |
| +func drainAppLines(t *testing.T, sink *applog.Sink, n int) []applog.Record { |
| + t.Helper() |
| + var out []applog.Record |
| + for deadline := time.Now().Add(5 * time.Second); len(out) < n && time.Now().Before(deadline); time.Sleep(10 * time.Millisecond) { |
| + for _, recs := range sink.Drain() { |
| + for _, r := range recs { |
| + if r.Stream == applog.StreamApp { |
| + out = append(out, r) |
| + } |
| + } |
| + } |
| + } |
| + if len(out) != n { |
| + t.Fatalf("%d app lines arrived, want %d: %+v", len(out), n, out) |
| + } |
| + return out |
| +} |
diff --git a/internal/activator/rootfsrelease.go b/internal/activator/rootfsrelease.go| index ee6376dbe..6244ca4d5 100644 |
| --- a/internal/activator/rootfsrelease.go |
| +++ b/internal/activator/rootfsrelease.go |
| @@ -348,14 +348,10 @@ func releaseLeaseGone(held leaseWatch) bool { |
| } |
| // releaseAppLog is the writer a release's output goes through to the app |
| -// log, a line a Write (StartRelease's line writer feeds it whole lines); |
| -// nil Logs discards. |
| +// log, a line a Write (StartRelease's line writer feeds it whole lines), |
| +// each naming the release process; nil Logs discards. |
| func (a *Service) releaseAppLog(rt registry.Route) io.Writer { |
| - if a.Logs == nil { |
| - return lineEmitter(nil) |
| - } |
| - sink := a.Logs |
| - return lineEmitter(func(line string) { sink.App(release.RouteAccount(rt), rt.App, rt.Host, line) }) |
| + return lineEmitter(appLineEmitter(a.Logs, rt, release.ProcessRelease)) |
| } |
| // runRelease runs id's release process for rt, if one is due, under held |
diff --git a/internal/applog/record.go b/internal/applog/record.go| index 92ae49d69..ea153fdd6 100644 |
| --- a/internal/applog/record.go |
| +++ b/internal/applog/record.go |
| @@ -23,6 +23,11 @@ const ( |
| StreamRelease = "release" // ship/promote history join (stage 3) |
| ) |
| +// FieldProcess is the field an app line carries naming the process that |
| +// wrote it, when the platform knows (Sink.AppProcess): a rootfs instance's |
| +// web, release or worker. `carlos logs --field process=web` filters on it. |
| +const FieldProcess = "process" |
| + |
| // ValidStream reports whether s names a known stream — the CLI/API's |
| // vocabulary check, kept here so there is exactly one list. |
| func ValidStream(s string) bool { |
diff --git a/internal/applog/sink.go b/internal/applog/sink.go| index 19a3db172..e40b8eed7 100644 |
| --- a/internal/applog/sink.go |
| +++ b/internal/applog/sink.go |
| @@ -129,7 +129,22 @@ func (s *Sink) Known() []AppKey { |
| // JSON lines at the door (ParseAppLine) so the bucket holds fields and |
| // honored levels rather than opaque text. Subject to both budgets. |
| func (s *Sink) App(acct, app, host, line string) { |
| + s.AppProcess(acct, app, host, "", line) |
| +} |
| + |
| +// AppProcess is App for a line the platform knows the process of (a rootfs |
| +// instance's web, release or a named worker): a non-empty process is set |
| +// as Fields["process"] after parsing, over any "process" key in the app's |
| +// own JSON, so the name a member filters on is always the platform's. The |
| +// stream stays StreamApp; StreamRelease is the ship/promote history. |
| +func (s *Sink) AppProcess(acct, app, host, process, line string) { |
| msg, level, fields := ParseAppLine(line) |
| + if process != "" { |
| + if fields == nil { |
| + fields = make(map[string]string, 1) |
| + } |
| + fields[FieldProcess] = process |
| + } |
| s.mu.Lock() |
| defer s.mu.Unlock() |
| b := s.buf(AppKey{acct, app}) |
diff --git a/internal/applog/sink_test.go b/internal/applog/sink_test.go| index fcdaa2ac0..17068a9fd 100644 |
| --- a/internal/applog/sink_test.go |
| +++ b/internal/applog/sink_test.go |
| @@ -162,3 +162,29 @@ func TestIsDropNotice(t *testing.T) { |
| } |
| } |
| } |
| + |
| +// AppProcess is App with the platform naming the process that wrote the |
| +// line (a rootfs instance's web, release or a worker): the name rides in |
| +// Fields["process"], on a plain line and on a structured one alike, and |
| +// the platform's value wins over a "process" key in the app's own JSON. |
| +// The stream stays app: "release" is the ship/promote history's stream. |
| +func TestSinkAppProcessNamesTheProcess(t *testing.T) { |
| + now, _ := testClock(time.Date(2026, 10, 30, 12, 0, 0, 0, time.UTC)) |
| + sk := NewSink("box1", now) |
| + sk.AppProcess("bab", "shop", "shop.bab.oncarlos.com", "web", "listening") |
| + sk.AppProcess("bab", "shop", "shop.bab.oncarlos.com", "jobs", `{"msg":"tick","process":"web","n":3}`) |
| + sk.AppProcess("bab", "shop", "shop.bab.oncarlos.com", "release", "migrated") |
| + sk.App("bab", "shop", "shop.bab.oncarlos.com", `{"msg":"own","process":"mine"}`) |
| + recs := sk.Drain()[AppKey{"bab", "shop"}] |
| + if len(recs) != 4 { |
| + t.Fatalf("records: %d, want 4", len(recs)) |
| + } |
| + for i, want := range []struct{ msg, process, n string }{ |
| + {"listening", "web", ""}, {"tick", "jobs", "3"}, {"migrated", "release", ""}, {"own", "mine", ""}, |
| + } { |
| + r := recs[i] |
| + if r.Stream != StreamApp || r.Msg != want.msg || r.Fields["process"] != want.process || r.Fields["n"] != want.n { |
| + t.Errorf("record %d = %+v, want app stream, msg %q, process %q, n %q", i, r, want.msg, want.process, want.n) |
| + } |
| + } |
| +} |
diff --git a/logs_e2e_test.go b/logs_e2e_test.go| index d0939784f..13d0b8352 100644 |
| --- a/logs_e2e_test.go |
| +++ b/logs_e2e_test.go |
| @@ -20,65 +20,7 @@ import ( |
| ) |
| func TestE2ELogsThroughConsole(t *testing.T) { |
| - useTempCredentials(t) |
| - clearDeploymentEnv(t) |
| - checkNoRegistry := requireNoRegistryTouch(t) |
| - |
| - tmp := t.TempDir() |
| - relStore := release.NewDirStore(filepath.Join(tmp, "store")) |
| - seed, pubB64, err := release.GenerateKey() |
| - if err != nil { |
| - t.Fatal(err) |
| - } |
| - priv, err := release.ParseSigningKey(seed, "e2e") |
| - if err != nil { |
| - t.Fatal(err) |
| - } |
| - pub, err := release.ParsePublicKey(pubB64, "e2e") |
| - if err != nil { |
| - t.Fatal(err) |
| - } |
| - cst, err := console.Open(filepath.Join(tmp, "console.db")) |
| - if err != nil { |
| - t.Fatal(err) |
| - } |
| - t.Cleanup(func() { cst.Close() }) |
| - |
| - var srv *console.Server |
| - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| - srv.ServeHTTP(w, r) |
| - })) |
| - t.Cleanup(ts.Close) |
| - srv, err = console.NewServer(console.Config{ |
| - Origin: ts.URL, |
| - Keymail: "http://keymail.invalid", // never dialled — see e2eDeviceLogin |
| - Registry: filepath.Join(tmp, "registry.db"), |
| - Releases: relStore, |
| - ReleasePub: pub, |
| - Key: priv, |
| - AppScheme: "http", |
| - AppsDomain: "oncarlos.test", |
| - }, cst) |
| - if err != nil { |
| - t.Fatal(err) |
| - } |
| - |
| - const addr = "paul@keymail.dev" |
| - if err := cst.AddMember(console.OpsAccountID, addr, console.RoleOwner, time.Now()); err != nil { |
| - t.Fatal(err) |
| - } |
| - if _, err := cst.CreateAppIn(console.OpsAccountID, "titogo", addr, time.Now()); err != nil { |
| - t.Fatal(err) |
| - } |
| - token, expiresAt := e2eDeviceLogin(t, ts.URL, cst, addr, "e2e@laptop") |
| - credFile, err := loadCredentials() |
| - if err != nil { |
| - t.Fatal(err) |
| - } |
| - credFile.put(credential{Console: ts.URL, Address: addr, Token: token, ExpiresAt: expiresAt}) |
| - if err := credFile.save(); err != nil { |
| - t.Fatal(err) |
| - } |
| + checkNoRegistry, relStore := startLogsE2E(t) |
| // A box shipped two chunks: routine output, then a wake failure — the |
| // #131 case, as the shipping loop would have written it. |
| @@ -160,3 +102,122 @@ func TestE2ELogsThroughConsole(t *testing.T) { |
| // a local registry. |
| checkNoRegistry() |
| } |
| + |
| +// startLogsE2E is a real console.Server over a DirStore, an ops app |
| +// titogo and a device-flow login for its owner. It returns the |
| +// no-registry check and the store boxes ship chunks into. |
| +func startLogsE2E(t *testing.T) (checkNoRegistry func(), relStore *release.DirStore) { |
| + t.Helper() |
| + useTempCredentials(t) |
| + clearDeploymentEnv(t) |
| + checkNoRegistry = requireNoRegistryTouch(t) |
| + |
| + tmp := t.TempDir() |
| + relStore = release.NewDirStore(filepath.Join(tmp, "store")) |
| + seed, pubB64, err := release.GenerateKey() |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + priv, err := release.ParseSigningKey(seed, "e2e") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + pub, err := release.ParsePublicKey(pubB64, "e2e") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + cst, err := console.Open(filepath.Join(tmp, "console.db")) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(func() { cst.Close() }) |
| + |
| + var srv *console.Server |
| + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| + srv.ServeHTTP(w, r) |
| + })) |
| + t.Cleanup(ts.Close) |
| + srv, err = console.NewServer(console.Config{ |
| + Origin: ts.URL, |
| + Keymail: "http://keymail.invalid", // never dialled — see e2eDeviceLogin |
| + Registry: filepath.Join(tmp, "registry.db"), |
| + Releases: relStore, |
| + ReleasePub: pub, |
| + Key: priv, |
| + AppScheme: "http", |
| + AppsDomain: "oncarlos.test", |
| + }, cst) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + |
| + const addr = "paul@keymail.dev" |
| + if err := cst.AddMember(console.OpsAccountID, addr, console.RoleOwner, time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := cst.CreateAppIn(console.OpsAccountID, "titogo", addr, time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + token, expiresAt := e2eDeviceLogin(t, ts.URL, cst, addr, "e2e@laptop") |
| + credFile, err := loadCredentials() |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + credFile.put(credential{Console: ts.URL, Address: addr, Token: token, ExpiresAt: expiresAt}) |
| + if err := credFile.save(); err != nil { |
| + t.Fatal(err) |
| + } |
| + |
| + return checkNoRegistry, relStore |
| +} |
| + |
| +// A rootfs instance's lines name their process end to end (#366 Task 11 |
| +// follow-up): a real applog.Sink tags them as the activator's capture |
| +// sites do, the drain is shipped as the agent's logshipFlush writes a |
| +// chunk, and the member reads them through the console with the real |
| +// runLogs. --field process=<name> narrows to one process; every line stays |
| +// on the app stream, never "release" (the ship/promote history's). |
| +func TestE2ELogsNameTheProcess(t *testing.T) { |
| + checkNoRegistry, relStore := startLogsE2E(t) |
| + |
| + const host = "titogo.bab.oncarlos.test" |
| + sink := applog.NewSink("box1", time.Now) |
| + sink.AppProcess(release.OpsAccount, "titogo", host, release.ProcessWeb, `{"msg":"web says hi","process":"spoof"}`) |
| + sink.AppProcess(release.OpsAccount, "titogo", host, "jobs", "worker tick") |
| + sink.AppProcess(release.OpsAccount, "titogo", host, release.ProcessRelease, "migrations ran") |
| + for k, recs := range sink.Drain() { |
| + body, err := applog.EncodeChunk(recs) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + key, err := applog.ChunkKey(k.Acct, k.App, recs[0].Box, applog.HourOf(recs[0].TS), recs[0].Seq, len(body)) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := relStore.Put(context.Background(), key, body, ""); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + |
| + buf := captureStdout(t) |
| + runLogs([]string{"-app", "titogo", "-account", "ops"}) |
| + out := buf.String() |
| + for _, want := range []string{"web says hi process=web", "worker tick process=jobs", "migrations ran process=release"} { |
| + if !strings.Contains(out, want) { |
| + t.Errorf("logs stdout missing %q:\n%s", want, out) |
| + } |
| + } |
| + if strings.Contains(out, "spoof") || strings.Contains(out, "[release]") || strings.Count(out, "[app]") != 3 { |
| + t.Errorf("want three [app] lines and the platform's process names only:\n%s", out) |
| + } |
| + |
| + for _, c := range []struct{ process, want string }{{"web", "web says hi"}, {"jobs", "worker tick"}, {"release", "migrations ran"}} { |
| + buf.Reset() |
| + runLogs([]string{"-app", "titogo", "-account", "ops", "-field", "process=" + c.process}) |
| + out := buf.String() |
| + if !strings.Contains(out, c.want) || strings.Count(out, "[app]") != 1 { |
| + t.Errorf("--field process=%s: want %q alone:\n%s", c.process, c.want, out) |
| + } |
| + } |
| + checkNoRegistry() |
| +} |