diff --git a/apps/backend/src/prisma-client.tsx b/apps/backend/src/prisma-client.tsx index 64a478f18..fa00ad6e8 100644 --- a/apps/backend/src/prisma-client.tsx +++ b/apps/backend/src/prisma-client.tsx @@ -58,32 +58,40 @@ export async function rawQueryAll[]>(queries: Q): Promise<[] & { [K in keyof Q]: Awaited> }> { - if (queries.length === 0) return [] as any; + return await traceSpan({ + description: "raw SQL query", + attributes: { + "stack.raw-queries.length": queries.length, + ...Object.fromEntries(queries.map((q, index) => [`stack.raw-queries.${index}`, q.sql.text])), + }, + }, async () => { + if (queries.length === 0) return [] as any; - const query = Prisma.sql` - WITH ${Prisma.join(queries.map((q, index) => { - return Prisma.sql`${Prisma.raw("q" + index)} AS ( - ${q.sql} - )`; - }), ",\n")} + const query = Prisma.sql` + WITH ${Prisma.join(queries.map((q, index) => { + return Prisma.sql`${Prisma.raw("q" + index)} AS ( + ${q.sql} + )`; + }), ",\n")} - ${Prisma.join(queries.map((q, index) => { - return Prisma.sql` - SELECT - ${"q" + index} AS type, - row_to_json(c) AS json - FROM (SELECT * FROM ${Prisma.raw("q" + index)}) c - `; - }), "\nUNION ALL\n")} - `; - const rawResult = await prismaClient.$queryRaw(query) as { type: string, json: any }[]; - const unprocessed = new Array(queries.length).fill(null).map(() => [] as any[]); - for (const row of rawResult) { - const type = row.type; - const index = +type.slice(1); - unprocessed[index].push(row.json); - } - const postProcessed = queries.map((q, index) => q.postProcess(unprocessed[index])); - return postProcessed as any; + ${Prisma.join(queries.map((q, index) => { + return Prisma.sql` + SELECT + ${"q" + index} AS type, + row_to_json(c) AS json + FROM (SELECT * FROM ${Prisma.raw("q" + index)}) c + `; + }), "\nUNION ALL\n")} + `; + const rawResult = await prismaClient.$queryRaw(query) as { type: string, json: any }[]; + const unprocessed = new Array(queries.length).fill(null).map(() => [] as any[]); + for (const row of rawResult) { + const type = row.type; + const index = +type.slice(1); + unprocessed[index].push(row.json); + } + const postProcessed = queries.map((q, index) => q.postProcess(unprocessed[index])); + return postProcessed as any; + }); } diff --git a/apps/backend/src/route-handlers/smart-request.tsx b/apps/backend/src/route-handlers/smart-request.tsx index b940ca528..7359f4b5b 100644 --- a/apps/backend/src/route-handlers/smart-request.tsx +++ b/apps/backend/src/route-handlers/smart-request.tsx @@ -234,7 +234,7 @@ const parseAuth = withTraceSpan('smart request parseAuth', async (req: NextReque if (!result) throw new StatusError(401, "Invalid development key override"); } else if (adminAccessToken) { // TODO put this into the bundled queries above (not so important because this path is quite rare) - const internalUser = await extractUserFromAdminAccessToken({ token: adminAccessToken, projectId }); + await extractUserFromAdminAccessToken({ token: adminAccessToken, projectId }); if (!await queries.project) { // this happens if the project is still in the user's managedProjectIds, but has since been deleted throw new KnownErrors.InvalidProjectForAdminAccessToken();