feat: preserve safe request and exception diagnostics

This commit is contained in:
Thomas Durieux committed 2026-11-14 21:22:28 +00:00
1 parent 3fbf2cbad7
commit f6574069da
20 files changed
+3187 -65

No files matched your search

+1 -1
View File
@@ -1,7 +1,7 @@
import { AsyncLocalStorage } from "async_hooks";
import { randomUUID } from "crypto";
export interface LogContext { requestId?: string; jobId?: string; attempt?: number; service?: string }
export interface LogContext { requestId?: string; jobId?: string; attempt?: number; service?: string; route?: string; method?: string }
export const logContext = new AsyncLocalStorage<LogContext>();
export const validTraceId = (value: unknown): value is string => typeof value === "string" &&
/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/.test(value);
File diff suppressed because it is too large. Load diff
+74
View File
@@ -0,0 +1,74 @@
import { resolve, relative, sep } from "path";
import { LOG_ERROR_TYPES, LOG_SOURCE_FUNCTIONS } from "./log-diagnostic-schema";
// Node 24 exposes Error.stack through a shared native accessor; never call user getters.
const nativeStackGetter = Object.getOwnPropertyDescriptor(new Error(), "stack")?.get;
const root = resolve(__dirname, "..");
function data(value: unknown, key: string, inherited = false): unknown {
try {
let object = value;
for (let depth = 0; object && typeof object === "object" && depth < (inherited ? 4 : 1); depth++) {
const descriptor = Object.getOwnPropertyDescriptor(object, key);
if (descriptor) {
if ("value" in descriptor) return descriptor.value;
if (key === "stack" && nativeStackGetter && descriptor.get === nativeStackGetter) return nativeStackGetter.call(object);
return undefined;
}
object = Object.getPrototypeOf(object);
}
} catch { /* Logging must not invoke hostile getters or fail with a Proxy. */ }
}
function frame(file: string, fn: string, line: string, column: string): string | null {
if (!LOG_SOURCE_FUNCTIONS[file]?.includes(fn) || !/^[1-9]\d{0,5}$/.test(line) || !/^[1-9]\d{0,4}$/.test(column)) return null;
return `at ${fn} (${file}:${line}:${column})`;
}
/** Validate already-projected diagnostics again at every storage/API boundary. */
export function safeDiagnosticFields(value: unknown): Record<string, string> {
const out: Record<string,string> = {};
for (const key of ["errorType", "causeType"]) {
const type = data(value,key);
if (typeof type === "string" && LOG_ERROR_TYPES.includes(type)) out[key] = type;
}
for (const key of ["errorStack", "causeStack"]) {
const stack = data(value,key);
if (typeof stack !== "string" || stack.length > 1024) continue;
const frames = stack.split("\n").slice(0,8).flatMap(line => {
const match = /^at ([A-Za-z_$][A-Za-z0-9_$]*|<anonymous>) \(((?:src|build)\/[A-Za-z0-9_/-]+\.(?:ts|js)):([1-9]\d{0,5}):([1-9]\d{0,4})\)$/.exec(line);
const safe = match && frame(match[2],match[1],match[3],match[4]);
return safe ? [safe] : [];
});
if (frames.length) out[key] = frames.join("\n");
}
return out;
}
/** Discard exception messages, absolute roots, external frames and unknown function names. */
export function errorDiagnostics(error: unknown): Record<string,string> {
if (!error || typeof error !== "object") return {};
const out: Record<string,string> = {};
const seen = new Set<unknown>();
let current: unknown = error;
for(let depth=0; current && typeof current === "object" && depth<3 && !seen.has(current); depth++) {
seen.add(current);
const name = data(current,"name",true);
const type = typeof name === "string" && LOG_ERROR_TYPES.includes(name) ? name : "UnknownError";
const stack = data(current,"stack");
const frames: string[] = [];
if(typeof stack === "string") for(const line of stack.slice(0,16384).split("\n").slice(1,33)) {
const match = /^\s*at (?:(?:async )?([^()]+) \()?([^()\s]+):(\d+):(\d+)\)?$/.exec(line);
if(!match) continue;
const local = relative(root,match[2]).split(sep).join("/");
if(local.startsWith("../") || local.includes(":")) continue;
const file = (root.endsWith(sep+"build") ? "build/" : "src/")+local;
const candidate = (match[1] || "").trim().split(".").at(-1) || "<anonymous>";
const fn = LOG_SOURCE_FUNCTIONS[file]?.includes(candidate) ? candidate : "<anonymous>";
const safe = frame(file,fn,match[3],match[4]);
if(safe && frames.join("\n").length + safe.length + 1 <= 1024) frames.push(safe);
if(frames.length===8) break;
}
if (depth) delete out.causeStack;
out[depth ? "causeType" : "errorType"] = type;
if(frames.length) out[depth ? "causeStack" : "errorStack"] = frames.join("\n");
current = data(current,"cause");
}
return out;
}
+11 -3
View File
@@ -1,3 +1,5 @@
import { safeDiagnosticFields, errorDiagnostics } from "./log-diagnostics";
import { LOG_ROUTES } from "./log-diagnostic-schema";
import { LOG_MODULES, ERROR_CODES, LOG_EVENTS } from "./log-schema";
import { randomUUID } from "crypto";
import { logContext, validTraceId } from "./log-context";
@@ -28,7 +30,7 @@ export function logRecord(level: string, module: string, args: unknown[]) {
const context = logContext.getStore() || {};
const details: Record<string, unknown> = {};
const keys = [...numbers, "requestId", "jobId", "code", "message", "outcome", "method", "route", "queue",
"stages", "reason", "phase", "event", "deliveryId", "hostname", "service"];
"errorType", "errorStack", "causeType", "causeStack", "stages", "reason", "phase", "event", "deliveryId", "hostname", "service"];
// Do not traverse arbitrary objects or invoke error getters while logging.
for (const arg of args.slice(0, 8)) if (arg && typeof arg === "object") {
for (const key of keys) {
@@ -38,6 +40,9 @@ export function logRecord(level: string, module: string, args: unknown[]) {
} catch { /* A hostile Proxy must not make logging throw. */ }
}
}
for (const arg of args.slice(0,8)) {
try { if (arg instanceof Error) Object.assign(details, errorDiagnostics(arg)); } catch { /* hostile Proxy */ }
}
const first = typeof args[0] === "string" && args[0].length < 256 ? args[0] : "";
const event = (Object.hasOwn(events, first) ? events[first] : undefined) || (module.includes("webhook") ? "webhook_log" : "application_log");
const record: Record<string, unknown> = { timestamp: new Date().toISOString(), severity: level,
@@ -60,8 +65,11 @@ export function logRecord(level: string, module: string, args: unknown[]) {
else if (details.hostname) record.upstreamHost = "other";
if (allowed(["completed", "interrupted"], details.outcome)) record.outcome = details.outcome;
if (allowed(["GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "OTHER"], details.method)) record.method = details.method;
// Route labels are generated by request-monitoring, never from URLs in arbitrary errors.
if (module === "requests" && typeof details.route === "string") record.route = details.route;
// Only fixed route labels survive; never raw URLs or request parameters.
const route = context.route || details.route;
if (typeof route === "string" && LOG_ROUTES.includes(route)) record.route = route;
if (context.method && allowed(["GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "OTHER"], context.method)) record.method = context.method;
Object.assign(record, safeDiagnosticFields(details));
if (allowed(["download", "remove", "cache"], details.queue)) record.queue = details.queue;
if (details.stages && typeof details.stages === "object") for (const key of stages) {
let value: unknown;
+2 -1
View File
@@ -1,3 +1,4 @@
import { errorDiagnostics } from "./log-diagnostics";
import { logRecord } from "./log-record";
import { redactSecrets } from "./redact-secrets";
import { createClient, RedisClientType } from "redis";
@@ -307,7 +308,7 @@ export function serializeError(err: unknown, seen = new WeakSet<object>(), depth
if (seen.has(err) || depth >= 4) return { message: "error_details_truncated" };
seen.add(err);
const e = err as ErrorLike;
const out: Record<string, unknown> = {};
const out: Record<string, unknown> = errorDiagnostics(err);
if (e.name) out.name = e.name;
if (e.message) out.message = e.message;
+1 -1
View File
@@ -165,6 +165,6 @@ export function monitorRequests(service: Service, metrics = requestMetrics,
else if (!route.includes("healthcheck")) log.info("request", detail);
};
res.once("finish", done); res.once("close", done);
context.run(trace, () => logContext.run({ requestId: id, service }, next));
context.run(trace, () => logContext.run({ requestId: id, service, route, method }, next));
};
}