Files
triggerdotdev--trigger.dev/apps/webapp/app/utils/queryPerformanceMonitor.server.ts
Matt Aitken 24a915133e Prisma 6.14.0 upgrade (#2444)
* Initial work on upgrading to 6.14.0

Set the output to node_modules still to make it easier

* Use ./generated Prisma folder, update types to fix issues

* Docker compose restart Clickhouse

* Prisma instrumentation update

* Docker

* Removed database dockerignore file, add generated prisma client to the top-level one

* Delete v3-catalog package.json

* Resolved pnpm lock file

* Log errors for very slow queries
2025-08-27 16:52:58 +01:00

65 lines
1.7 KiB
TypeScript

import { env } from "~/env.server";
import { logger } from "~/services/logger.server";
export interface QueryPerformanceConfig {
verySlowQueryThreshold?: number; // ms
maxQueryLogLength: number;
}
export class QueryPerformanceMonitor {
private config: QueryPerformanceConfig;
constructor(config: Partial<QueryPerformanceConfig> = {}) {
this.config = {
maxQueryLogLength: 1000,
...config,
};
}
onQuery(
clientType: "writer" | "replica",
log: {
duration: number;
query: string;
params: string;
target: string;
timestamp: Date;
}
) {
if (this.config.verySlowQueryThreshold === undefined) {
return;
}
const { duration, query, params, target, timestamp } = log;
// Only log very slow queries as errors
if (duration > this.config.verySlowQueryThreshold) {
// Truncate long queries for readability
const truncatedQuery =
query.length > this.config.maxQueryLogLength
? query.substring(0, this.config.maxQueryLogLength) + "..."
: query;
logger.error("Prisma: very slow database query", {
clientType,
durationMs: duration,
query: truncatedQuery,
target,
timestamp,
paramCount: this.countParams(query),
hasParams: params !== "[]" && params !== "",
});
}
}
private countParams(query: string): number {
// Count the number of $1, $2, etc. parameters in the query
const paramMatches = query.match(/\$\d+/g);
return paramMatches ? paramMatches.length : 0;
}
}
export const queryPerformanceMonitor = new QueryPerformanceMonitor({
verySlowQueryThreshold: env.VERY_SLOW_QUERY_THRESHOLD_MS,
});