spans.ts
This file
Path
packages/telemetry/src/spans.ts
98 lines · 3.5 KiB · TypeScript
Last changed by
Merge observability into mainoscabrielbefore adoption1985111
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899// The spans port. Effect's own `Tracer` is the port's face: code opens a span// with `Effect.withSpan` or `Effect.fn("Name")` and never learns where it// goes. Behind it sits a Worker's `tracing` (`cloudflare:workers`), described// here by what the bridge uses of it, so no package imports the Workers// runtime: a Worker hands its own `tracing` to `spans/binding.ts`, and a test// hands `spans/memory.ts`'s recording one.import { Array, Context, Effect } from "effect";import { constVoid } from "effect/Function";
export type SpanValue = string | number | boolean;
/** The part of a workerd span the bridge uses; `cloudflare:workers`' `Span` fits it. */export interface WorkerSpan { /** False when the invocation was not sampled: every call is then a no-op. */ readonly isTraced: boolean; readonly setAttribute: (key: string, value: SpanValue) => void; readonly recordException: (exception: { readonly name: string; readonly message?: string; readonly stack?: string; }) => void; readonly end: () => void;}
/** The part of `cloudflare:workers`' `tracing` the bridge uses. */export interface WorkerTracing { /** Never made active: a span opened while it is open is its sibling, not its child. */ readonly startSpan: (name: string) => WorkerSpan; /** The invocation's root span while nothing else is active; none outside an invocation. */ readonly getActiveSpan: () => WorkerSpan | undefined;}
/** * What Issues shows beside each occurrence: ids alone (AGENTS.md principle 7), * never a repo, a layer, a path, a check's name or a value. */export interface InvocationIds { readonly project?: string; readonly ask?: string; readonly sha?: string; readonly requestId?: string; readonly lane?: string; /** Refusal tags, comma-joined. */ readonly refusal?: string;}
export const INVOCATION_KEYS = [ "project", "ask", "sha", "requestId", "lane", "refusal",] as const satisfies readonly (keyof InvocationIds)[];
/** * The span attributes a span may carry onto a Worker's trace; any other key * stays in Effect. A library's span (an HTTP client's `url.full`, SQL's * `db.query.text`) or a later one of ours cannot put a repo, a path, a query * or a value onto a trace by mistake. */export const SPAN_KEYS: ReadonlySet<string> = new Set([ ...INVOCATION_KEYS, // A Workflow step's whole name, or the subcommand of a Lander's step. "step", // The kind of repo a push reached: `Canonical`, `ClaimFork` and the rest. "target",]);
export const InvocationSpan = Context.Reference<{ readonly annotate: (ids: InvocationIds) => void;}>("telemetry/InvocationSpan", { defaultValue: () => ({ annotate: constVoid }),});
/** The tags an invocation was refused with, each once; none when it was not. */export const refusedWith = ( refusals: readonly { readonly _tag: string }[]): InvocationIds => Array.isReadonlyArrayNonEmpty(refusals) ? { refusal: Array.dedupe(refusals.map(({ _tag }) => _tag)).join(",") } : {};
/** Sets ids on the invocation's root span, where Issues reads them; a no-op off a Worker. */export const annotateInvocation = (ids: InvocationIds): Effect.Effect<void> => InvocationSpan.use(({ annotate }) => Effect.sync(() => { annotate(ids); }) );
/** Runs `effect` as the invocation `ids` name: they go onto its root span, and onto each line it logs. */export const withInvocation = (ids: InvocationIds) => <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> => Effect.andThen(annotateInvocation(ids), effect).pipe( Effect.annotateLogs({ ...ids }) );