-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathqueryPerformanceMonitor.server.ts
More file actions
64 lines (55 loc) · 1.68 KB
/
queryPerformanceMonitor.server.ts
File metadata and controls
64 lines (55 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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,
});