(req: Request, res: Response, next: NextFunction)
| 76 | * Only tracks /api/* paths to avoid noise from static assets. |
| 77 | */ |
| 78 | export function slowResponseTracker(req: Request, res: Response, next: NextFunction): void { |
| 79 | if (!req.path.startsWith("/api/")) { |
| 80 | return next(); |
| 81 | } |
| 82 | |
| 83 | const start = process.hrtime.bigint(); |
| 84 | |
| 85 | res.on("finish", () => { |
| 86 | const durationMs = Number(process.hrtime.bigint() - start) / 1e6; |
| 87 | |
| 88 | if (durationMs > WARN_THRESHOLD_MS) { |
| 89 | logger.warn( |
| 90 | { |
| 91 | method: req.method, |
| 92 | path: req.path.slice(0, 200), |
| 93 | status: res.statusCode, |
| 94 | duration_ms: Math.round(durationMs), |
| 95 | }, |
| 96 | "Slow API response" |
| 97 | ); |
| 98 | |
| 99 | if (durationMs > ALERT_THRESHOLD_MS && shouldAlert(normalizeAlertKey(req.method, req.path))) { |
| 100 | notifySlack(req.method, req.path, durationMs, res.statusCode); |
| 101 | } |
| 102 | } |
| 103 | }); |
| 104 | |
| 105 | next(); |
| 106 | } |
nothing calls this directly
no test coverage detected