binding.ts
This file
Path
packages/telemetry/src/spans/binding.ts
179 lines · 4.7 KiB · TypeScript
Last changed by
Merge observability/typed-failures into mainoscabrielbefore adoption0d940fc
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180// Effect's spans onto a Worker's trace, through its `tracing`. Each span opens// with `startSpan`, which the runtime never makes active, and workerd has no// way to make a span active once it is open: every span opened here is a// child of the invocation's root span, a sibling of the others and of the// runtime's own (fetch, RPC, D1, storage) opened while it runs. Effect keeps// the nesting; the trace shows each span's name, timing, ids and failures.import { Array, Cause, Exit, Layer, Predicate, Tracer } from "effect";
import type { DefectKeeping } from "../defects.ts";import type { Scrub } from "../logger.ts";import { INVOCATION_KEYS, InvocationSpan, SPAN_KEYS } from "../spans.ts";import type { InvocationIds, SpanValue, WorkerSpan, WorkerTracing,} from "../spans.ts";
const isSpanValue = (value: unknown): value is SpanValue => Predicate.isString(value) || Predicate.isNumber(value) || Predicate.isBoolean(value);
/** What a span keeps of what it carries. */export interface Keeping { readonly scrub: Scrub; /** * All of a defect, scrubbed, or its name alone where its text may quote * what no trace may hold (a layer's repo, a held-back check's path). */ readonly defects: DefectKeeping;}
// A typed failure is a refusal or a port's error, the product working: its// tag goes on the span as `failure`. workerd logs each recorded exception at// error level, where Issues, smoke and the alerts count it, so only a defect// is recorded as one, with its message and stack, scrubbed.const recordFailures = <A, E>( span: WorkerSpan, exit: Exit.Exit<A, E>, { defects, scrub }: Keeping) => { if (Exit.isSuccess(exit)) { return; }
const failures = Array.dedupe( exit.cause.reasons.flatMap((reason) => { if (!Cause.isFailReason(reason)) { return []; }
const failure = reason.error;
return [ Predicate.hasProperty(failure, "_tag") && Predicate.isString(failure._tag) ? failure._tag : "Failure", ]; }) );
if (Array.isReadonlyArrayNonEmpty(failures)) { span.setAttribute("failure", failures.join(",")); }
for (const reason of exit.cause.reasons) { if (Cause.isDieReason(reason)) { for (const error of Cause.prettyErrors(Cause.fromReasons([reason]))) { span.recordException( defects === "name" ? { name: error.name } : { message: scrub(error.message), name: error.name, stack: scrub(error.stack ?? ""), } ); } } }};
const mirrored = ( span: Tracer.NativeSpan, mirror: WorkerSpan | undefined, keeping: Keeping): Tracer.Span => { const traced = mirror?.isTraced === true ? mirror : undefined;
return { _tag: span._tag, addLinks: (links) => { span.addLinks(links); }, annotations: span.annotations, attribute: (key, value) => { span.attribute(key, value);
if (traced !== undefined && SPAN_KEYS.has(key) && isSpanValue(value)) { traced.setAttribute(key, value); } }, get attributes() { return span.attributes; }, end: (endTime, exit) => { span.end(endTime, exit);
if (traced !== undefined) { recordFailures(traced, exit, keeping); }
mirror?.end(); }, event: (name, startTime, attributes) => { span.event(name, startTime, attributes); }, kind: span.kind, links: span.links, name: span.name, parent: span.parent, sampled: span.sampled, get spanId() { return span.spanId; }, get status() { return span.status; }, get traceId() { return span.traceId; }, };};
/** * A `client` span (fetch, RPC, D1, storage) is the runtime's own to trace, * with what it carries, and SQL's run once per statement: none is opened twice. */export const tracerOver = ( tracing: WorkerTracing, keeping: Keeping): Tracer.Tracer => Tracer.make({ span: (options) => mirrored( new Tracer.NativeSpan(options), options.sampled && options.kind !== "client" ? tracing.startSpan(options.name) : undefined, keeping ), });
const annotateRoot = (tracing: WorkerTracing) => (ids: InvocationIds) => { const root = tracing.getActiveSpan();
if (root?.isTraced !== true) { return; }
for (const key of INVOCATION_KEYS) { const value = ids[key];
if (value !== undefined) { root.setAttribute(key, value); } }};
export const SpansBinding = ( tracing: WorkerTracing, keeping: Keeping): Layer.Layer<never> => Layer.mergeAll( Layer.succeed(Tracer.Tracer, tracerOver(tracing, keeping)), Layer.succeed(InvocationSpan, { annotate: annotateRoot(tracing) }) );