UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

71 lines (70 loc) • 2.5 kB
import { r as emitDiagnosticEvent, t as areDiagnosticsEnabledForProcess } from "./diagnostic-events-BADdmgZf.mjs"; import { performance } from "node:perf_hooks"; //#region src/logging/diagnostic-phase.ts const RECENT_PHASE_CAPACITY = 40; let activePhaseStack = []; let recentPhases = []; function roundMetric(value, digits = 1) { if (!Number.isFinite(value)) return 0; const factor = 10 ** digits; return Math.round(value * factor) / factor; } function pushRecentPhase(snapshot) { recentPhases.push(snapshot); if (recentPhases.length > RECENT_PHASE_CAPACITY) recentPhases = recentPhases.slice(-40); } function getCurrentDiagnosticPhase() { return activePhaseStack.at(-1)?.name; } function getRecentDiagnosticPhases(limit = 8, options) { if (!Number.isFinite(limit) || limit <= 0) return []; const completedAfter = options?.completedAfter; return (completedAfter === void 0 ? recentPhases : recentPhases.filter((phase) => phase.endedAt !== void 0 && phase.endedAt >= completedAfter)).slice(-Math.floor(limit)).map((phase) => Object.assign({}, phase)); } function recordDiagnosticPhase(snapshot) { pushRecentPhase(snapshot); if (!areDiagnosticsEnabledForProcess()) return; emitDiagnosticEvent({ type: "diagnostic.phase.completed", ...snapshot }); } /** Runs work inside a measured diagnostic phase with wall-clock and CPU metrics. */ async function withDiagnosticPhase(name, run, details) { const active = { name, startedAt: Date.now(), startedWallMs: performance.now(), cpuStarted: process.cpuUsage(), details }; activePhaseStack.push(active); try { return await run(); } finally { const endedAt = Date.now(); const durationMs = roundMetric(performance.now() - active.startedWallMs, 1); const cpu = process.cpuUsage(active.cpuStarted); const cpuUserMs = roundMetric(cpu.user / 1e3, 1); const cpuSystemMs = roundMetric(cpu.system / 1e3, 1); const cpuTotalMs = roundMetric(cpuUserMs + cpuSystemMs, 1); activePhaseStack = activePhaseStack.filter((entry) => entry !== active); recordDiagnosticPhase({ name, startedAt: active.startedAt, endedAt, durationMs, cpuUserMs, cpuSystemMs, cpuTotalMs, cpuCoreRatio: roundMetric(cpuTotalMs / Math.max(1, durationMs), 3), details: active.details }); } } function resetDiagnosticPhasesForTest() { activePhaseStack = []; recentPhases = []; } //#endregion export { withDiagnosticPhase as i, getRecentDiagnosticPhases as n, resetDiagnosticPhasesForTest as r, getCurrentDiagnosticPhase as t };