1import * as Tracer from "effect/Tracer";
2
3export interface SqlStatementCounter {
4 readonly tracer: Tracer.Tracer;
5 /** Statements executed so far. Read before and after a phase and subtract. */
6 readonly count: () => number;
7}
8
9/**
10 * Counts the client spans the Effect SQL client opens once per statement. Our
11 * SQLite client names them after its `db.system.name`, `sqlite`. Spans behave
12 * exactly as with the native tracer. Install the same counter on every runtime
13 * under test, otherwise statements run by background reactors and statements
14 * run by request handlers land in different counters.
15 */
16export function makeSqlStatementCounter(): SqlStatementCounter {
17 let statements = 0;
18 const tracer = Tracer.make({
19 span: (options) => {
20 if (options.kind === "client" && options.name === "sqlite") statements += 1;
21 return new Tracer.NativeSpan(options);
22 },
23 });
24 return { tracer, count: () => statements };
25}