|
| 1 | +import type { EventEmitter } from "node:events"; |
| 2 | +import { ColumnUsageAnalyzer } from "./column-usage-analyzer.ts"; |
| 3 | + |
| 4 | +interface QueryLike { |
| 5 | + sanitizedQuery?: string; |
| 6 | + traceId?: string; |
| 7 | + correlationId?: string; |
| 8 | + suggestions?: { rule: string }[]; |
| 9 | +} |
| 10 | + |
| 11 | +interface HttpLike { |
| 12 | + method?: string; |
| 13 | + url?: string; |
| 14 | + durationMs?: number; |
| 15 | + traceId?: string; |
| 16 | +} |
| 17 | + |
| 18 | +interface SlowQueryLike { |
| 19 | + sanitizedQuery?: string; |
| 20 | + durationMs?: number; |
| 21 | + driver?: string; |
| 22 | +} |
| 23 | + |
| 24 | +interface PoolExhaustionLike { |
| 25 | + driver?: string; |
| 26 | + waitingCount?: number; |
| 27 | +} |
| 28 | + |
| 29 | +export const CROSS_SIGNAL_THRESHOLDS = { |
| 30 | + SLOW_HTTP_MS: 1_000, |
| 31 | + POOL_STARVATION_WINDOW_MS: 10_000, |
| 32 | + N_PLUS_ONE_TTL_MS: 30_000, |
| 33 | +} as const; |
| 34 | + |
| 35 | +export interface CrossSignalRuleEngineOptions { |
| 36 | + columnUsageDir?: string | null; |
| 37 | + columnUsageThreshold?: number; |
| 38 | + selectStarHits?: Map<string, number>; |
| 39 | + parseLineFromSourceLine?: (sourceLine: string) => number; |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Wires cross-signal correlation rules (R.3–R.7) onto a shared EventEmitter. |
| 44 | + * Each rule listens for events emitted by individual monitors and combines them |
| 45 | + * into higher-signal compound anomalies. |
| 46 | + */ |
| 47 | +export class CrossSignalRuleEngine { |
| 48 | + private readonly emitter: EventEmitter; |
| 49 | + private readonly opts: CrossSignalRuleEngineOptions; |
| 50 | + private readonly listeners: [string, (...args: unknown[]) => void][] = []; |
| 51 | + |
| 52 | + constructor(emitter: EventEmitter, opts: CrossSignalRuleEngineOptions = {}) { |
| 53 | + this.emitter = emitter; |
| 54 | + this.opts = opts; |
| 55 | + } |
| 56 | + |
| 57 | + wire(): void { |
| 58 | + const SLOW_HTTP_MS = CROSS_SIGNAL_THRESHOLDS.SLOW_HTTP_MS; |
| 59 | + const POOL_STARVATION_WINDOW_MS = CROSS_SIGNAL_THRESHOLDS.POOL_STARVATION_WINDOW_MS; |
| 60 | + const N_PLUS_ONE_TTL_MS = CROSS_SIGNAL_THRESHOLDS.N_PLUS_ONE_TTL_MS; |
| 61 | + |
| 62 | + // traceId → timestamp of last N+1 detection (R.3) |
| 63 | + const nPlusOneByTraceId = new Map<string, number>(); |
| 64 | + // query key → { timestamp, sanitizedQuery, durationMs, driver } (R.4) |
| 65 | + const recentSlowQueries = new Map< |
| 66 | + string, |
| 67 | + { timestamp: number; sanitizedQuery: string; durationMs: number; driver?: string } |
| 68 | + >(); |
| 69 | + // traceId/correlationId of currently open transactions (R.5) |
| 70 | + const openTransactions = new Set<string>(); |
| 71 | + |
| 72 | + const onQuery = (event: QueryLike): void => { |
| 73 | + const sql = event.sanitizedQuery ?? ""; |
| 74 | + const traceKey = event.traceId ?? event.correlationId; |
| 75 | + |
| 76 | + // R.5: track open transactions |
| 77 | + if (traceKey) { |
| 78 | + if (/^\s*BEGIN\b/i.test(sql)) { |
| 79 | + openTransactions.add(traceKey); |
| 80 | + } else if (/^\s*(COMMIT|ROLLBACK)\b/i.test(sql)) { |
| 81 | + openTransactions.delete(traceKey); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + const hasNPlusOne = event.suggestions?.some((s) => s.rule === "n-plus-one"); |
| 86 | + |
| 87 | + // R.3: record traceIds with active N+1 |
| 88 | + if (hasNPlusOne && event.traceId) { |
| 89 | + nPlusOneByTraceId.set(event.traceId, Date.now()); |
| 90 | + } |
| 91 | + |
| 92 | + // R.5: N+1 inside open transaction |
| 93 | + if (hasNPlusOne && traceKey && openTransactions.has(traceKey)) { |
| 94 | + this.emitter.emit("anomaly", { |
| 95 | + type: "n-plus-one-in-transaction", |
| 96 | + traceId: event.traceId, |
| 97 | + correlationId: event.correlationId, |
| 98 | + suggestions: [ |
| 99 | + { |
| 100 | + severity: "critical", |
| 101 | + rule: "n-plus-one-in-transaction", |
| 102 | + message: |
| 103 | + "N+1 query pattern detected inside an open transaction — each repeated query holds the database connection and delays COMMIT.", |
| 104 | + suggestedFix: |
| 105 | + "Batch the repeated queries before opening the transaction, or use a JOIN/IN clause to reduce round-trips.", |
| 106 | + }, |
| 107 | + ], |
| 108 | + }); |
| 109 | + } |
| 110 | + }; |
| 111 | + |
| 112 | + const onHttp = (event: HttpLike): void => { |
| 113 | + // R.3: correlated-slow-endpoint |
| 114 | + if (!event.traceId || !event.durationMs || event.durationMs <= SLOW_HTTP_MS) return; |
| 115 | + const recordedAt = nPlusOneByTraceId.get(event.traceId); |
| 116 | + if (!recordedAt) return; |
| 117 | + if (Date.now() - recordedAt > N_PLUS_ONE_TTL_MS) { |
| 118 | + nPlusOneByTraceId.delete(event.traceId); |
| 119 | + return; |
| 120 | + } |
| 121 | + this.emitter.emit("anomaly", { |
| 122 | + type: "correlated-slow-endpoint", |
| 123 | + url: event.url, |
| 124 | + method: event.method, |
| 125 | + durationMs: event.durationMs, |
| 126 | + traceId: event.traceId, |
| 127 | + suggestions: [ |
| 128 | + { |
| 129 | + severity: "critical", |
| 130 | + rule: "correlated-slow-endpoint", |
| 131 | + message: `${event.method ?? "HTTP"} ${event.url ?? "(unknown)"} took ${event.durationMs}ms — N+1 query pattern active within the same request trace.`, |
| 132 | + suggestedFix: |
| 133 | + "Batch the repeated queries with IN (...) or a JOIN before this endpoint can scale.", |
| 134 | + }, |
| 135 | + ], |
| 136 | + }); |
| 137 | + }; |
| 138 | + |
| 139 | + const onSlowQuery = (event: SlowQueryLike): void => { |
| 140 | + // R.4: track recent slow queries keyed by a truncated query fingerprint |
| 141 | + if (!event.sanitizedQuery) return; |
| 142 | + const key = `${event.driver ?? ""}:${event.sanitizedQuery.slice(0, 120)}`; |
| 143 | + recentSlowQueries.set(key, { |
| 144 | + timestamp: Date.now(), |
| 145 | + sanitizedQuery: event.sanitizedQuery, |
| 146 | + durationMs: event.durationMs ?? 0, |
| 147 | + driver: event.driver, |
| 148 | + }); |
| 149 | + }; |
| 150 | + |
| 151 | + const onPoolExhaustion = (event: PoolExhaustionLike): void => { |
| 152 | + // R.4: pool-starvation-by-slow-query |
| 153 | + const now = Date.now(); |
| 154 | + const culprits: { sanitizedQuery: string; durationMs: number }[] = []; |
| 155 | + |
| 156 | + for (const [key, sq] of recentSlowQueries) { |
| 157 | + if (now - sq.timestamp > POOL_STARVATION_WINDOW_MS) { |
| 158 | + recentSlowQueries.delete(key); |
| 159 | + continue; |
| 160 | + } |
| 161 | + if (!event.driver || !sq.driver || sq.driver === event.driver) { |
| 162 | + culprits.push({ sanitizedQuery: sq.sanitizedQuery, durationMs: sq.durationMs }); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + if (culprits.length === 0) return; |
| 167 | + |
| 168 | + this.emitter.emit("anomaly", { |
| 169 | + type: "pool-starvation-by-slow-query", |
| 170 | + driver: event.driver, |
| 171 | + waitingCount: event.waitingCount, |
| 172 | + culprits, |
| 173 | + suggestions: [ |
| 174 | + { |
| 175 | + severity: "critical", |
| 176 | + rule: "pool-starvation-by-slow-query", |
| 177 | + message: `Connection pool exhausted (${event.waitingCount ?? 0} waiting) — ${culprits.length} slow ${event.driver ?? ""} quer${culprits.length === 1 ? "y is" : "ies are"} holding connections.`, |
| 178 | + suggestedFix: |
| 179 | + "Optimize the slow queries or increase pool size. Use EXPLAIN to identify missing indexes.", |
| 180 | + }, |
| 181 | + ], |
| 182 | + }); |
| 183 | + }; |
| 184 | + |
| 185 | + this.register("query", onQuery as (...args: unknown[]) => void); |
| 186 | + this.register("http", onHttp as (...args: unknown[]) => void); |
| 187 | + this.register("slow-query", onSlowQuery as (...args: unknown[]) => void); |
| 188 | + this.register("pool-exhaustion", onPoolExhaustion as (...args: unknown[]) => void); |
| 189 | + |
| 190 | + // R.7 hot-path-select-star |
| 191 | + if (this.opts.columnUsageDir) { |
| 192 | + const colDir = this.opts.columnUsageDir; |
| 193 | + const threshold = this.opts.columnUsageThreshold ?? 5; |
| 194 | + const selectStarHits = this.opts.selectStarHits ?? new Map<string, number>(); |
| 195 | + const parseLine = |
| 196 | + this.opts.parseLineFromSourceLine ?? |
| 197 | + ((s: string) => { |
| 198 | + const m = /:(\d+):\d+$/.exec(s); |
| 199 | + return m ? parseInt(m[1], 10) : 1; |
| 200 | + }); |
| 201 | + const analyzed = new Set<string>(); |
| 202 | + |
| 203 | + const onQueryForColUsage = (event: Record<string, unknown>): void => { |
| 204 | + const sq = typeof event.sanitizedQuery === "string" ? event.sanitizedQuery : ""; |
| 205 | + const sourceLine = typeof event.sourceLine === "string" ? event.sourceLine : undefined; |
| 206 | + if (!sourceLine || !sq.toUpperCase().includes("SELECT *")) return; |
| 207 | + if (analyzed.has(sourceLine)) return; |
| 208 | + |
| 209 | + const hits = (selectStarHits.get(sourceLine) ?? 0) + 1; |
| 210 | + selectStarHits.set(sourceLine, hits); |
| 211 | + if (hits < threshold) return; |
| 212 | + |
| 213 | + analyzed.add(sourceLine); |
| 214 | + ColumnUsageAnalyzer.analyzeFile( |
| 215 | + colDir, |
| 216 | + sourceLine.replace(/:\d+:\d+$/, ""), |
| 217 | + parseLine(sourceLine), |
| 218 | + ) |
| 219 | + .then((fields) => { |
| 220 | + if (!fields) return; |
| 221 | + const suggestion = ColumnUsageAnalyzer.buildSuggestion( |
| 222 | + fields, |
| 223 | + sourceLine.replace(/:\d+:\d+$/, ""), |
| 224 | + parseLine(sourceLine), |
| 225 | + ); |
| 226 | + this.emitter.emit("anomaly", { |
| 227 | + type: "hot-path-select-star", |
| 228 | + sourceLine, |
| 229 | + fields: [...fields], |
| 230 | + suggestions: [suggestion], |
| 231 | + }); |
| 232 | + }) |
| 233 | + .catch((err: unknown) => { |
| 234 | + this.emitter.emit("warn", err); |
| 235 | + }); |
| 236 | + }; |
| 237 | + |
| 238 | + this.register("query", onQueryForColUsage); |
| 239 | + } |
| 240 | + } |
| 241 | + |
| 242 | + unwire(): void { |
| 243 | + for (const [event, fn] of this.listeners) { |
| 244 | + this.emitter.off(event, fn); |
| 245 | + } |
| 246 | + this.listeners.length = 0; |
| 247 | + } |
| 248 | + |
| 249 | + private register(event: string, handler: (...args: unknown[]) => void): void { |
| 250 | + this.emitter.on(event, handler); |
| 251 | + this.listeners.push([event, handler]); |
| 252 | + } |
| 253 | +} |
0 commit comments