Files
t3code/apps/server/src/persistence/Layers/Sqlite.ts
T
Theo BrowneandClaude Fable 5 3cc7cf3c83 perf(server): trace logs keep hours of history instead of minutes
The local trace file burned through its whole 100 MB rotation window in
10-36 minutes, which made incident audits impossible: by the time a
slow-PR-badge report came in, the relevant spans were already rotated
away. Two sources dominated: the terminal subprocess poller (~40% of
bytes: pgrep + up to three ps spawns per session per second, each a full
process-table enumeration) and sql.execute spans (~28%).

Write less:
- One shared ps/powershell process-table snapshot per poll tick serves
  every running terminal; per-session inspection is now pure lookup.
  This also removes 50-75 process spawns/sec on busy hosts.
- The poll tick runs untraced; an explicit
  terminal.subprocessActivityChanged span fires only on a real state
  flip.
- Tail policy in the local file tracer: failures, interruptions, spans
  with events, and slow spans always persist; fast successes are
  rate-limited per span name (50 per 30s). OTLP delegate still sees
  every span. T3CODE_TRACE_KEEP_ALL_SPANS=true restores full fidelity.
- Dropped constant db.name/service.name attributes from every
  sql.execute span.

Keep more:
- Rotated trace backups are gzipped (measured 10.6x on real span data)
  and T3CODE_TRACE_MAX_FILES now defaults to 30, so the same disk
  budget covers hours.
- TraceDiagnostics reads backups one file at a time (instead of
  slurping every rotation into memory) and transparently gunzips .gz
  backups; plain legacy backups still age along the chain.

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-08-08 21:12:57 -07:00

65 lines
2.2 KiB
TypeScript

import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as FileSystem from "effect/FileSystem";
import * as Path from "effect/Path";
import * as SqlClient from "effect/unstable/sql/SqlClient";
import type { SqlError } from "effect/unstable/sql/SqlError";
import { runMigrations } from "../Migrations.ts";
import { ServerConfig } from "../../config.ts";
type RuntimeSqliteLayerConfig = {
readonly filename: string;
readonly spanAttributes?: Record<string, unknown>;
};
type Loader = {
layer: (config: RuntimeSqliteLayerConfig) => Layer.Layer<SqlClient.SqlClient, SqlError>;
};
const defaultSqliteClientLoaders = {
bun: () => import("@effect/sql-sqlite-bun/SqliteClient"),
node: () => import("../NodeSqliteClient.ts"),
} satisfies Record<string, () => Promise<Loader>>;
const makeRuntimeSqliteLayer = Effect.fn("makeRuntimeSqliteLayer")(function* (
config: RuntimeSqliteLayerConfig,
) {
const runtime = process.versions.bun !== undefined ? "bun" : "node";
const loader = defaultSqliteClientLoaders[runtime];
const clientModule = yield* Effect.promise<Loader>(loader);
return clientModule.layer(config);
}, Layer.unwrap);
const setup = Layer.effectDiscard(
Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient;
yield* sql`PRAGMA foreign_keys = ON;`;
yield* sql`PRAGMA journal_mode = WAL;`;
yield* runMigrations();
}),
);
export const makeSqlitePersistenceLive = Effect.fn("makeSqlitePersistenceLive")(function* (
dbPath: string,
) {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
yield* fs.makeDirectory(path.dirname(dbPath), { recursive: true });
// No per-span attributes: sql.execute fires ~76/s under load and constant
// attributes on every span were a measurable share of local trace volume.
return Layer.provideMerge(setup, makeRuntimeSqliteLayer({ filename: dbPath }));
}, Layer.unwrap);
export const SqlitePersistenceMemory = Layer.provideMerge(
setup,
makeRuntimeSqliteLayer({ filename: ":memory:" }),
);
export const layerConfig = Layer.unwrap(
Effect.gen(function* () {
const { dbPath } = yield* ServerConfig;
return makeSqlitePersistenceLive(dbPath);
}),
);