memory.ts
This file
Path
packages/telemetry/src/spans/memory.ts
136 lines · 3.7 KiB · TypeScript
Last changed by
Merge observability into mainoscabrielbefore adoption1985111
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137// A Worker's `tracing` in memory, kept as workerd keeps it: the invocation's// root span is active throughout, and `startSpan` opens a span under whatever// is active without making it active, so its parent is always the root.import { Context, Effect, Layer } from "effect";import { constUndefined, constVoid, identity } from "effect/Function";
import type { SpanValue, WorkerSpan, WorkerTracing } from "../spans.ts";import { SpansBinding } from "./binding.ts";import type { Keeping } from "./binding.ts";
export interface RecordedException { readonly name: string; readonly message?: string; readonly stack?: string;}
export interface RecordedSpan { readonly name: string; /** The span that was active when this one opened; none for the root. */ readonly parent: string | undefined; readonly attributes: ReadonlyMap<string, SpanValue>; readonly exceptions: readonly RecordedException[]; readonly ended: boolean;}
export const ROOT = "invocation";
export interface RecordingTracing { readonly tracing: WorkerTracing; /** Every span opened, the root first, in the order each opened. */ readonly spans: () => readonly RecordedSpan[];}
interface OpenSpan { readonly name: string; readonly parent: string | undefined; readonly attributes: Map<string, SpanValue>; readonly exceptions: RecordedException[]; ended: boolean;}
/** `traced: false` is an invocation the head sampling left out. */export const recordingTracing = (traced = true): RecordingTracing => { const opened: OpenSpan[] = [];
const open = (name: string, parent?: string): WorkerSpan => { const span: OpenSpan = { attributes: new Map(), ended: false, exceptions: [], name, parent, };
opened.push(span);
// As in workerd, an ended span is traced no more: what it is handed then is lost. const tracing = () => traced && !span.ended;
return { end: () => { span.ended = true; }, get isTraced() { return tracing(); }, recordException: (exception) => { if (tracing()) { span.exceptions.push(exception); } }, setAttribute: (key, value) => { if (tracing()) { span.attributes.set(key, value); } }, }; };
const root = open(ROOT);
return { spans: () => opened.map((span) => ({ attributes: new Map(span.attributes), ended: span.ended, exceptions: [...span.exceptions], name: span.name, parent: span.parent, })), tracing: { getActiveSpan: () => root, startSpan: (name) => open(name, ROOT), }, };};
const untracedSpan: WorkerSpan = { end: constVoid, isTraced: false, recordException: constVoid, setAttribute: constVoid,};
/** A runtime outside workerd, where no invocation is traced: every span is a no-op. */export const untracedTracing: WorkerTracing = { getActiveSpan: constUndefined, startSpan: () => untracedSpan,};
export class SpansRecorded extends Context.Service< SpansRecorded, { readonly spans: Effect.Effect<readonly RecordedSpan[]> }>()("telemetry/SpansRecorded") {}
export const SpansMemory = ({ keeping = { defects: "whole", scrub: identity }, traced = true,}: { readonly keeping?: Keeping; /** False: an invocation the head sampling left out. */ readonly traced?: boolean;} = {}): Layer.Layer<SpansRecorded> => Layer.unwrap( Effect.sync(() => { const recording = recordingTracing(traced);
return Layer.merge( SpansBinding(recording.tracing, keeping), Layer.succeed( SpansRecorded, SpansRecorded.of({ spans: Effect.sync(recording.spans) }) ) ); }) );