defects.ts
This file
Path
packages/telemetry/src/defects.ts
148 lines · 4.2 KiB · TypeScript
Last changed by
Merge observability into mainoscabrielbefore adoption1985111
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149// Crash reporting. A defect is the platform's own fault, so it is logged at// Error, with its cause and stack, where Issues groups it. A typed failure is// never reported: a refusal is the product working (AGENTS.md principle 3),// and a port's error is handled where it is typed. Where a defect's text may// quote what no log may hold, it is kept by its name and frames alone.import { Array, Cause, Context, DateTime, Effect, ErrorReporter, Logger, Predicate,} from "effect";
const NANOS_PER_MILLI = 1_000_000n;
class ReportedElsewhere extends Context.Service<ReportedElsewhere, true>()( "telemetry/ReportedElsewhere") {}
/** * What a report keeps of a defect: all of it, scrubbed, or its name and the * frames of its stack alone, where its text may quote what no log may hold * (a layer's repo, a held-back check's path). */export type DefectKeeping = "whole" | "name";
const WITHHELD = "<withheld>";
const FRAME = /^\s+at /u;
const withheld = (reason: Cause.Die): Cause.Die => { const [error] = Cause.prettyErrors(Cause.fromReasons([reason])); const name = error?.name ?? "Error"; const kept = new Error(WITHHELD);
kept.name = name; // The frames Effect adds for its spans included; the message, and any // cause the defect carries, left out. kept.stack = [ `${name}: ${WITHHELD}`, ...(error?.stack ?? "").split("\n").filter((line) => FRAME.test(line)), ].join("\n");
return Cause.makeDieReason(kept);};
/** `cause` with each defect kept by its name and stack frames alone. */export const unquoted = <E>(cause: Cause.Cause<E>): Cause.Cause<E> => Cause.fromReasons( cause.reasons.map((reason) => Cause.isDieReason(reason) ? withheld(reason) : reason ) );
/** * Reports each defect once, through the reporting fiber's own loggers and * annotations, however many boundaries it crosses. */export const defectReporter = ( keeping: DefectKeeping = "whole"): ErrorReporter.ErrorReporter => { const seen = new WeakSet<object>();
const unreported = <E>(reason: Cause.Reason<E>): readonly Cause.Die[] => { if ( !Cause.isDieReason(reason) || ErrorReporter.isIgnored(reason.defect) || Context.getOrUndefined( Cause.reasonAnnotations(reason), ReportedElsewhere ) === true ) { return []; }
if (Predicate.isObject(reason.defect)) { if (seen.has(reason.defect)) { return []; }
seen.add(reason.defect); }
return [reason]; };
return { [ErrorReporter.TypeId]: ErrorReporter.TypeId, report: ({ cause, fiber, timestamp }) => { const defects = cause.reasons.flatMap(unreported);
if (!Array.isReadonlyArrayNonEmpty(defects)) { return; }
const whole = Cause.fromReasons(defects); const reported = keeping === "name" ? unquoted(whole) : whole; const [first] = Cause.prettyErrors(reported);
const date = DateTime.toDateUtc( DateTime.makeUnsafe(Number(timestamp / NANOS_PER_MILLI)) );
for (const logger of fiber.getRef(Logger.CurrentLoggers)) { logger.log({ cause: reported, date, fiber, logLevel: "Error", message: [ first === undefined ? "a defect" : `${first.name}: ${first.message}`, ], }); } }, };};
/** * Marks the defects of a cause another side reported already, with the stack * only it held: one that comes back over a wire (a Durable Object's reply) is * reported there, never again here. The mark rides on the cause, so the * defect itself stays as it came. */export const reportedElsewhere = <E>(cause: Cause.Cause<E>): Cause.Cause<E> => Cause.annotate(cause, Context.make(ReportedElsewhere, true));
/** * A reporting boundary: reports the defects `effect` dies with, then marks * them, so a boundary further out lets them pass. Its place is where the * invocation's ids annotate the logs, so the report carries them. */export const reportDefects = <A, E, R>( effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> => effect.pipe( Effect.catchCauseIf(Cause.hasDies, (cause) => Effect.andThen( ErrorReporter.report(cause), Effect.failCause(reportedElsewhere(cause)) ) ) );