mirror of
https://github.com/Kaelio/ktx.git
synced 2026-06-16 08:25:14 +02:00
feat(telemetry): collect PostHog $exception error reports in CLI and daemon (#262)
* feat(telemetry): add node exception reporter * feat(telemetry): report node cli exceptions * feat(telemetry): add daemon exception reporter * feat(telemetry): report daemon exceptions * docs(telemetry): document error reports * fix(telemetry): pass redaction snapshots from node call sites * test(telemetry): verify prepared node exception payload * fix(telemetry): close daemon exception lifecycle gaps * test(telemetry): verify prepared daemon exception payload * test(telemetry): close error collection acceptance gaps * test(telemetry): close posthog exception acceptance gaps
This commit is contained in:
parent
c3d8cedb0b
commit
fb7b94b60e
36 changed files with 2870 additions and 140 deletions
|
|
@ -529,6 +529,13 @@ export async function runCommanderKtxCli(
|
|||
try {
|
||||
return await runBareInteractiveCommand(program, io, context);
|
||||
} catch (error) {
|
||||
const telemetry = await import('./telemetry/index.js');
|
||||
await telemetry.reportException({
|
||||
error,
|
||||
context: { source: 'bare-interactive', handled: true, fatal: false },
|
||||
packageInfo: info,
|
||||
io,
|
||||
});
|
||||
io.stderr.write(`${formatCliError(error)}\n`);
|
||||
return 1;
|
||||
}
|
||||
|
|
@ -563,6 +570,23 @@ export async function runCommanderKtxCli(
|
|||
outcome: commandOutcomeForParseResult(parseError, exitCode),
|
||||
error: parseError,
|
||||
});
|
||||
if (
|
||||
parseError &&
|
||||
!isCommanderExit(parseError) &&
|
||||
!isKtxProjectMissingAbortError(parseError)
|
||||
) {
|
||||
await telemetryModule.reportException({
|
||||
error: parseError,
|
||||
context: {
|
||||
source: completed?.commandPath.join(' ') ?? 'commander parseAsync',
|
||||
handled: true,
|
||||
fatal: false,
|
||||
},
|
||||
projectDir: completed?.projectGroupAttached ? completed.projectDir : undefined,
|
||||
packageInfo: info,
|
||||
io,
|
||||
});
|
||||
}
|
||||
await telemetryModule.emitCompletedCommand({ completed, packageInfo: info, io });
|
||||
await telemetryModule.shutdownTelemetryEmitter();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -129,6 +129,48 @@ function installTelemetrySignalFlush(io: KtxCliIo, info: KtxCliPackageInfo): ()
|
|||
};
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export function createGlobalExceptionReporter(io: KtxCliIo, info: KtxCliPackageInfo) {
|
||||
return async (source: 'uncaughtException' | 'unhandledRejection', error: unknown): Promise<void> => {
|
||||
const { reportException, shutdownTelemetryEmitter } = await import('./telemetry/index.js');
|
||||
await reportException({
|
||||
error,
|
||||
context: { source, handled: false, fatal: true },
|
||||
io,
|
||||
packageInfo: info,
|
||||
immediate: true,
|
||||
});
|
||||
await shutdownTelemetryEmitter();
|
||||
};
|
||||
}
|
||||
|
||||
export function installGlobalExceptionHandlers(io: KtxCliIo, info: KtxCliPackageInfo): () => void {
|
||||
const report = createGlobalExceptionReporter(io, info);
|
||||
const handle = (source: 'uncaughtException' | 'unhandledRejection', error: unknown): void => {
|
||||
void (async () => {
|
||||
try {
|
||||
await report(source, error);
|
||||
} catch {
|
||||
// Best-effort: preserve Node's process termination behavior.
|
||||
}
|
||||
if (error instanceof Error && error.stack) {
|
||||
io.stderr.write(`${error.stack}\n`);
|
||||
} else {
|
||||
io.stderr.write(`${String(error)}\n`);
|
||||
}
|
||||
process.exit(1);
|
||||
})();
|
||||
};
|
||||
const onUncaught = (error: Error): void => handle('uncaughtException', error);
|
||||
const onUnhandled = (reason: unknown): void => handle('unhandledRejection', reason);
|
||||
process.on('uncaughtException', onUncaught);
|
||||
process.on('unhandledRejection', onUnhandled);
|
||||
return () => {
|
||||
process.off('uncaughtException', onUncaught);
|
||||
process.off('unhandledRejection', onUnhandled);
|
||||
};
|
||||
}
|
||||
|
||||
export async function runKtxCli(
|
||||
argv = process.argv.slice(2),
|
||||
io: KtxCliIo = process,
|
||||
|
|
@ -141,11 +183,14 @@ export async function runKtxCli(
|
|||
// Real-process entry only: flush telemetry if interrupted. Test/programmatic
|
||||
// callers pass their own `io`, so they never install process-level handlers.
|
||||
const removeSignalFlush = (io as unknown) === process ? installTelemetrySignalFlush(io, info) : undefined;
|
||||
const removeGlobalExceptionHandlers =
|
||||
(io as unknown) === process ? installGlobalExceptionHandlers(io, info) : undefined;
|
||||
try {
|
||||
return await runCommanderKtxCli(argv, io, deps, info, {
|
||||
runInit: runInitForCommander,
|
||||
});
|
||||
} finally {
|
||||
removeGlobalExceptionHandlers?.();
|
||||
removeSignalFlush?.();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,7 +16,8 @@ import { bold, dim, green, red, SYMBOLS } from './io/symbols.js';
|
|||
import { createKtxCliScanConnector } from './local-scan-connectors.js';
|
||||
import { profileMark } from './startup-profile.js';
|
||||
import { isDemoConnection } from './telemetry/demo-detect.js';
|
||||
import { emitTelemetryEvent } from './telemetry/index.js';
|
||||
import { emitTelemetryEvent, reportException } from './telemetry/index.js';
|
||||
import { collectTelemetryRedactionSecrets } from './telemetry/redaction-secrets.js';
|
||||
import { formatErrorDetail, scrubErrorClass } from './telemetry/scrubber.js';
|
||||
|
||||
profileMark('module:connection');
|
||||
|
|
@ -324,6 +325,21 @@ async function emitConnectionTest(input: {
|
|||
...(errorDetail ? { errorDetail } : {}),
|
||||
},
|
||||
});
|
||||
if (input.error) {
|
||||
await reportException({
|
||||
error: input.error,
|
||||
context: { source: 'connection test', handled: true, fatal: false },
|
||||
projectDir: input.project.projectDir,
|
||||
io: input.io,
|
||||
redactionSecrets: await collectTelemetryRedactionSecrets({
|
||||
project: input.project,
|
||||
connectionId: input.connectionId,
|
||||
includeLlm: false,
|
||||
includeEmbeddings: false,
|
||||
env: process.env,
|
||||
}),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function visualWidth(text: string): number {
|
||||
|
|
|
|||
|
|
@ -3,7 +3,13 @@ import type { ToolAnnotations } from '@modelcontextprotocol/sdk/types.js';
|
|||
import { z } from 'zod';
|
||||
import type { KtxCliIo } from '../../cli-runtime.js';
|
||||
import type { MemoryAgentInput } from '../../context/memory/types.js';
|
||||
import { emitTelemetryEvent, mcpTelemetrySampleRate, shouldEmitMcpTelemetry } from '../../telemetry/index.js';
|
||||
import {
|
||||
emitTelemetryEvent,
|
||||
mcpTelemetrySampleRate,
|
||||
reportException,
|
||||
shouldEmitMcpTelemetry,
|
||||
} from '../../telemetry/index.js';
|
||||
import { collectTelemetryRedactionSecrets } from '../../telemetry/redaction-secrets.js';
|
||||
import { scrubErrorClass } from '../../telemetry/scrubber.js';
|
||||
import type {
|
||||
KtxMcpClientInfo,
|
||||
|
|
@ -518,11 +524,26 @@ function registerParsedTool<TSchema extends z.ZodType>(
|
|||
},
|
||||
schema: TSchema,
|
||||
handler: (input: z.infer<TSchema>, context?: KtxMcpToolHandlerContext) => Promise<KtxMcpToolResult>,
|
||||
telemetry?: { projectDir?: string; io?: KtxCliIo },
|
||||
): void {
|
||||
server.registerTool(name, config, async (input, context) => {
|
||||
try {
|
||||
return await handler(schema.parse(input), context);
|
||||
} catch (error) {
|
||||
if (telemetry?.io) {
|
||||
await reportException({
|
||||
error,
|
||||
context: { source: `mcp:${name}`, handled: true, fatal: false },
|
||||
projectDir: telemetry.projectDir,
|
||||
io: telemetry.io,
|
||||
redactionSecrets: await collectTelemetryRedactionSecrets({
|
||||
projectDir: telemetry.projectDir,
|
||||
includeLlm: true,
|
||||
includeEmbeddings: true,
|
||||
env: process.env,
|
||||
}),
|
||||
});
|
||||
}
|
||||
return jsonErrorToolResult(formatToolError(error));
|
||||
}
|
||||
});
|
||||
|
|
@ -571,6 +592,20 @@ function instrumentMcpServer(
|
|||
}
|
||||
return result;
|
||||
} catch (error) {
|
||||
if (telemetry.io) {
|
||||
await reportException({
|
||||
error,
|
||||
context: { source: `mcp:${name}`, handled: true, fatal: false },
|
||||
projectDir: telemetry.projectDir,
|
||||
io: telemetry.io,
|
||||
redactionSecrets: await collectTelemetryRedactionSecrets({
|
||||
projectDir: telemetry.projectDir,
|
||||
includeLlm: true,
|
||||
includeEmbeddings: true,
|
||||
env: process.env,
|
||||
}),
|
||||
});
|
||||
}
|
||||
if (telemetry.io && telemetry.projectDir && shouldEmitMcpTelemetry()) {
|
||||
const errorClass = scrubErrorClass(error);
|
||||
await emitTelemetryEvent({
|
||||
|
|
@ -596,6 +631,7 @@ function instrumentMcpServer(
|
|||
|
||||
export function registerKtxContextTools(deps: RegisterKtxContextToolsDeps): void {
|
||||
const { ports, userContext } = deps;
|
||||
const toolTelemetry = { projectDir: deps.projectDir, io: deps.io };
|
||||
const server = instrumentMcpServer(deps.server, {
|
||||
projectDir: deps.projectDir,
|
||||
io: deps.io,
|
||||
|
|
@ -616,6 +652,7 @@ export function registerKtxContextTools(deps: RegisterKtxContextToolsDeps): void
|
|||
},
|
||||
connectionListSchema,
|
||||
async () => jsonToolResult({ connections: await connections.list() }),
|
||||
toolTelemetry,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -640,6 +677,7 @@ export function registerKtxContextTools(deps: RegisterKtxContextToolsDeps): void
|
|||
limit: input.limit,
|
||||
}),
|
||||
),
|
||||
toolTelemetry,
|
||||
);
|
||||
|
||||
registerParsedTool(
|
||||
|
|
@ -657,6 +695,7 @@ export function registerKtxContextTools(deps: RegisterKtxContextToolsDeps): void
|
|||
const page = await knowledge.read({ userId: userContext.userId, key: input.key });
|
||||
return page ? jsonToolResult(page) : jsonErrorToolResult(`Wiki page "${input.key}" was not found.`);
|
||||
},
|
||||
toolTelemetry,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -679,6 +718,7 @@ export function registerKtxContextTools(deps: RegisterKtxContextToolsDeps): void
|
|||
? jsonToolResult(source)
|
||||
: jsonErrorToolResult(`Semantic-layer source "${input.sourceName}" was not found.`);
|
||||
},
|
||||
toolTelemetry,
|
||||
);
|
||||
|
||||
registerParsedTool(
|
||||
|
|
@ -711,6 +751,7 @@ export function registerKtxContextTools(deps: RegisterKtxContextToolsDeps): void
|
|||
);
|
||||
return jsonToolResult(projectSlQueryResult(result, input.include));
|
||||
},
|
||||
toolTelemetry,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -728,6 +769,7 @@ export function registerKtxContextTools(deps: RegisterKtxContextToolsDeps): void
|
|||
},
|
||||
entityDetailsSchema,
|
||||
async (input) => jsonToolResult(await entityDetails.read(input)),
|
||||
toolTelemetry,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -745,6 +787,7 @@ export function registerKtxContextTools(deps: RegisterKtxContextToolsDeps): void
|
|||
},
|
||||
dictionarySearchSchema,
|
||||
async (input) => jsonToolResult(await dictionarySearch.search(input)),
|
||||
toolTelemetry,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -762,6 +805,7 @@ export function registerKtxContextTools(deps: RegisterKtxContextToolsDeps): void
|
|||
},
|
||||
discoverDataSchema,
|
||||
async (input) => jsonToolResult({ refs: await discover.search(input) }),
|
||||
toolTelemetry,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -791,6 +835,7 @@ export function registerKtxContextTools(deps: RegisterKtxContextToolsDeps): void
|
|||
),
|
||||
);
|
||||
},
|
||||
toolTelemetry,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -818,6 +863,7 @@ export function registerKtxContextTools(deps: RegisterKtxContextToolsDeps): void
|
|||
};
|
||||
return jsonToolResult(await memoryIngest.ingest(ingestInput));
|
||||
},
|
||||
toolTelemetry,
|
||||
);
|
||||
|
||||
registerParsedTool(
|
||||
|
|
@ -835,6 +881,7 @@ export function registerKtxContextTools(deps: RegisterKtxContextToolsDeps): void
|
|||
const status = await memoryIngest.status(input.runId);
|
||||
return status ? jsonToolResult(status) : jsonErrorToolResult(`Memory ingest run "${input.runId}" was not found.`);
|
||||
},
|
||||
toolTelemetry,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,7 +23,8 @@ import type { KtxScanArgs, KtxScanDeps } from './scan.js';
|
|||
import type { KtxTableRef } from './context/scan/types.js';
|
||||
import { profileMark } from './startup-profile.js';
|
||||
import { isDemoConnection } from './telemetry/demo-detect.js';
|
||||
import { emitProjectStackSnapshot, emitTelemetryEvent } from './telemetry/index.js';
|
||||
import { emitProjectStackSnapshot, emitTelemetryEvent, reportException } from './telemetry/index.js';
|
||||
import { collectTelemetryRedactionSecrets } from './telemetry/redaction-secrets.js';
|
||||
import { formatErrorDetail } from './telemetry/scrubber.js';
|
||||
|
||||
profileMark('module:public-ingest');
|
||||
|
|
@ -1119,30 +1120,63 @@ export async function runKtxPublicIngest(
|
|||
feature,
|
||||
});
|
||||
} catch (error) {
|
||||
await reportException({
|
||||
error,
|
||||
context: { source: 'ingest runtime', handled: true, fatal: false },
|
||||
projectDir: args.projectDir,
|
||||
io,
|
||||
redactionSecrets: await collectTelemetryRedactionSecrets({
|
||||
project,
|
||||
projectDir: args.projectDir,
|
||||
connectionId: args.targetConnectionId,
|
||||
includeLlm: true,
|
||||
includeEmbeddings: true,
|
||||
env: deps.env ?? process.env,
|
||||
}),
|
||||
});
|
||||
io.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
const { runContextBuild } = await import('./context-build-view.js');
|
||||
const contextBuild = deps.runContextBuild ?? runContextBuild;
|
||||
const result = await contextBuild(
|
||||
project,
|
||||
{
|
||||
try {
|
||||
const result = await contextBuild(
|
||||
project,
|
||||
{
|
||||
projectDir: args.projectDir,
|
||||
...(args.targetConnectionId ? { targetConnectionId: args.targetConnectionId } : {}),
|
||||
all: args.all,
|
||||
entrypoint: 'ingest',
|
||||
inputMode: args.inputMode,
|
||||
...(args.queryHistory ? { queryHistory: args.queryHistory } : {}),
|
||||
...(args.queryHistoryWindowDays !== undefined ? { queryHistoryWindowDays: args.queryHistoryWindowDays } : {}),
|
||||
...(args.scanMode ? { scanMode: args.scanMode } : {}),
|
||||
...(args.detectRelationships !== undefined ? { detectRelationships: args.detectRelationships } : {}),
|
||||
...(args.cliVersion ? { cliVersion: args.cliVersion } : {}),
|
||||
...(args.runtimeInstallPolicy ? { runtimeInstallPolicy: args.runtimeInstallPolicy } : {}),
|
||||
},
|
||||
io,
|
||||
);
|
||||
return result.exitCode;
|
||||
} catch (error) {
|
||||
await reportException({
|
||||
error,
|
||||
context: { source: 'ingest context-build', handled: true, fatal: false },
|
||||
projectDir: args.projectDir,
|
||||
...(args.targetConnectionId ? { targetConnectionId: args.targetConnectionId } : {}),
|
||||
all: args.all,
|
||||
entrypoint: 'ingest',
|
||||
inputMode: args.inputMode,
|
||||
...(args.queryHistory ? { queryHistory: args.queryHistory } : {}),
|
||||
...(args.queryHistoryWindowDays !== undefined ? { queryHistoryWindowDays: args.queryHistoryWindowDays } : {}),
|
||||
...(args.scanMode ? { scanMode: args.scanMode } : {}),
|
||||
...(args.detectRelationships !== undefined ? { detectRelationships: args.detectRelationships } : {}),
|
||||
...(args.cliVersion ? { cliVersion: args.cliVersion } : {}),
|
||||
...(args.runtimeInstallPolicy ? { runtimeInstallPolicy: args.runtimeInstallPolicy } : {}),
|
||||
},
|
||||
io,
|
||||
);
|
||||
return result.exitCode;
|
||||
io,
|
||||
redactionSecrets: await collectTelemetryRedactionSecrets({
|
||||
project,
|
||||
projectDir: args.projectDir,
|
||||
connectionId: args.targetConnectionId,
|
||||
includeLlm: true,
|
||||
includeEmbeddings: true,
|
||||
env: deps.env ?? process.env,
|
||||
}),
|
||||
});
|
||||
io.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
const plan = buildPublicIngestPlan(project, args);
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import type { KtxProgressPort, KtxScanMode, KtxScanReport, KtxScanWarning } from './context/scan/types.js';
|
||||
import { runLocalScan } from './context/scan/local-scan.js';
|
||||
import { loadKtxProject } from './context/project/project.js';
|
||||
import { loadKtxProject, type KtxLocalProject } from './context/project/project.js';
|
||||
import { getKtxCliPackageInfo } from './cli-runtime.js';
|
||||
import { resolveProjectEmbeddingProvider } from './embedding-resolution.js';
|
||||
import type { KtxCliIo } from './index.js';
|
||||
|
|
@ -8,7 +8,8 @@ import { createKtxCliLocalIngestAdapters } from './local-adapters.js';
|
|||
import { createKtxCliScanConnector } from './local-scan-connectors.js';
|
||||
import type { KtxManagedPythonInstallPolicy } from './managed-python-command.js';
|
||||
import { profileMark } from './startup-profile.js';
|
||||
import { emitTelemetryEvent } from './telemetry/index.js';
|
||||
import { emitTelemetryEvent, reportException } from './telemetry/index.js';
|
||||
import { collectTelemetryRedactionSecrets } from './telemetry/redaction-secrets.js';
|
||||
import { formatErrorDetail, scrubErrorClass } from './telemetry/scrubber.js';
|
||||
|
||||
profileMark('module:scan');
|
||||
|
|
@ -322,8 +323,9 @@ export function createCliScanProgress(
|
|||
|
||||
export async function runKtxScan(args: KtxScanArgs, io: KtxCliIo = process, deps: KtxScanDeps = {}): Promise<number> {
|
||||
const startedAt = performance.now();
|
||||
let project: KtxLocalProject | undefined;
|
||||
try {
|
||||
const project = await loadKtxProject({ projectDir: args.projectDir });
|
||||
project = await loadKtxProject({ projectDir: args.projectDir });
|
||||
const resolveEmbeddingProvider = deps.resolveEmbeddingProvider ?? resolveProjectEmbeddingProvider;
|
||||
const resolution = await resolveEmbeddingProvider(project, {
|
||||
mode: 'ensure',
|
||||
|
|
@ -397,6 +399,20 @@ export async function runKtxScan(args: KtxScanArgs, io: KtxCliIo = process, deps
|
|||
...(errorDetail ? { errorDetail } : {}),
|
||||
},
|
||||
});
|
||||
await reportException({
|
||||
error,
|
||||
context: { source: 'scan run', handled: true, fatal: false },
|
||||
projectDir: args.projectDir,
|
||||
io,
|
||||
redactionSecrets: await collectTelemetryRedactionSecrets({
|
||||
project,
|
||||
projectDir: args.projectDir,
|
||||
connectionId: args.connectionId,
|
||||
includeLlm: true,
|
||||
includeEmbeddings: true,
|
||||
env: process.env,
|
||||
}),
|
||||
});
|
||||
io.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
|
||||
return 1;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,7 +26,8 @@ import {
|
|||
type KtxManagedPythonInstallPolicy,
|
||||
} from './managed-python-command.js';
|
||||
import { profileMark } from './startup-profile.js';
|
||||
import { emitTelemetryEvent } from './telemetry/index.js';
|
||||
import { emitTelemetryEvent, reportException } from './telemetry/index.js';
|
||||
import { collectTelemetryRedactionSecrets } from './telemetry/redaction-secrets.js';
|
||||
import { scrubErrorClass } from './telemetry/scrubber.js';
|
||||
|
||||
profileMark('module:sl');
|
||||
|
|
@ -202,8 +203,9 @@ function ambiguousSourceMessage(sourceName: string, connectionIds: readonly stri
|
|||
export async function runKtxSl(args: KtxSlArgs, io: KtxSlIo = process, deps: KtxSlDeps = {}): Promise<number> {
|
||||
const startedAt = performance.now();
|
||||
let queryForTelemetry: SemanticLayerQueryInput | undefined;
|
||||
let project: KtxLocalProject | undefined;
|
||||
try {
|
||||
const project = await (deps.loadProject ?? loadKtxProject)({ projectDir: args.projectDir });
|
||||
project = await (deps.loadProject ?? loadKtxProject)({ projectDir: args.projectDir });
|
||||
if (args.command === 'list') {
|
||||
const sources = await listLocalSlSources(project, { connectionId: args.connectionId });
|
||||
await printSlSources({
|
||||
|
|
@ -320,7 +322,7 @@ export async function runKtxSl(args: KtxSlArgs, io: KtxSlIo = process, deps: Ktx
|
|||
projectDir: args.projectDir,
|
||||
});
|
||||
const queryExecutor = args.execute ? (deps.createQueryExecutor ?? createDefaultLocalQueryExecutor)() : undefined;
|
||||
const result = await compileLocalSlQuery(project as KtxLocalProject, {
|
||||
const result = await compileLocalSlQuery(project, {
|
||||
connectionId: args.connectionId,
|
||||
query,
|
||||
compute,
|
||||
|
|
@ -351,6 +353,20 @@ export async function runKtxSl(args: KtxSlArgs, io: KtxSlIo = process, deps: Ktx
|
|||
const _exhaustive: never = args;
|
||||
throw new Error(`Unsupported sl command: ${JSON.stringify(_exhaustive)}`);
|
||||
} catch (error) {
|
||||
await reportException({
|
||||
error,
|
||||
context: { source: `sl ${args.command}`, handled: true, fatal: false },
|
||||
projectDir: args.projectDir,
|
||||
io,
|
||||
redactionSecrets: await collectTelemetryRedactionSecrets({
|
||||
project,
|
||||
projectDir: args.projectDir,
|
||||
connectionId: args.connectionId,
|
||||
includeLlm: args.command === 'query',
|
||||
includeEmbeddings: args.command === 'search' || args.command === 'query',
|
||||
env: process.env,
|
||||
}),
|
||||
});
|
||||
if (args.command === 'validate') {
|
||||
const errorClass = scrubErrorClass(error);
|
||||
await emitTelemetryEvent({
|
||||
|
|
|
|||
|
|
@ -7,7 +7,8 @@ import { createKtxCliScanConnector } from './local-scan-connectors.js';
|
|||
import { createManagedDaemonSqlAnalysisPort } from './managed-python-http.js';
|
||||
import { profileMark } from './startup-profile.js';
|
||||
import { isDemoConnection } from './telemetry/demo-detect.js';
|
||||
import { emitTelemetryEvent } from './telemetry/index.js';
|
||||
import { emitTelemetryEvent, reportException } from './telemetry/index.js';
|
||||
import { collectTelemetryRedactionSecrets } from './telemetry/redaction-secrets.js';
|
||||
import { scrubErrorClass } from './telemetry/scrubber.js';
|
||||
|
||||
profileMark('module:sql');
|
||||
|
|
@ -142,8 +143,9 @@ export async function runKtxSql(args: KtxSqlArgs, io: KtxCliIo = process, deps:
|
|||
const startedAt = performance.now();
|
||||
let driver = 'unknown';
|
||||
let demoConnection = false;
|
||||
let project: KtxLocalProject | undefined;
|
||||
try {
|
||||
const project = await (deps.loadProject ?? loadKtxProject)({ projectDir: args.projectDir });
|
||||
project = await (deps.loadProject ?? loadKtxProject)({ projectDir: args.projectDir });
|
||||
const connection = project.config.connections[args.connectionId];
|
||||
if (!connection) {
|
||||
throw new Error(`Connection "${args.connectionId}" is not configured in ktx.yaml`);
|
||||
|
|
@ -171,7 +173,7 @@ export async function runKtxSql(args: KtxSqlArgs, io: KtxCliIo = process, deps:
|
|||
const createScanConnector = deps.createScanConnector ?? createKtxCliScanConnector;
|
||||
let connector: KtxScanConnector | null = null;
|
||||
try {
|
||||
connector = await createScanConnector(project as KtxLocalProject, args.connectionId);
|
||||
connector = await createScanConnector(project, args.connectionId);
|
||||
if (!connector.capabilities.readOnlySql || !connector.executeReadOnly) {
|
||||
throw new Error(`Connection "${args.connectionId}" does not support read-only SQL execution.`);
|
||||
}
|
||||
|
|
@ -218,6 +220,20 @@ export async function runKtxSql(args: KtxSqlArgs, io: KtxCliIo = process, deps:
|
|||
...(errorClass ? { errorClass } : {}),
|
||||
},
|
||||
});
|
||||
await reportException({
|
||||
error,
|
||||
context: { source: 'sql run', handled: true, fatal: false },
|
||||
projectDir: args.projectDir,
|
||||
io,
|
||||
redactionSecrets: await collectTelemetryRedactionSecrets({
|
||||
project,
|
||||
projectDir: args.projectDir,
|
||||
connectionId: args.connectionId,
|
||||
includeLlm: false,
|
||||
includeEmbeddings: false,
|
||||
env: process.env,
|
||||
}),
|
||||
});
|
||||
io.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
|
||||
return 1;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,6 +16,16 @@ type PostHogClient = {
|
|||
properties: Record<string, unknown>;
|
||||
groups?: Record<string, string>;
|
||||
}): void;
|
||||
captureException(
|
||||
error: unknown,
|
||||
distinctId?: string,
|
||||
additionalProperties?: Record<string, unknown>,
|
||||
): void;
|
||||
captureExceptionImmediate(
|
||||
error: unknown,
|
||||
distinctId?: string,
|
||||
additionalProperties?: Record<string, unknown>,
|
||||
): Promise<void>;
|
||||
shutdown(): Promise<void> | void;
|
||||
};
|
||||
|
||||
|
|
@ -105,6 +115,57 @@ export async function trackTelemetryEvent(input: {
|
|||
}
|
||||
}
|
||||
|
||||
function writeDebugExceptionPayload(input: {
|
||||
error: Error;
|
||||
distinctId: string;
|
||||
properties: Record<string, unknown>;
|
||||
stderr: TelemetrySink;
|
||||
}): void {
|
||||
input.stderr.write(
|
||||
`[telemetry-exception] ${JSON.stringify({
|
||||
distinctId: input.distinctId,
|
||||
message: input.error.message,
|
||||
name: input.error.name,
|
||||
properties: input.properties,
|
||||
})}\n`,
|
||||
);
|
||||
}
|
||||
|
||||
export async function trackTelemetryException(input: {
|
||||
error: Error;
|
||||
distinctId: string;
|
||||
properties: Record<string, unknown>;
|
||||
env?: TelemetryEmitterEnv;
|
||||
stderr: TelemetrySink;
|
||||
projectApiKey?: string;
|
||||
host?: string;
|
||||
immediate?: boolean;
|
||||
}): Promise<void> {
|
||||
const env = input.env ?? process.env;
|
||||
|
||||
if (debugEnabled(env)) {
|
||||
writeDebugExceptionPayload(input);
|
||||
return;
|
||||
}
|
||||
|
||||
const projectApiKey = telemetryProjectApiKey(input.projectApiKey);
|
||||
const host = telemetryHost(env, input.host);
|
||||
const client = await getPostHogClient(projectApiKey, host);
|
||||
if (!client) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
if (input.immediate) {
|
||||
await client.captureExceptionImmediate(input.error, input.distinctId, input.properties);
|
||||
return;
|
||||
}
|
||||
client.captureException(input.error, input.distinctId, input.properties);
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
export async function shutdownTelemetryEmitter(): Promise<void> {
|
||||
const client = await clientPromise;
|
||||
if (!client) {
|
||||
|
|
|
|||
201
packages/cli/src/telemetry/exception.ts
Normal file
201
packages/cli/src/telemetry/exception.ts
Normal file
|
|
@ -0,0 +1,201 @@
|
|||
import { inspect } from 'node:util';
|
||||
|
||||
import { getKtxCliPackageInfo, type KtxCliIo, type KtxCliPackageInfo } from '../cli-runtime.js';
|
||||
import { buildCommonEnvelope } from './events.js';
|
||||
import { trackTelemetryException } from './emitter.js';
|
||||
import { computeTelemetryProjectId, loadTelemetryIdentity } from './identity.js';
|
||||
|
||||
export interface ExceptionContext {
|
||||
source: string;
|
||||
handled: boolean;
|
||||
fatal: boolean;
|
||||
extra?: Record<string, string | number | boolean>;
|
||||
}
|
||||
|
||||
type AnyObject = object;
|
||||
|
||||
const reportedObjects = new WeakSet<AnyObject>();
|
||||
const recentHandledPrimitives: string[] = [];
|
||||
const RECENT_PRIMITIVE_LIMIT = 128;
|
||||
|
||||
function primitiveKey(value: unknown): string {
|
||||
return `${typeof value}:${String(value)}`;
|
||||
}
|
||||
|
||||
function rememberHandledPrimitive(value: unknown): void {
|
||||
recentHandledPrimitives.push(primitiveKey(value));
|
||||
if (recentHandledPrimitives.length > RECENT_PRIMITIVE_LIMIT) {
|
||||
recentHandledPrimitives.splice(0, recentHandledPrimitives.length - RECENT_PRIMITIVE_LIMIT);
|
||||
}
|
||||
}
|
||||
|
||||
function consumeHandledPrimitive(value: unknown): boolean {
|
||||
const key = primitiveKey(value);
|
||||
const index = recentHandledPrimitives.indexOf(key);
|
||||
if (index < 0) {
|
||||
return false;
|
||||
}
|
||||
recentHandledPrimitives.splice(index, 1);
|
||||
return true;
|
||||
}
|
||||
|
||||
function shouldSkipAsAlreadyReported(error: unknown, handled: boolean): boolean {
|
||||
if ((typeof error === 'object' || typeof error === 'function') && error !== null) {
|
||||
if (reportedObjects.has(error)) {
|
||||
return true;
|
||||
}
|
||||
reportedObjects.add(error);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (handled) {
|
||||
rememberHandledPrimitive(error);
|
||||
return false;
|
||||
}
|
||||
|
||||
return consumeHandledPrimitive(error);
|
||||
}
|
||||
|
||||
function escapeRegExp(value: string): string {
|
||||
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||
}
|
||||
|
||||
function redactStaticPatterns(value: string): string {
|
||||
return value
|
||||
.replace(/([a-z][a-z0-9+.-]*:\/\/[^:\s/@]+:)([^@\s/]+)(@)/gi, '$1[redacted]$3')
|
||||
.replace(/\b(password|pwd)=([^;&\s]+)/gi, '$1=[redacted]')
|
||||
.replace(/\bAuthorization\s*:\s*[^\r\n,;]+/gi, 'Authorization: [redacted]')
|
||||
.replace(/\bBearer\s+[A-Za-z0-9._~+/=-]+/gi, 'Bearer [redacted]')
|
||||
.replace(/\b(api[_-]?key)\s*[:=]\s*([^\s,;]+)/gi, '$1=[redacted]')
|
||||
.replace(/\b(KTX_[A-Z0-9_]*|[A-Z0-9_]*(?:TOKEN|SECRET))\s*[:=]\s*([^\s,;]+)/g, '$1=[redacted]')
|
||||
.replace(/([?&](?:X-Amz-Signature|X-Goog-Signature|sig)=)[^&\s]+/gi, '$1[redacted]');
|
||||
}
|
||||
|
||||
function redactText(value: string, secrets: ReadonlyArray<string>): string {
|
||||
let redacted = value;
|
||||
for (const secret of secrets) {
|
||||
if (secret) {
|
||||
redacted = redacted.replace(new RegExp(escapeRegExp(secret), 'g'), '[redacted]');
|
||||
}
|
||||
}
|
||||
return redactStaticPatterns(redacted);
|
||||
}
|
||||
|
||||
const FORBIDDEN_EXTRA_PROPERTY_KEYS = new Set([
|
||||
'argv',
|
||||
'args',
|
||||
'env',
|
||||
'environment',
|
||||
'sql',
|
||||
'query',
|
||||
'prompt',
|
||||
'mcparguments',
|
||||
'mcpargs',
|
||||
'tablename',
|
||||
'schemaname',
|
||||
'columnname',
|
||||
'databaseurl',
|
||||
'connectionstring',
|
||||
'url',
|
||||
'password',
|
||||
'token',
|
||||
'apikey',
|
||||
'api_key',
|
||||
'authorization',
|
||||
]);
|
||||
|
||||
function safeExtraProperties(
|
||||
extra: Record<string, string | number | boolean> | undefined,
|
||||
): Record<string, string | number | boolean> {
|
||||
const safe: Record<string, string | number | boolean> = {};
|
||||
for (const [key, value] of Object.entries(extra ?? {})) {
|
||||
if (!FORBIDDEN_EXTRA_PROPERTY_KEYS.has(key.replace(/[^a-z0-9_]/gi, '').toLowerCase())) {
|
||||
safe[key] = value;
|
||||
}
|
||||
}
|
||||
return safe;
|
||||
}
|
||||
|
||||
function toMessage(error: unknown): string {
|
||||
if (error instanceof Error) {
|
||||
return error.message;
|
||||
}
|
||||
if (typeof error === 'string') {
|
||||
return error;
|
||||
}
|
||||
return inspect(error, { depth: 4, breakLength: 120 });
|
||||
}
|
||||
|
||||
function sanitizedError(error: unknown, secrets: ReadonlyArray<string>): Error {
|
||||
if (error instanceof Error) {
|
||||
const cause = 'cause' in error ? (error as Error & { cause?: unknown }).cause : undefined;
|
||||
const clone = new Error(redactText(error.message, secrets), {
|
||||
...(cause !== undefined ? { cause: sanitizedError(cause, secrets) } : {}),
|
||||
});
|
||||
clone.name = error.name;
|
||||
if (error.stack) {
|
||||
clone.stack = redactText(error.stack, secrets);
|
||||
}
|
||||
return clone;
|
||||
}
|
||||
return new Error(redactText(toMessage(error), secrets));
|
||||
}
|
||||
|
||||
export async function reportException(input: {
|
||||
error: unknown;
|
||||
context: ExceptionContext;
|
||||
io: KtxCliIo;
|
||||
packageInfo?: KtxCliPackageInfo;
|
||||
projectDir?: string;
|
||||
immediate?: boolean;
|
||||
redactionSecrets?: ReadonlyArray<string>;
|
||||
}): Promise<void> {
|
||||
try {
|
||||
if (shouldSkipAsAlreadyReported(input.error, input.context.handled)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const debug = process.env.KTX_TELEMETRY_DEBUG === '1';
|
||||
const identity = await loadTelemetryIdentity({
|
||||
stderr: input.io.stderr,
|
||||
env: process.env,
|
||||
});
|
||||
|
||||
if ((!identity.enabled || !identity.installId) && !debug) {
|
||||
return;
|
||||
}
|
||||
|
||||
const packageInfo = input.packageInfo ?? getKtxCliPackageInfo();
|
||||
const installId = identity.installId ?? 'debug';
|
||||
const projectId = input.projectDir ? computeTelemetryProjectId(installId, input.projectDir) : undefined;
|
||||
const safeError = sanitizedError(input.error, input.redactionSecrets ?? []);
|
||||
const properties: Record<string, unknown> = {
|
||||
...buildCommonEnvelope({
|
||||
cliVersion: packageInfo.version,
|
||||
isCi: Boolean(process.env.CI),
|
||||
}),
|
||||
source: input.context.source,
|
||||
handled: input.context.handled,
|
||||
fatal: input.context.fatal,
|
||||
...(projectId ? { projectId } : {}),
|
||||
...safeExtraProperties(input.context.extra),
|
||||
};
|
||||
|
||||
delete properties.$groups;
|
||||
await trackTelemetryException({
|
||||
error: safeError,
|
||||
distinctId: installId,
|
||||
properties,
|
||||
env: process.env,
|
||||
stderr: input.io.stderr,
|
||||
immediate: input.immediate,
|
||||
});
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export function __resetTelemetryExceptionStateForTests(): void {
|
||||
recentHandledPrimitives.length = 0;
|
||||
}
|
||||
|
|
@ -7,6 +7,7 @@ import {
|
|||
type CompletedCommandSpan,
|
||||
} from './command-hook.js';
|
||||
import { shutdownTelemetryEmitter, trackTelemetryEvent } from './emitter.js';
|
||||
import { reportException, type ExceptionContext } from './exception.js';
|
||||
import {
|
||||
buildCommonEnvelope,
|
||||
buildTelemetryEvent,
|
||||
|
|
@ -17,8 +18,8 @@ import {
|
|||
import { computeTelemetryProjectId, loadTelemetryIdentity } from './identity.js';
|
||||
import { buildProjectStackSnapshotFields } from './project-snapshot.js';
|
||||
|
||||
export { beginCommandSpan, completeCommandSpan, shutdownTelemetryEmitter };
|
||||
export type { CommandOutcome, CompletedCommandSpan };
|
||||
export { beginCommandSpan, completeCommandSpan, reportException, shutdownTelemetryEmitter };
|
||||
export type { CommandOutcome, CompletedCommandSpan, ExceptionContext };
|
||||
|
||||
export async function showTelemetryNoticeIfNeeded(io: KtxCliIo, packageInfo: KtxCliPackageInfo): Promise<void> {
|
||||
const identity = await loadTelemetryIdentity({
|
||||
|
|
|
|||
117
packages/cli/src/telemetry/redaction-secrets.ts
Normal file
117
packages/cli/src/telemetry/redaction-secrets.ts
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
import { resolveKtxConfigReference } from '../context/core/config-reference.js';
|
||||
import { loadKtxProject, type KtxLocalProject } from '../context/project/project.js';
|
||||
|
||||
const SENSITIVE_KEY =
|
||||
/(password|secret|token|api[_-]?key|auth[_-]?token|auth_token_ref|private[_-]?key|passphrase|credential|authorization|url)$/i;
|
||||
|
||||
type TelemetryRedactionProject = Pick<KtxLocalProject, 'config' | 'projectDir'>;
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
||||
}
|
||||
|
||||
function addSecret(values: string[], value: string | undefined): void {
|
||||
const trimmed = value?.trim();
|
||||
if (trimmed && !values.includes(trimmed)) {
|
||||
values.push(trimmed);
|
||||
}
|
||||
}
|
||||
|
||||
function tryResolve(value: string, env: NodeJS.ProcessEnv): string | undefined {
|
||||
try {
|
||||
return resolveKtxConfigReference(value, env);
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
function addUrlCredentials(values: string[], value: string): void {
|
||||
try {
|
||||
const parsed = new URL(value);
|
||||
addSecret(values, parsed.password ? decodeURIComponent(parsed.password) : undefined);
|
||||
addSecret(values, parsed.username ? decodeURIComponent(parsed.username) : undefined);
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
function collectFromRecord(input: unknown, env: NodeJS.ProcessEnv, values: string[]): void {
|
||||
if (Array.isArray(input)) {
|
||||
for (const item of input) {
|
||||
collectFromRecord(item, env, values);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isRecord(input)) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const [key, raw] of Object.entries(input)) {
|
||||
if (isRecord(raw) || Array.isArray(raw)) {
|
||||
collectFromRecord(raw, env, values);
|
||||
continue;
|
||||
}
|
||||
if (typeof raw !== 'string' || !SENSITIVE_KEY.test(key)) {
|
||||
continue;
|
||||
}
|
||||
const resolved = tryResolve(raw, env);
|
||||
addSecret(values, resolved);
|
||||
if (resolved) {
|
||||
addUrlCredentials(values, resolved);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function collectLlmSecrets(project: TelemetryRedactionProject, env: NodeJS.ProcessEnv, values: string[]): void {
|
||||
collectFromRecord(project.config.llm.provider, env, values);
|
||||
}
|
||||
|
||||
function collectEmbeddingSecrets(project: TelemetryRedactionProject, env: NodeJS.ProcessEnv, values: string[]): void {
|
||||
collectFromRecord(project.config.ingest.embeddings, env, values);
|
||||
collectFromRecord(project.config.scan.enrichment.embeddings, env, values);
|
||||
}
|
||||
|
||||
function collectConnectionSecrets(
|
||||
project: TelemetryRedactionProject,
|
||||
connectionId: string | undefined,
|
||||
env: NodeJS.ProcessEnv,
|
||||
values: string[],
|
||||
): void {
|
||||
if (!connectionId) {
|
||||
return;
|
||||
}
|
||||
collectFromRecord(project.config.connections[connectionId], env, values);
|
||||
}
|
||||
|
||||
export async function collectTelemetryRedactionSecrets(input: {
|
||||
project?: TelemetryRedactionProject;
|
||||
projectDir?: string;
|
||||
connectionId?: string;
|
||||
includeLlm?: boolean;
|
||||
includeEmbeddings?: boolean;
|
||||
env?: NodeJS.ProcessEnv;
|
||||
}): Promise<string[]> {
|
||||
const env = input.env ?? process.env;
|
||||
let project = input.project;
|
||||
if (!project && input.projectDir) {
|
||||
try {
|
||||
project = await loadKtxProject({ projectDir: input.projectDir });
|
||||
} catch {
|
||||
project = undefined;
|
||||
}
|
||||
}
|
||||
if (!project) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const values: string[] = [];
|
||||
if (input.includeLlm) {
|
||||
collectLlmSecrets(project, env, values);
|
||||
}
|
||||
if (input.includeEmbeddings) {
|
||||
collectEmbeddingSecrets(project, env, values);
|
||||
}
|
||||
collectConnectionSecrets(project, input.connectionId, env, values);
|
||||
return values;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue