openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
5 lines • 1.8 kB
JavaScript
import{r as runWithMainThreadTask}from"./main-thread-stall-CJLZRvCI.mjs";import{r as emitDiagnosticEvent,t as areDiagnosticsEnabledForProcess}from"./diagnostic-events-BYBrlAdF.mjs";import{performance}from"node:perf_hooks";
//!src/logging/diagnostic-metric.ts
function roundDiagnosticMetric(e,t=3){if(!Number.isFinite(e))return 0;const n=10**t;return Math.round(e*n)/n}
//!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})}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})}}export{roundDiagnosticMetric as i,getRecentDiagnosticPhases as n,withDiagnosticPhase as r,getCurrentDiagnosticPhase as t};