Files
openclaw/src/cli/startup-trace.ts
Peter Steinberger e2ec8283c4 refactor(deadcode): trim mid-size src exports (#105888)
* refactor(deadcode): trim auto-reply and CLI exports

* refactor(deadcode): trim cron and task exports

* refactor(deadcode): trim fleet and process exports

* test(deadcode): exercise live task and process seams

* test(fleet): cover stream redaction through owner module

* refactor(security): trim dead internal exports

* refactor(secrets): trim dead internal exports

* refactor(deadcode): trim remaining src exports

* refactor(deadcode): remove test-only runtime exports

* refactor(deadcode): trim pairing test exports

* refactor(deadcode): reconcile refreshed baseline

* test(auto-reply): deduplicate queue state imports
2026-07-13 00:42:56 -07:00

45 lines
1.3 KiB
TypeScript

// Shared gateway startup tracing for the entry wrapper and CLI dispatcher.
import process from "node:process";
import { isTruthyEnvValue } from "../infra/env.js";
type GatewayStartupTraceSource = "entry" | "cli.main";
export function createGatewayStartupTrace(
argv: string[],
source: GatewayStartupTraceSource,
): {
mark(name: string): void;
measure<T>(name: string, run: () => T | PromiseLike<T>): Promise<T>;
} {
const enabled =
isTruthyEnvValue(process.env.OPENCLAW_GATEWAY_STARTUP_TRACE) &&
argv.slice(2).includes("gateway");
const started = performance.now();
let last = started;
const emit = (name: string, durationMs: number, totalMs: number) => {
if (!enabled) {
return;
}
process.stderr.write(
`[gateway] startup trace: ${source}.${name} ${durationMs.toFixed(1)}ms total=${totalMs.toFixed(1)}ms\n`,
);
};
return {
mark(name: string) {
const now = performance.now();
emit(name, now - last, now - started);
last = now;
},
async measure<T>(name: string, run: () => T | PromiseLike<T>): Promise<T> {
const before = performance.now();
try {
return await run();
} finally {
const now = performance.now();
emit(name, now - before, now - started);
last = now;
}
},
};
}