

























@@ -47,6 +47,10 @@ type ChannelHealthMonitorConfig = HealthMonitorConfig & {
4747accounts?: Record<string, HealthMonitorConfig>;
4848};
494950+type GatewayStartupTrace = {
51+measure: <T>(name: string, run: () => T | Promise<T>) => Promise<T>;
52+};
53+5054function createRuntimeStore(): ChannelRuntimeStore {
5155return {
5256aborts: new Map(),
@@ -161,6 +165,7 @@ type ChannelManagerOptions = {
161165 * `createPluginRuntime().channel` surface.
162166 */
163167resolveChannelRuntime?: () => ChannelRuntimeSurface | Promise<ChannelRuntimeSurface>;
168+startupTrace?: GatewayStartupTrace;
164169};
165170166171type StartChannelOptions = {
@@ -187,6 +192,7 @@ export function createChannelManager(opts: ChannelManagerOptions): ChannelManage
187192 channelRuntimeEnvs,
188193 channelRuntime,
189194 resolveChannelRuntime,
195+ startupTrace,
190196} = opts;
191197192198const channelStores = new Map<ChannelId, ChannelRuntimeStore>();
@@ -286,6 +292,9 @@ export function createChannelManager(opts: ChannelManagerOptions): ChannelManage
286292const getChannelRuntime = async (): Promise<ChannelRuntimeSurface | undefined> => {
287293return channelRuntime ?? (await resolveChannelRuntime?.());
288294};
295+const measureStartup = async <T>(name: string, run: () => T | Promise<T>): Promise<T> => {
296+return startupTrace ? startupTrace.measure(name, run) : await run();
297+};
289298290299const evictStaleChannelAccountState = (
291300channelId: ChannelId,
@@ -322,7 +331,11 @@ export function createChannelManager(opts: ChannelManagerOptions): ChannelManage
322331const cfg = getRuntimeConfig();
323332resetDirectoryCache({ channel: channelId, accountId });
324333const store = getStore(channelId);
325-const accountIds = accountId ? [accountId] : plugin.config.listAccountIds(cfg);
334+const accountIds = accountId
335+ ? [accountId]
336+ : await measureStartup(`channels.${channelId}.list-accounts`, () =>
337+plugin.config.listAccountIds(cfg),
338+);
326339if (!accountId) {
327340evictStaleChannelAccountState(channelId, store, accountIds);
328341}
@@ -391,7 +404,9 @@ export function createChannelManager(opts: ChannelManagerOptions): ChannelManage
391404392405let configured = true;
393406if (plugin.config.isConfigured) {
394-configured = await plugin.config.isConfigured(account, cfg);
407+configured = await measureStartup(`channels.${channelId}.is-configured`, () =>
408+plugin.config.isConfigured!(account, cfg),
409+);
395410}
396411if (!configured) {
397412setRuntime(channelId, id, {
@@ -420,21 +435,31 @@ export function createChannelManager(opts: ChannelManagerOptions): ChannelManage
420435return;
421436}
422437423-scopedChannelRuntime = createTaskScopedChannelRuntime({
424-channelRuntime: await getChannelRuntime(),
425-});
438+scopedChannelRuntime = await measureStartup(`channels.${channelId}.runtime`, async () =>
439+createTaskScopedChannelRuntime({
440+channelRuntime: await getChannelRuntime(),
441+}),
442+);
426443channelRuntimeForTask = scopedChannelRuntime.channelRuntime;
427444428445if (!preserveRestartAttempts) {
429446restartAttempts.delete(rKey);
430447}
431-stopApprovalBootstrap = await startChannelApprovalHandlerBootstrap({
432- plugin,
433- cfg,
434-accountId: id,
435-channelRuntime: channelRuntimeForTask,
436-logger: log,
437-});
448+try {
449+stopApprovalBootstrap = await measureStartup(
450+`channels.${channelId}.approval-bootstrap`,
451+() =>
452+startChannelApprovalHandlerBootstrap({
453+ plugin,
454+ cfg,
455+accountId: id,
456+channelRuntime: channelRuntimeForTask,
457+logger: log,
458+}),
459+);
460+} catch (error) {
461+log.error?.(`[${id}] native approval bootstrap failed: ${formatErrorMessage(error)}`);
462+}
438463setRuntime(channelId, id, {
439464accountId: id,
440465enabled: true,
@@ -446,17 +471,19 @@ export function createChannelManager(opts: ChannelManagerOptions): ChannelManage
446471reconnectAttempts: preserveRestartAttempts ? (restartAttempts.get(rKey) ?? 0) : 0,
447472});
448473const task = Promise.resolve().then(() =>
449-startAccount({
450- cfg,
451-accountId: id,
452- account,
453-runtime: channelRuntimeEnvs[channelId],
454-abortSignal: abort.signal,
455- log,
456-getStatus: () => getRuntime(channelId, id),
457-setStatus: (next) => setRuntime(channelId, id, next),
458- ...(channelRuntimeForTask ? { channelRuntime: channelRuntimeForTask } : {}),
459-}),
474+measureStartup(`channels.${channelId}.start-account`, () =>
475+startAccount({
476+ cfg,
477+accountId: id,
478+ account,
479+runtime: channelRuntimeEnvs[channelId],
480+abortSignal: abort.signal,
481+ log,
482+getStatus: () => getRuntime(channelId, id),
483+setStatus: (next) => setRuntime(channelId, id, next),
484+ ...(channelRuntimeForTask ? { channelRuntime: channelRuntimeForTask } : {}),
485+}),
486+),
460487);
461488const trackedPromise = task
462489.catch((err) => {
@@ -636,7 +663,7 @@ export function createChannelManager(opts: ChannelManagerOptions): ChannelManage
636663return;
637664}
638665try {
639-await startChannel(plugin.id);
666+await measureStartup(`channels.${plugin.id}.start`, () => startChannel(plugin.id));
640667} catch (err) {
641668channelLogs[plugin.id]?.error?.(
642669`[${plugin.id}] channel startup failed: ${formatErrorMessage(err)}`,
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。