import { Context, Effect, Exit, Layer, Option, Tracer } from "effect"; /** * How a captured span ended, or `"unended"` while it is still open. * * @remarks * `"failure"` is a typed failure, `"defect"` an unexpected error, and `"interrupt"` an interruption with neither. An * exit that holds more than one reason reads as the first of failure, defect and interrupt it holds. * * @public */ export type SpanOutcome = "success" | "failure" | "defect" | "interrupt" | "unended"; /** * One span a {@link SpanCapture} tracer was asked for, as it stood when it was read. * * @public */ export interface CapturedSpan { /** The span's name. */ readonly name: string; /** The parent span's name, and `Option.none()` for a root span or an external parent. */ readonly parent: Option.Option; /** * Whether the span is sampled: the decision core made before it asked the tracer for the span, so it reflects * `Tracer.MinimumTraceLevel`, a span's own `level` and an unsampled parent. */ readonly sampled: boolean; /** The span's attributes at the time of the read. */ readonly attributes: ReadonlyMap; /** How the span ended, or `"unended"` when it has not. */ readonly outcome: SpanOutcome; } /** * What a {@link SpanCapture} answers. * * @public */ export interface SpanCaptureShape { /** * Every span started so far, in start order, so a parent comes before its children. Each read is a fresh * snapshot: a span still open reads `"unended"` now and its real outcome on a later read. */ readonly spans: Effect.Effect>; /** The first span started with this name, or `Option.none()` when none was. */ readonly find: (name: string) => Effect.Effect>; } const outcomeOf = (span: Tracer.NativeSpan): SpanOutcome => { if (span.status._tag !== "Ended") return "unended"; const exit = span.status.exit; if (Exit.isSuccess(exit)) return "success"; if (Exit.hasFails(exit)) return "failure"; if (Exit.hasDies(exit)) return "defect"; return "interrupt"; }; const snapshot = (span: Tracer.NativeSpan): CapturedSpan => ({ name: span.name, parent: Option.flatMap(span.parent, (parent) => (parent._tag === "Span" ? Option.some(parent.name) : Option.none())), sampled: span.sampled, attributes: new Map(span.attributes), outcome: outcomeOf(span), }); /** One capture: a tracer that keeps every span it is asked for, and the service that reads them. */ const makeCapture = (): { readonly tracer: Tracer.Tracer; readonly shape: SpanCaptureShape } => { const started: Array = []; const tracer = Tracer.make({ span: (options) => { // `sampled` arrives already decided by core, and the native span carries it through. const span = new Tracer.NativeSpan(options); started.push(span); return span; }, }); const spans = Effect.sync(() => started.map(snapshot)); return { tracer, shape: { spans, find: (name) => Effect.sync(() => Option.map(Option.fromUndefinedOr(started.find((span) => span.name === name)), snapshot)), }, }; }; /** * Captures the spans a program emits, so a test can assert on their names, attributes, parents and outcomes. * Imported from `@effected/plugin/testing`. * * @remarks * Two forms over one capturing tracer: * * - {@link SpanCapture.run} wraps one effect and answers its exit with the spans it started. * - {@link SpanCapture.layer} installs the tracer for everything it is provided to, and the program reads the spans * through the `SpanCapture` service. * * A span is recorded when it starts, so one that never ends is still there, with the outcome `"unended"`. A span * below `Tracer.MinimumTraceLevel` is recorded too, with `sampled` false: that is the span an exporter would leave * out. * * Both forms replace the tracer for the program under test, so nothing is exported while a capture is installed. * This is test tooling: an application installs its tracer at its entry point. It runs on the `effect` the test * itself imports, so the spans it sees are the ones that copy of `effect` starts. * * @example * ```ts * import { assert, it } from "@effect/vitest"; * import { SpanCapture } from "@effected/plugin/testing"; * import { Effect, Option } from "effect"; * * const runJob = Effect.fn("Jobs.run")(function* () { * yield* Effect.annotateCurrentSpan("jobs.count", 2); * yield* Effect.fail("boom").pipe(Effect.withSpan("Jobs.step")); * }); * * it.effect("records the job's spans", () => * Effect.gen(function* () { * const { spans } = yield* SpanCapture.run(runJob()); * assert.deepStrictEqual( * spans.map((span) => [span.name, span.outcome]), * [["Jobs.run", "failure"], ["Jobs.step", "failure"]], * ); * assert.deepStrictEqual(spans[1]?.parent, Option.some("Jobs.run")); * assert.strictEqual(spans[0]?.attributes.get("jobs.count"), 2); * }), * ); * ``` * * @public */ export class SpanCapture extends Context.Service()("@effected/testing/SpanCapture") { /** * Runs `effect` under a fresh capturing tracer and answers its exit with every span it started, in start order. * * @remarks * Never fails: the effect's own failure, defect or interruption is the `exit`. The spans are read once the effect * has finished, so a span reads `"unended"` only when nothing ended it, such as one made with `Effect.makeSpan` * and never closed. Each call captures on its own, so two runs in one test never see each other's spans. */ static readonly run = ( effect: Effect.Effect, ): Effect.Effect<{ readonly exit: Exit.Exit; readonly spans: ReadonlyArray }, never, R> => Effect.suspend(() => { const { tracer, shape } = makeCapture(); return Effect.exit(effect).pipe( Effect.withTracer(tracer), Effect.flatMap((exit) => Effect.map(shape.spans, (spans) => ({ exit, spans }))), ); }); /** * Installs a capturing tracer and provides the `SpanCapture` service that reads it. * * @remarks * The capture lasts as long as the layer: provided to one test with `Effect.provide`, it holds that test's spans; * shared by a suite, it accumulates every test's spans. Read the spans inside the provide, since the service is * out of reach after it. * * @example * ```ts * import { assert, it } from "@effect/vitest"; * import { SpanCapture } from "@effected/plugin/testing"; * import { Effect, Option } from "effect"; * * it.effect("sees a span that is still open", () => * Effect.gen(function* () { * const capture = yield* SpanCapture; * yield* Effect.gen(function* () { * const open = yield* capture.find("Jobs.run"); * assert.deepStrictEqual(Option.map(open, (span) => span.outcome), Option.some("unended")); * }).pipe(Effect.withSpan("Jobs.run")); * const ended = yield* capture.find("Jobs.run"); * assert.deepStrictEqual(Option.map(ended, (span) => span.outcome), Option.some("success")); * }).pipe(Effect.provide(SpanCapture.layer)), * ); * ``` */ static readonly layer: Layer.Layer = Layer.effectContext( Effect.sync(() => { const { tracer, shape } = makeCapture(); return Context.make(SpanCapture, shape).pipe(Context.add(Tracer.Tracer, tracer)); }), ); }