/**
 * Structured per-request tracing with timing waterfall.
 * Zero allocation when not used — phases are only tracked when mark() is called.
 */
const BUFFER_SIZE = 50;
const recentTraces = [];
export function createTrace(requestId) {
    const marks = [];
    const startTime = Date.now();
    return {
        mark(phase) {
            marks.push({ name: phase, time: Date.now() });
        },
        end() {
            const endTime = Date.now();
            const phases = [];
            for (let i = 0; i < marks.length; i++) {
                const prevTime = i === 0 ? startTime : marks[i - 1].time;
                phases.push({
                    name: marks[i].name,
                    timestampMs: marks[i].time,
                    durationMs: marks[i].time - prevTime,
                });
            }
            const result = {
                requestId,
                phases,
                totalMs: endTime - startTime,
            };
            // Store in circular buffer
            recentTraces.push(result);
            if (recentTraces.length > BUFFER_SIZE) {
                recentTraces.shift();
            }
            return result;
        },
    };
}
export function getRecentTraces(count = 10) {
    return recentTraces.slice(-count);
}
export function resetTraces() {
    recentTraces.length = 0;
}
//# sourceMappingURL=request-trace.js.map