Skip to content
Myr

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 file

Take it

Merge observability into mainoscabrielbefore adoption1985111

lines 1–98why, one step back
1// The spans port. Effect's own `Tracer` is the port's face: code opens a span
2// with `Effect.withSpan` or `Effect.fn("Name")` and never learns where it3// goes. Behind it sits a Worker's `tracing` (`cloudflare:workers`), described4// here by what the bridge uses of it, so no package imports the Workers5// runtime: a Worker hands its own `tracing` to `spans/binding.ts`, and a test6// hands `spans/memory.ts`'s recording one.7import { Array, Context, Effect } from "effect";8import { constVoid } from "effect/Function";910export type SpanValue = string | number | boolean;1112/** The part of a workerd span the bridge uses; `cloudflare:workers`' `Span` fits it. */13export interface WorkerSpan {14 /** False when the invocation was not sampled: every call is then a no-op. */15 readonly isTraced: boolean;16 readonly setAttribute: (key: string, value: SpanValue) => void;17 readonly recordException: (exception: {18 readonly name: string;19 readonly message?: string;20 readonly stack?: string;21 }) => void;22 readonly end: () => void;23}2425/** The part of `cloudflare:workers`' `tracing` the bridge uses. */26export interface WorkerTracing {27 /** Never made active: a span opened while it is open is its sibling, not its child. */28 readonly startSpan: (name: string) => WorkerSpan;29 /** The invocation's root span while nothing else is active; none outside an invocation. */30 readonly getActiveSpan: () => WorkerSpan | undefined;31}3233/**34 * What Issues shows beside each occurrence: ids alone (AGENTS.md principle 7),35 * never a repo, a layer, a path, a check's name or a value.36 */37export interface InvocationIds {38 readonly project?: string;39 readonly ask?: string;40 readonly sha?: string;41 readonly requestId?: string;42 readonly lane?: string;43 /** Refusal tags, comma-joined. */44 readonly refusal?: string;45}4647export const INVOCATION_KEYS = [48 "project",49 "ask",50 "sha",51 "requestId",52 "lane",53 "refusal",54] as const satisfies readonly (keyof InvocationIds)[];5556/**57 * The span attributes a span may carry onto a Worker's trace; any other key58 * stays in Effect. A library's span (an HTTP client's `url.full`, SQL's59 * `db.query.text`) or a later one of ours cannot put a repo, a path, a query60 * or a value onto a trace by mistake.61 */62export const SPAN_KEYS: ReadonlySet<string> = new Set([63 ...INVOCATION_KEYS,64 // A Workflow step's whole name, or the subcommand of a Lander's step.65 "step",66 // The kind of repo a push reached: `Canonical`, `ClaimFork` and the rest.67 "target",68]);6970export const InvocationSpan = Context.Reference<{71 readonly annotate: (ids: InvocationIds) => void;72}>("telemetry/InvocationSpan", {73 defaultValue: () => ({ annotate: constVoid }),74});7576/** The tags an invocation was refused with, each once; none when it was not. */77export const refusedWith = (78 refusals: readonly { readonly _tag: string }[]79): InvocationIds =>80 Array.isReadonlyArrayNonEmpty(refusals)81 ? { refusal: Array.dedupe(refusals.map(({ _tag }) => _tag)).join(",") }82 : {};8384/** Sets ids on the invocation's root span, where Issues reads them; a no-op off a Worker. */85export const annotateInvocation = (ids: InvocationIds): Effect.Effect<void> =>86 InvocationSpan.use(({ annotate }) =>87 Effect.sync(() => {88 annotate(ids);89 })90 );9192/** Runs `effect` as the invocation `ids` name: they go onto its root span, and onto each line it logs. */93export const withInvocation =94 (ids: InvocationIds) =>95 <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> =>96 Effect.andThen(annotateInvocation(ids), effect).pipe(97 Effect.annotateLogs({ ...ids })98 );