pi-mono/packages/chord/test/examples/61-tutorial/src/telemetry.ts
Mario Zechner ff33ef42c4 feat(chord): telemetry from pages and workers stored by the server, who a span was for
The four telemetry gaps (rows on commits, chord.host, microsecond times, a read-only store) and: pages record and
send span batches over their link to the server's ingest object (checked, capped, rate-limited, stamped browser and
with the link's principal); workers send theirs too, so the server is the store's only writer; principalAttributes
lets an app choose what a span records about its principal. The tutorial shows a real page's click traced to the
worker, with the trace viewer.
2026-11-04 11:59:14 +00:00

35 lines
1.6 KiB
TypeScript

// The todo app's telemetry store: one SQLite file in the data directory, `telemetry.sqlite`. The server is its only
// writer: its own spans, and those every list's worker and every recording page send it; readers (the trace viewer,
// `trace.ts`) open it read-only with Chord's `openTelemetryStore` and read through the store's read interface.
import { mkdir } from "node:fs/promises";
import { dirname, join } from "node:path";
import { openNodeSqliteDatabase } from "../../../../src/sqlite/node.ts";
import { createSqliteTelemetry, type SqliteTelemetry } from "../../../../src/telemetry-sqlite/index.ts";
/** The telemetry store as one process opened it: its recorder (an adapter and the reads) and its close. */
export type TodoTelemetry = {
readonly store: SqliteTelemetry;
/** Flushes what the recorder still holds and closes the file. */
close(): Promise<void>;
};
/** The telemetry store's file in the data directory `dataDir`. */
export function telemetryFile(dataDir: string): string {
return join(dataDir, "telemetry.sqlite");
}
/** Opens the telemetry store at `file` for this process (its directory made, private, when missing). */
export async function openTelemetry(file: string): Promise<TodoTelemetry> {
// Spans carry who called what: the data directory is private before the store's first file appears in it.
await mkdir(dirname(file), { recursive: true, mode: 0o700 });
const db = await openNodeSqliteDatabase(file);
const store = await createSqliteTelemetry(db);
return {
store,
close: async () => {
await store.close();
await db.close();
},
};
}