| 1 | export type StatsQueryObserver = (label: string, durationMs: number, rows: number) => void; |
| 2 | |
| 3 | const SLOW_QUERY_MS = 250; |
| 4 | const SAMPLE_RATE = 0.02; |
| 5 | |
| 6 | export function statsQueryObserver(route: string): StatsQueryObserver { |
| 7 | return (label, durationMs, rows) => { |
| 8 | if (durationMs < SLOW_QUERY_MS && Math.random() >= SAMPLE_RATE) return; |
| 9 | console.log(JSON.stringify({ |
| 10 | event: "d1_query_timing", |
| 11 | route, |
| 12 | label, |
| 13 | durationMs: Math.round(durationMs), |
| 14 | rows, |
| 15 | })); |
| 16 | }; |
| 17 | } |
| 18 |