-
Notifications
You must be signed in to change notification settings - Fork 25
feat(metrics): report startup crash cause in not-running alarm #168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
linrunqi08
merged 2 commits into
alibaba:main
from
Snssn:feat/report-startup-crash-cause
Jul 22, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import type { StartupCrashBreadcrumb } from '../utils/crash-breadcrumb.js'; | ||
|
|
||
| export type StartupCrashReason = | ||
| | 'native_module_missing' | ||
| | 'module_not_found' | ||
| | 'config_error' | ||
| | 'permission_or_disk' | ||
| | 'unknown'; | ||
|
|
||
| export interface StartupCrashClassification { | ||
| reason: StartupCrashReason; | ||
| detailHead: string; | ||
| } | ||
|
|
||
| const DETAIL_MAX_CHARS = 300; | ||
|
|
||
| /** | ||
| * Maps a raw crash breadcrumb to a stable `reason` label (for aggregation/alarming) | ||
| * plus a human-readable detail head. Rules are evaluated in order; the first match | ||
| * wins, and `unknown` always carries the raw message so nothing is lost. | ||
| */ | ||
| export function classifyStartupCrash(breadcrumb: StartupCrashBreadcrumb): StartupCrashClassification { | ||
| const message = (breadcrumb.error_message || '').toLowerCase(); | ||
| const full = `${breadcrumb.error_message}\n${breadcrumb.error_stack_head}`.toLowerCase(); | ||
| return { | ||
| reason: detectReason(message, full, breadcrumb.phase), | ||
| detailHead: sanitizeDetail(firstLine(breadcrumb.error_message)), | ||
| }; | ||
| } | ||
|
|
||
| function detectReason(message: string, full: string, phase: string): StartupCrashReason { | ||
| if ( | ||
| full.includes('sqlite3') | ||
| || full.includes('err_dlopen_failed') | ||
| || full.includes('did not self-register') | ||
| || /cannot find module\s+['"][^'"]*\.node['"]/.test(full) | ||
| || full.includes('install scripts') | ||
| || full.includes('node_module_version') | ||
| || full.includes('compiled against a different node') | ||
| ) { | ||
| return 'native_module_missing'; | ||
| } | ||
| if (full.includes('cannot find module')) { | ||
| return 'module_not_found'; | ||
| } | ||
| // Check permission/disk before config so a startup-phase EACCES whose message merely | ||
| // mentions "config" is not mislabeled as a configuration error. | ||
| if (full.includes('eacces') || full.includes('erofs') || full.includes('enospc')) { | ||
| return 'permission_or_disk'; | ||
| } | ||
| // config_error is intentionally narrow: only a JSON-parse signature in the error | ||
| // *message* (not the stack, which routinely contains config-loader.ts paths) during | ||
| // the startup phase. Bare "config"/"json" substrings are too broad. | ||
| if ( | ||
| phase === 'startup' | ||
| && ( | ||
| message.includes('unexpected token') | ||
| || message.includes('unexpected end of json') | ||
| || message.includes(' in json') | ||
| || message.includes('not valid json') | ||
| || message.includes('json.parse') | ||
| ) | ||
| ) { | ||
| return 'config_error'; | ||
| } | ||
| return 'unknown'; | ||
| } | ||
|
|
||
| function firstLine(text: string): string { | ||
| return (text || '').split(/\r?\n/)[0] ?? ''; | ||
| } | ||
|
|
||
| // Keep the detail safe to embed in `detail="..."` inside the alarm message: no quotes | ||
| // or control chars that could break downstream parsing/readability. | ||
| function sanitizeDetail(text: string): string { | ||
| return text.replace(/["\r\n\t]+/g, ' ').replace(/\s+/g, ' ').trim().slice(0, DETAIL_MAX_CHARS); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import * as fs from 'node:fs'; | ||
| import * as path from 'node:path'; | ||
| import { resolveHome } from './fs-utils.js'; | ||
|
|
||
| export type StartupCrashPhase = 'module_load' | 'startup' | 'runtime'; | ||
|
|
||
| export interface StartupCrashBreadcrumb { | ||
| schema: 1; | ||
| ts: number; | ||
| phase: StartupCrashPhase; | ||
| version: string; | ||
| pid: number; | ||
| error_message: string; | ||
| error_stack_head: string; | ||
| } | ||
|
|
||
| const FILE_NAME = 'last-startup-crash.json'; | ||
| const STACK_HEAD_MAX_LINES = 10; | ||
| const STACK_HEAD_MAX_CHARS = 4000; | ||
|
|
||
| export function startupCrashPath(dataDir: string): string { | ||
| return path.join(dataDir, 'logs', FILE_NAME); | ||
| } | ||
|
|
||
| /** | ||
| * The single data dir the breadcrumb lives in. It MUST match where the updater (the | ||
| * reader, src/updater/index.ts) and the bootstrap (scripts/collector-daemon.js, the | ||
| * earliest writer) look — both use env-or-default, NOT config.dataDir. Aligning the | ||
| * writer, clearer and reader on this one directory is what makes the "lingering | ||
| * breadcrumb = most recent failed startup" invariant hold even when config.json | ||
| * overrides dataDir. | ||
| */ | ||
| export function resolveBreadcrumbDataDir(): string { | ||
| return resolveHome(process.env.LOONGSUITE_PILOT_DATA_DIR ?? '~/.loongsuite-pilot'); | ||
| } | ||
|
|
||
| function truncateStackHead(stack: string | undefined): string { | ||
| if (!stack) return ''; | ||
| const head = stack.split(/\r?\n/).slice(0, STACK_HEAD_MAX_LINES).join('\n'); | ||
| return head.length > STACK_HEAD_MAX_CHARS ? head.slice(0, STACK_HEAD_MAX_CHARS) : head; | ||
| } | ||
|
|
||
| /** | ||
| * Persists the cause of an abnormal collector exit as a breadcrumb the updater can | ||
| * read later. Synchronous and best-effort: a write failure must never mask the | ||
| * original error or alter the exit code. | ||
| */ | ||
| export function writeStartupCrash(opts: { | ||
| dataDir: string; | ||
| phase: StartupCrashPhase; | ||
| version: string; | ||
| error: unknown; | ||
| }): void { | ||
| try { | ||
| const { error } = opts; | ||
| const breadcrumb: StartupCrashBreadcrumb = { | ||
| schema: 1, | ||
| ts: Math.floor(Date.now() / 1000), | ||
| phase: opts.phase, | ||
| version: opts.version || 'unknown', | ||
| pid: process.pid, | ||
| error_message: error instanceof Error ? error.message : String(error), | ||
| error_stack_head: truncateStackHead(error instanceof Error ? error.stack : undefined), | ||
| }; | ||
| const file = startupCrashPath(opts.dataDir); | ||
| fs.mkdirSync(path.dirname(file), { recursive: true }); | ||
| const tmp = `${file}.${process.pid}.${Date.now()}.tmp`; | ||
| fs.writeFileSync(tmp, `${JSON.stringify(breadcrumb, null, 2)}\n`, 'utf8'); | ||
| fs.renameSync(tmp, file); | ||
| } catch { | ||
| // best-effort | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Removes the breadcrumb after a healthy startup so a lingering file always means | ||
| * the most recent startup attempt failed (PID/version-independent correlation). | ||
| */ | ||
| export function clearStartupCrash(dataDir: string): void { | ||
| try { | ||
| fs.rmSync(startupCrashPath(dataDir), { force: true }); | ||
| } catch { | ||
| // ignore | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Reads the last-startup-crash breadcrumb, or null when absent/unreadable/unknown schema. | ||
| */ | ||
| export function readStartupCrash(dataDir: string): StartupCrashBreadcrumb | null { | ||
| try { | ||
| const parsed = JSON.parse(fs.readFileSync(startupCrashPath(dataDir), 'utf8')) as StartupCrashBreadcrumb; | ||
| return parsed && parsed.schema === 1 ? parsed : null; | ||
| } catch { | ||
| return null; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Medium]
dataDir三处解析不一致。这里的resolveDataDir()与src/updater/index.ts都只认env(LOONGSUITE_PILOT_DATA_DIR) ?? ~/.loongsuite-pilot,而src/index.ts的写入/清除用resolveHome(config.dataDir),其中config.dataDir = env ?? file.dataDir ?? default(config-loader.ts:208)。当用户仅在config.json设置dataDir(不设 env)时三者发散:module_load面包屑由本文件写到默认目录,但健康启动后clearStartupCrash(config.dataDir)清的是自定义目录 → 面包屑永不被清除,违反「lingering breadcrumb = 最近一次失败启动」不变式;后续一次无关的 not-running(手动停止/OOM)告警会读到陈旧面包屑并误标cause=native_module_missing。startupphase 面包屑写到自定义config.dataDir,但 updater 从默认目录读 → 读不到,增强静默失效。影响: config.json 覆盖 dataDir 场景下,清除失效导致陈旧原因误报 + startup-phase 增强丢失。
建议: 收敛到单一 dataDir 解析源——让本文件与
updater/index.ts也读取config.json的file.dataDir(与 config-loader 对齐),或让index.ts的写/清除改用相同的env ?? default解析。务必让「写/清除/读」落在同一目录。Generated by LoongSuite-Pilot Code Review Agent