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
Merge observability into mainoscabrielbefore adoption1985111
lines 1–136why, one step back1// A Worker's `tracing` in memory, kept as workerd keeps it: the invocation's2// root span is active throughout, and `startSpan` opens a span under whatever3// is active without making it active, so its parent is always the root.4import { Context, Effect, Layer } from "effect";5import { constUndefined, constVoid, identity } from "effect/Function";67import type { SpanValue, WorkerSpan, WorkerTracing } from "../spans.ts";8import { SpansBinding } from "./binding.ts";9import type { Keeping } from "./binding.ts";1011export interface RecordedException {12 readonly name: string;13 readonly message?: string;14 readonly stack?: string;15}1617export interface RecordedSpan {18 readonly name: string;19 /** The span that was active when this one opened; none for the root. */20 readonly parent: string | undefined;21 readonly attributes: ReadonlyMap<string, SpanValue>;22 readonly exceptions: readonly RecordedException[];23 readonly ended: boolean;24}2526export const ROOT = "invocation";2728export interface RecordingTracing {29 readonly tracing: WorkerTracing;30 /** Every span opened, the root first, in the order each opened. */31 readonly spans: () => readonly RecordedSpan[];32}3334interface OpenSpan {35 readonly name: string;36 readonly parent: string | undefined;37 readonly attributes: Map<string, SpanValue>;38 readonly exceptions: RecordedException[];39 ended: boolean;40}4142/** `traced: false` is an invocation the head sampling left out. */43export const recordingTracing = (traced = true): RecordingTracing => {44 const opened: OpenSpan[] = [];4546 const open = (name: string, parent?: string): WorkerSpan => {47 const span: OpenSpan = {48 attributes: new Map(),49 ended: false,50 exceptions: [],51 name,52 parent,53 };5455 opened.push(span);5657 // As in workerd, an ended span is traced no more: what it is handed then is lost.58 const tracing = () => traced && !span.ended;5960 return {61 end: () => {62 span.ended = true;63 },64 get isTraced() {65 return tracing();66 },67 recordException: (exception) => {68 if (tracing()) {69 span.exceptions.push(exception);70 }71 },72 setAttribute: (key, value) => {73 if (tracing()) {74 span.attributes.set(key, value);75 }76 },77 };78 };7980 const root = open(ROOT);8182 return {83 spans: () =>84 opened.map((span) => ({85 attributes: new Map(span.attributes),86 ended: span.ended,87 exceptions: [...span.exceptions],88 name: span.name,89 parent: span.parent,90 })),91 tracing: {92 getActiveSpan: () => root,93 startSpan: (name) => open(name, ROOT),94 },95 };96};9798const untracedSpan: WorkerSpan = {99 end: constVoid,100 isTraced: false,101 recordException: constVoid,102 setAttribute: constVoid,103};104105/** A runtime outside workerd, where no invocation is traced: every span is a no-op. */106export const untracedTracing: WorkerTracing = {107 getActiveSpan: constUndefined,108 startSpan: () => untracedSpan,109};110111export class SpansRecorded extends Context.Service<112 SpansRecorded,113 { readonly spans: Effect.Effect<readonly RecordedSpan[]> }114>()("telemetry/SpansRecorded") {}115116export const SpansMemory = ({117 keeping = { defects: "whole", scrub: identity },118 traced = true,119}: {120 readonly keeping?: Keeping;121 /** False: an invocation the head sampling left out. */122 readonly traced?: boolean;123} = {}): Layer.Layer<SpansRecorded> =>124 Layer.unwrap(125 Effect.sync(() => {126 const recording = recordingTracing(traced);127128 return Layer.merge(129 SpansBinding(recording.tracing, keeping),130 Layer.succeed(131 SpansRecorded,132 SpansRecorded.of({ spans: Effect.sync(recording.spans) })133 )134 );135 })136 );