Skip to content

Commit ff72d1c

Browse files
committed
fix(prisma): Record SQL queries in the call which made them
The context for Prisma's query events was kept in a single module-level variable, overwritten by every model call, so with concurrent calls all the queries were recorded in whichever call started last. Each request, and each interactive transaction, now runs in a context of its own, like an instrumented async function, which replaces Recording.reserve(). Prisma 7 runs queries in JavaScript through driver adapters and emits query events in that context, so they are recorded where they belong. Batched requests (findUnique calls made at the same time, a $transaction array) run in the context of the first one, so their queries go to the closest context enclosing the batch. The Rust engine of earlier versions emits query events out of any context, with nothing telling which request made them. A query then goes to the client's request in progress or, if there are several, to the closest context enclosing them all. This is a best guess: the events can arrive after their request is done, once another has started. Sequential code is reliable in practice. Raw queries get a context too, so their SQL is now recorded where they were made instead of at the top level. The return event is emitted right away and filled in later, as for other async functions; with async_tracking_timeout set to 0, the SQL now follows the call instead of being nested in it. Query logging is now turned on through the public log option. Setting the engine config after construction did nothing on Prisma 7, so no SQL was recorded from query events there. When the adapter's driver is one we hook (pg, better-sqlite3), that hook records the queries with more detail, including failed ones, which Prisma doesn't report. The adapter passed to the client is wrapped so that its calls, and those of the connections and transactions it makes, run in a context naming the client; once a driver hook records a query in such a call, the client's query events are ignored. This also covers the driverAdapters preview feature of earlier versions. (Such a hook places batched queries in the batch's first call.) test/prisma7 covers Prisma 7 with a libsql adapter (query events) and a better-sqlite3 one (driver hook). Prisma 7 needs Node 20.19+, so the tests are skipped below that and its preinstall check is disabled so that installing still works on Node 18. better-sqlite3 is held at 11.x for the adapter, as 12 doesn't support Node 18. Assisted-by: Claude:claude-opus-5-5
1 parent 3c72fad commit ff72d1c

14 files changed

Lines changed: 4291 additions & 209 deletions

File tree

‎package.json‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,13 @@
9898
"ts-blank-space": "^0.7.0",
9999
"yaml": "^2.3.4"
100100
},
101+
"dependenciesMeta": {
102+
"prisma@7.10.1": {
103+
"built": false
104+
}
105+
},
101106
"resolutions": {
107+
"@prisma/adapter-better-sqlite3/better-sqlite3": "^11.10.0",
102108
"node-gyp": "^11.5.0",
103109
"postcss": "^8.5.26",
104110
"rollup": "^4.59.0",
@@ -122,6 +128,7 @@
122128
"test/next16",
123129
"test/typescript-esm",
124130
"test/prisma",
131+
"test/prisma7",
125132
"test/libraryCalls",
126133
"test/sourceMapPath"
127134
]

‎src/Recording.ts‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,11 @@ export default class Recording {
109109
this.fixup(makeExceptionEvent(event.id, event.parent_id, exception, getTime() - startTime));
110110
}
111111

112+
// Called whenever an SQL query is recorded, in the async context which made it.
113+
public static onSqlQuery: (() => void) | undefined;
114+
112115
sqlQuery(databaseType: string, sql: string): AppMap.SqlQueryEvent {
116+
Recording.onSqlQuery?.();
113117
const event: AppMap.SqlQueryEvent = {
114118
event: "call",
115119
sql_query: compactObject({
@@ -376,15 +380,6 @@ export default class Recording {
376380
};
377381
}
378382

379-
// Reserves the current position for events emitted later in the returned
380-
// context, eg. the return event of a call whose result comes in a callback.
381-
// Events emitted afterwards in the current context are held back until the
382-
// reservation is completed.
383-
public static reserve(): { context: EventBuffer | undefined; complete: () => void } {
384-
const { result: context, complete } = Recording.fork(() => Recording.getContext());
385-
return { context, complete };
386-
}
387-
388383
public static run<T>(context: EventBuffer | undefined, fun: () => T): T {
389384
return context ? Recording.asyncStorage.run(context, fun) : fun();
390385
}

‎src/hooks/prisma.ts‎

Lines changed: 219 additions & 82 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)