UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

69 lines (68 loc) • 2.26 kB
import { r as runWithMainThreadTask } from "./main-thread-stall-BhUfYJnA.mjs"; import { r as emitDiagnosticEvent, t as areDiagnosticsEnabledForProcess } from "./diagnostic-events-BjAH1Kbx.mjs"; import { performance } from "node:perf_hooks"; //#region src/logging/diagnostic-metric.ts /** Normalize non-finite telemetry values before decimal rounding. */ function roundDiagnosticMetric(e, t = 3) { if (!Number.isFinite(e)) return 0; const n = 10 ** t; return Math.round(e * n) / n; } //#endregion //#region src/logging/diagnostic-phase.ts const RECENT_PHASE_CAPACITY = 40; let activePhaseStack = []; let recentPhases = []; function pushRecentPhase(e) { recentPhases.push(e); if (recentPhases.length > RECENT_PHASE_CAPACITY) recentPhases = recentPhases.slice(-40); } function getCurrentDiagnosticPhase() { return activePhaseStack.at(-1)?.name; } function getRecentDiagnosticPhases(e) { return recentPhases.filter((t) => t.endedAt !== void 0 && t.endedAt >= e).slice(-6); } function recordDiagnosticPhase(e) { pushRecentPhase(e); if (!areDiagnosticsEnabledForProcess()) return; emitDiagnosticEvent({ type: "diagnostic.phase.completed", ...e }); } /** Runs work inside a measured diagnostic phase with wall-clock and CPU metrics. */ async function withDiagnosticPhase(e, t, n) { const r = { name: e, startedAt: Date.now(), startedWallMs: performance.now(), cpuStarted: process.cpuUsage(), details: n }; activePhaseStack.push(r); try { return await runWithMainThreadTask(e, t); } finally { const t = Date.now(); const n = roundDiagnosticMetric(performance.now() - r.startedWallMs, 1); const i = process.cpuUsage(r.cpuStarted); const a = roundDiagnosticMetric(i.user / 1e3, 1); const o = roundDiagnosticMetric(i.system / 1e3, 1); const s = roundDiagnosticMetric(a + o, 1); activePhaseStack = activePhaseStack.filter((e) => e !== r); recordDiagnosticPhase({ name: e, startedAt: r.startedAt, endedAt: t, durationMs: n, cpuUserMs: a, cpuSystemMs: o, cpuTotalMs: s, cpuCoreRatio: roundDiagnosticMetric(s / Math.max(1, n), 3), details: r.details }); } } //#endregion export { roundDiagnosticMetric as i, getRecentDiagnosticPhases as n, withDiagnosticPhase as r, getCurrentDiagnosticPhase as t };