Merge observability/typed-failures into main
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by oscabriel · committed by oscabriel · parents 1985111, 09babdf · Browse files · 3 files·+33 added−18 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+33 added−18 removed
- tests:+9 added−5 removed
- other:+24 added−13 removed
Provenance
- Commit
0d940fc- Tree
76c7830- Author
oscabriel <oscarhernandeziv@gmail.com>- Committer
oscabriel <oscarhernandeziv@gmail.com>- Parent 1
1985111- Parent 2
09babdf
3 files changed+33 added−18 removed
modified:apps/sandbox/test/telemetry.test.ts+3 added-1 removed
94 unmodified lines959697989910010110210310310410510610710810954 unmodified lines94 unmodified lines
expect([ spans.map(({ name }) => name), Object.fromEntries(spans[0]?.attributes ?? []), Object.fromEntries(spans[1]?.attributes ?? []), spans[1]?.exceptions, ]).toStrictEqual([ [ROOT, "Checker.check"], { project, sha }, [{ name: "VerifierBlocked" }], { failure: "VerifierBlocked" }, [], ]); }) );
54 unmodified linesmodified:packages/telemetry/src/spans/binding.ts+24 added-13 removed
2 unmodified lines3 // way to make a span active once it is open: every span opened here is a4 // child of the invocation's root span, a sibling of the others and of the5 // runtime's own (fetch, RPC, D1, storage) opened while it runs. Effect keeps6 // the nesting; the trace shows each span's name, timing, ids and failures.7-import { Cause, Exit, Layer, Predicate, Tracer } from "effect";7+import { Array, Cause, Exit, Layer, Predicate, Tracer } from "effect";8 9 import type { DefectKeeping } from "../defects.ts";10 import type { Scrub } from "../logger.ts";11 import { INVOCATION_KEYS, InvocationSpan, SPAN_KEYS } from "../spans.ts";18 unmodified lines30 */31 readonly defects: DefectKeeping;32 }33 34-// A typed failure is a refusal or a port's error: its tag says what it was.35-// Only a defect brings its message and stack, scrubbed.34+// A typed failure is a refusal or a port's error, the product working: its35+// tag goes on the span as `failure`. workerd logs each recorded exception at36+// error level, where Issues, smoke and the alerts count it, so only a defect37+// is recorded as one, with its message and stack, scrubbed.38 const recordFailures = <A, E>(39 span: WorkerSpan,40 exit: Exit.Exit<A, E>,41 { defects, scrub }: Keeping1 unmodified line43 if (Exit.isSuccess(exit)) {44 return;45 }46 45- for (const reason of exit.cause.reasons) {46- if (Cause.isFailReason(reason)) {47+ const failures = Array.dedupe(48+ exit.cause.reasons.flatMap((reason) => {49+ if (!Cause.isFailReason(reason)) {50+ return [];51+ }52+53 const failure = reason.error;54 49- span.recordException({50- name:51- Predicate.hasProperty(failure, "_tag") &&52- Predicate.isString(failure._tag)53- ? failure._tag54- : "Failure",55- });56- }55+ return [56+ Predicate.hasProperty(failure, "_tag") &&57+ Predicate.isString(failure._tag)58+ ? failure._tag59+ : "Failure",60+ ];61+ })62+ );63 64+ if (Array.isReadonlyArrayNonEmpty(failures)) {65+ span.setAttribute("failure", failures.join(","));66+ }67+68+ for (const reason of exit.cause.reasons) {69 if (Cause.isDieReason(reason)) {70 for (const error of Cause.prettyErrors(Cause.fromReasons([reason]))) {71 span.recordException(72 defects === "name"107 unmodified linesmodified:packages/telemetry/test/spans.test.ts+6 added-4 removed
112 unmodified lines113 })114 );115 116 it.effect(117- "records a typed failure by its tag alone, and a defect by its name, message and stack, scrubbed",117+ "keeps a typed failure's tag as an attribute, never an exception workerd would log as an error, and records a defect by its name, message and stack, scrubbed",118 () =>119 Effect.gen(function* () {120 const secret = "tok_0123456789abcdef0123456789abcdef";121 23 unmodified lines145 });146 147 const [, refused, defect] = spans;148 149- expect(refused?.exceptions).toStrictEqual([150- { name: "VerifierBlocked" },151- ]);149+ expect(refused?.exceptions).toStrictEqual([]);150+ expect(Object.fromEntries(refused?.attributes ?? [])).toStrictEqual({151+ failure: "VerifierBlocked",152+ });153+ expect(Object.fromEntries(defect?.attributes ?? [])).toStrictEqual({});154 expect(155 defect?.exceptions.map(({ message, name }) => ({ message, name }))156 ).toStrictEqual([157 { message: "no fetch with <redacted>", name: "TypeError" },163 unmodified lines