litestream: hold replication logs to warnings (#802)
Human
Prompt
Can we take €802 ? carlos/platform#802
From this commit's own message. The prompt ledger has no entries for it — which is what a squashed, rebased or amended commit looks like, and what every commit looks like in a repo without a ledger.
Summary
carlos-litestream.service sets LOG_LEVEL=warn and the activator's embedded replicator gets a WARN floor, so per-sync INFO lines stop filling the journal.
Agent notes
LogLevelMax= rejected: journald stamps every stdout line priority info, so it would drop litestream's errors too. TestPlan14ActivationNamedUnitPreflight fails on unmodified main on docs-t1 (lifecycle provider unavailable); unrelated.
5 files changed,
+112
−0
internal/host/units/carlos-litestream.service+6 −0internal/host/units_isolation_test.go+14 −0internal/lease/lease.go+1 −0internal/lease/quietlog.go+41 −0internal/lease/quietlog_test.go+50 −0
diff --git a/internal/host/units/carlos-litestream.service b/internal/host/units/carlos-litestream.service| index 89472b450..235421d3b 100644 |
| --- a/internal/host/units/carlos-litestream.service |
| +++ b/internal/host/units/carlos-litestream.service |
| @@ -15,6 +15,12 @@ Wants=network-online.target |
| [Service] |
| ExecStart=/usr/local/bin/litestream replicate -config /etc/litestream.yml |
| EnvironmentFile=/data/litestream.env |
| +# Warnings and errors only. At INFO litestream logs one "replica sync" line |
| +# per database per sync, about 220k lines an hour on a box with a few dozen |
| +# databases, which pushed everything else out of a 256M journal after two |
| +# hours (discussion #802). LogLevelMax= is no substitute: journald stamps |
| +# every stdout line of this unit as info, so it would drop the errors too. |
| +Environment=LOG_LEVEL=warn |
| User=carlos |
| Group=carlos |
| CapabilityBoundingSet= |
diff --git a/internal/host/units_isolation_test.go b/internal/host/units_isolation_test.go| index 9b7d8b114..3b1b98f09 100644 |
| --- a/internal/host/units_isolation_test.go |
| +++ b/internal/host/units_isolation_test.go |
| @@ -635,3 +635,17 @@ func TestGenerationTemplateKeysNothingByItsInstanceString(t *testing.T) { |
| } |
| } |
| } |
| + |
| +// Litestream at INFO logs a line per database per sync and pushes everything |
| +// else out of a capped journal (#802). LOG_LEVEL holds it to warnings; a |
| +// LogLevelMax= would also drop its errors, since journald stamps every stdout |
| +// line of the unit as info. |
| +func TestLitestreamUnitLogsWarningsOnly(t *testing.T) { |
| + l := readUnit(t, "carlos-litestream.service") |
| + if !strings.Contains(l, "\nEnvironment=LOG_LEVEL=warn\n") { |
| + t.Error("carlos-litestream.service does not set LOG_LEVEL=warn") |
| + } |
| + if strings.Contains(l, "\nLogLevelMax=") { |
| + t.Error("carlos-litestream.service sets LogLevelMax=, which would drop litestream's errors") |
| + } |
| +} |
diff --git a/internal/lease/lease.go b/internal/lease/lease.go| index 1c7e7b7c6..2801676c4 100644 |
| --- a/internal/lease/lease.go |
| +++ b/internal/lease/lease.go |
| @@ -457,6 +457,7 @@ func (h *Held) newStore(dbPath string, syncInterval time.Duration) (*litestream. |
| db.Replica.SyncInterval = syncInterval |
| } |
| store := litestream.NewStore([]*litestream.DB{db}, compactionLevels) |
| + quietStoreLogs(store, db) |
| store.SnapshotInterval = SnapshotInterval |
| store.SnapshotRetention = SnapshotRetention |
| if h.generationPrefix != "" { |
diff --git a/internal/lease/quietlog.go b/internal/lease/quietlog.go| new file mode 100644 |
| index 000000000..315b56757 |
| --- /dev/null |
| +++ b/internal/lease/quietlog.go |
| @@ -0,0 +1,41 @@ |
| +package lease |
| + |
| +import ( |
| + "context" |
| + "log/slog" |
| + "path/filepath" |
| + |
| + "github.com/benbjohnson/litestream" |
| +) |
| + |
| +// Litestream logs one INFO "replica sync" line per database on every sync. |
| +// The box-wide binary is held to WARN by LOG_LEVEL in its unit; this is the |
| +// same floor for the replicator embedded here, whose lines would otherwise |
| +// land in carlos's own journal through slog.Default(). On a capped journal |
| +// those lines are what pushed everything else out after about two hours |
| +// (discussion #802). Warnings and errors still come through. |
| + |
| +// warnFloor passes on only records at WARN or above. |
| +type warnFloor struct{ slog.Handler } |
| + |
| +func (h warnFloor) Enabled(ctx context.Context, l slog.Level) bool { |
| + return l >= slog.LevelWarn && h.Handler.Enabled(ctx, l) |
| +} |
| + |
| +func (h warnFloor) WithAttrs(attrs []slog.Attr) slog.Handler { |
| + return warnFloor{h.Handler.WithAttrs(attrs)} |
| +} |
| + |
| +func (h warnFloor) WithGroup(name string) slog.Handler { |
| + return warnFloor{h.Handler.WithGroup(name)} |
| +} |
| + |
| +// quietStoreLogs replaces the loggers litestream.NewStore gave the store and |
| +// its database with the same ones held to WARN. Call it after NewStore and |
| +// before Open: NewStore sets the database's logger, Open starts the work |
| +// that logs. |
| +func quietStoreLogs(store *litestream.Store, db *litestream.DB) { |
| + quiet := slog.New(warnFloor{slog.Default().Handler()}).With(litestream.LogKeySystem, litestream.LogSystemStore) |
| + store.Logger = quiet |
| + db.SetLogger(quiet.With(litestream.LogKeyDB, filepath.Base(db.Path()))) |
| +} |
diff --git a/internal/lease/quietlog_test.go b/internal/lease/quietlog_test.go| new file mode 100644 |
| index 000000000..ad6ef9d3a |
| --- /dev/null |
| +++ b/internal/lease/quietlog_test.go |
| @@ -0,0 +1,50 @@ |
| +package lease |
| + |
| +import ( |
| + "bytes" |
| + "log/slog" |
| + "path/filepath" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + lss3 "github.com/benbjohnson/litestream/s3" |
| +) |
| + |
| +// The embedded replicator's per-sync INFO lines stay out of carlos's journal |
| +// (#802); its warnings still reach it, labelled as litestream labels them. |
| +func TestNewStoreHoldsLitestreamToWarn(t *testing.T) { |
| + var buf bytes.Buffer |
| + prev := slog.Default() |
| + slog.SetDefault(slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug}))) |
| + t.Cleanup(func() { slog.SetDefault(prev) }) |
| + |
| + l := lss3.NewLeaser() |
| + l.Path = "instances/x.example/inc" |
| + h := &Held{leaser: l, replicaClient: &generationFake{}} |
| + store, db, err := h.newStore(filepath.Join(t.TempDir(), "app.db"), time.Second) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + |
| + db.Replica.Logger().Info("replica sync") |
| + db.Logger.Info("db info") |
| + store.Logger.Info("starting compaction monitor") |
| + db.Replica.Logger().Warn("replica sync warn") |
| + db.Logger.Error("compaction failed") |
| + |
| + got := buf.String() |
| + for _, quiet := range []string{"msg=\"replica sync\"", "db info", "starting compaction monitor"} { |
| + if strings.Contains(got, quiet) { |
| + t.Errorf("INFO line %s reached the log:\n%s", quiet, got) |
| + } |
| + } |
| + for _, loud := range []string{"replica sync warn", "compaction failed"} { |
| + if !strings.Contains(got, loud) { |
| + t.Errorf("%q did not reach the log:\n%s", loud, got) |
| + } |
| + } |
| + if !strings.Contains(got, "system=store") || !strings.Contains(got, "db=app.db") { |
| + t.Errorf("warnings lost litestream's labels:\n%s", got) |
| + } |
| +} |