diff --git a/clients/php/ErrorReporter.php b/clients/php/ErrorReporter.php index ee78077..4ebef56 100644 --- a/clients/php/ErrorReporter.php +++ b/clients/php/ErrorReporter.php @@ -69,12 +69,17 @@ final class ErrorReporter if (!in_array($err['type'], $fatal, true)) { return; } + $file = (string) ($err['file'] ?? ''); + $line = (int) ($err['line'] ?? 0); + $syn = $file !== '' && $line > 0 + ? "[fatal shutdown] {$file}:{$line}" + : '[fatal shutdown] (location unknown)'; self::reportMessage( (string) $err['message'], 'critical', - (string) ($err['file'] ?? ''), - (int) ($err['line'] ?? 0), - '', + $file, + $line, + $syn, ['shutdown' => true, 'errno' => $err['type']] ); }); diff --git a/ingest-api/src/buildApp.ts b/ingest-api/src/buildApp.ts index d647bb9..9e353db 100644 --- a/ingest-api/src/buildApp.ts +++ b/ingest-api/src/buildApp.ts @@ -28,6 +28,7 @@ const _ingestEventSchema = { message: { type: "string", minLength: 1, maxLength: 20000 }, fingerprint: { type: "string", minLength: 1, maxLength: 128 }, stacktrace: { type: "string", minLength: 1, maxLength: 500000 }, + stack_trace: { type: "string", minLength: 1, maxLength: 500000 }, context: { type: "object", additionalProperties: true }, }, } as const; diff --git a/ingest-api/src/lib/ingestStacktrace.test.ts b/ingest-api/src/lib/ingestStacktrace.test.ts new file mode 100644 index 0000000..0894c1d --- /dev/null +++ b/ingest-api/src/lib/ingestStacktrace.test.ts @@ -0,0 +1,42 @@ +import { describe, expect, it } from "vitest"; +import { clientProvidedStack, synthesizeStackTraceForOccurrence } from "./ingestStacktrace.js"; + +describe("ingestStacktrace", () => { + it("prefers stacktrace over stack_trace", () => { + expect( + clientProvidedStack({ + stacktrace: " a ", + stack_trace: "b", + }), + ).toBe("a"); + }); + + it("falls back to stack_trace", () => { + expect( + clientProvidedStack({ + stack_trace: " trace ", + }), + ).toBe("trace"); + }); + + it("returns null when both empty or missing", () => { + expect(clientProvidedStack({})).toBeNull(); + expect(clientProvidedStack({ stacktrace: " " })).toBeNull(); + expect(clientProvidedStack({ stack_trace: "" })).toBeNull(); + }); + + it("synthesizes from file and line in context", () => { + const s = synthesizeStackTraceForOccurrence("boom\nline2", { + file_path: "/app/x.php", + line_number: 42, + }); + expect(s).toContain("[synthesized:"); + expect(s).toContain("/app/x.php:42"); + expect(s).toContain("boom"); + expect(s).not.toContain("line2"); + }); + + it("synthesizes with message only when no location", () => { + expect(synthesizeStackTraceForOccurrence("only msg", null)).toContain("only msg"); + }); +}); diff --git a/ingest-api/src/lib/ingestStacktrace.ts b/ingest-api/src/lib/ingestStacktrace.ts new file mode 100644 index 0000000..bde7ed2 --- /dev/null +++ b/ingest-api/src/lib/ingestStacktrace.ts @@ -0,0 +1,44 @@ +/** + * Resolve stack text for ingest: accept CRM-style `stack_trace` alias, and when + * clients omit stacks entirely, synthesize a non-empty line from context so + * recent-errors / dedupe tooling always have a stable string (BIT-796). + * + * Fingerprinting uses only client-provided stacks (not synthesized), so + * grouping stays stable for payloads that never sent a stack. + */ + +export function clientProvidedStack(ev: { + stacktrace?: string; + stack_trace?: string; +}): string | null { + const a = typeof ev.stacktrace === "string" ? ev.stacktrace.trim() : ""; + if (a !== "") { + return a; + } + const b = typeof ev.stack_trace === "string" ? ev.stack_trace.trim() : ""; + if (b !== "") { + return b; + } + return null; +} + +const _SYNTH_HEAD = "[synthesized: no stack trace in ingest payload]"; + +export function synthesizeStackTraceForOccurrence( + message: string, + context: Record | null, +): string { + const firstLine = message.split(/\r?\n/)[0]?.trim() ?? message; + if (!context) { + return `${_SYNTH_HEAD}\n${firstLine}`; + } + const fp = String(context.file_path ?? context.filename ?? context.file ?? "").trim(); + const ln = String(context.line_number ?? context.line ?? context.lineno ?? "").trim(); + if (fp !== "" && ln !== "") { + return `${_SYNTH_HEAD}\n#0 ${fp}:${ln}: ${firstLine}`; + } + if (fp !== "") { + return `${_SYNTH_HEAD}\n#0 ${fp}: ${firstLine}`; + } + return `${_SYNTH_HEAD}\n${firstLine}`; +} diff --git a/ingest-api/src/services/processIngestBatch.ts b/ingest-api/src/services/processIngestBatch.ts index 0778ce2..324454f 100644 --- a/ingest-api/src/services/processIngestBatch.ts +++ b/ingest-api/src/services/processIngestBatch.ts @@ -12,6 +12,10 @@ import { parseOccurredAt, } from "../lib/normalize.js"; import { evaluateNoise, NOISE_RULESET_VERSION } from "../lib/noisePolicy.js"; +import { + clientProvidedStack, + synthesizeStackTraceForOccurrence, +} from "../lib/ingestStacktrace.js"; export type IngestEventInput = { schema_version: number; @@ -20,6 +24,8 @@ export type IngestEventInput = { message: string; fingerprint?: string; stacktrace?: string; + /** CRM / snake_case alias for `stacktrace` (same fingerprint rules as stacktrace). */ + stack_trace?: string; context?: Record; }; @@ -178,14 +184,16 @@ async function _ingestOneEvent( fingerprint_policy_version: FINGERPRINT_POLICY_VERSION, }; } + const _clientStack = clientProvidedStack(_ev); const _fingerprint = computeFingerprint({ clientFingerprint: _ev.fingerprint, message: _message, - stacktrace: _ev.stacktrace, + ...(_clientStack !== null ? { stacktrace: _clientStack } : {}), context: _context, }); const _title = incidentTitleFromMessage(_message); - const _stack = typeof _ev.stacktrace === "string" ? _ev.stacktrace : null; + const _stack = + _clientStack !== null ? _clientStack : synthesizeStackTraceForOccurrence(_message, _context); await client.execute( `INSERT INTO er_incidents (