Skip to content
Myr

observability.test.ts

This file

Path

packages/config/test/observability.test.ts

194 lines · 5.8 KiB · TypeScript

Last changed by

Merge observability into mainoscabrielbefore adoption1985111

History of this file

Take it

Merge observability into mainoscabrielbefore adoption1985111

lines 1–194why, one step back
1import { NodeServices } from "@effect/platform-node";
2import { describe, expect, it } from "@effect/vitest";3import { Effect, FileSystem, Path, Schema } from "effect";45import { blankJsonc } from "../../../infra/jsonc.ts";6import { stages } from "../../../infra/stages.ts";7import { repoRoot } from "./root.ts";89const WORKERS = ["platform", "sandbox", "web"] as const;1011type Worker = (typeof WORKERS)[number];1213const Container = Schema.Struct({14 class_name: Schema.String,15 observability: Schema.optionalKey(Schema.Unknown),16});1718const Block = Schema.Struct({19 containers: Schema.optionalKey(Schema.Array(Container)),20 observability: Schema.optionalKey(Schema.Unknown),21 upload_source_maps: Schema.optionalKey(Schema.Boolean),22});2324type Block = typeof Block.Type;2526const Wrangler = Schema.fromJsonString(27 Schema.Struct({28 ...Block.fields,29 env: Schema.Record(Schema.String, Block),30 })31);3233// What a Worker keeps of each invocation: its logs, its own line among them,34// and its Issues, none sampled away and none left unkept.35const Recorded = Schema.Struct({36 enabled: Schema.Literal(true),37 issues: Schema.Struct({ enabled: Schema.Literal(true) }),38 logs: Schema.Struct({39 enabled: Schema.Literal(true),40 head_sampling_rate: Schema.Literal(1),41 invocation_logs: Schema.Literal(true),42 persist: Schema.optionalKey(Schema.Literal(true)),43 }),44});4546const isRecorded = Schema.is(Recorded);4748// Its trace: every invocation's, kept in the account.49const unsampled = {50 enabled: Schema.Literal(true),51 head_sampling_rate: Schema.Literal(1),52 persist: Schema.optionalKey(Schema.Literal(true)),53};5455const Traces = {56 // Sent nowhere: a destination would take it out of the account.57 kept: Schema.Struct({58 traces: Schema.Struct({59 ...unsampled,60 destinations: Schema.optionalKey(Schema.Never),61 }),62 }),63 unsampled: Schema.Struct({ traces: Schema.Struct(unsampled) }),64};6566// The runtime's own spans record each Durable Object SQL statement's bound67// values and each fetch's full URL, so the platform's traces hold layer68// names and repos and sealed values. The maintainer keeps them, in the69// account's own trace store alone (principle 7).70const TRACING: Readonly<Record<Worker, keyof typeof Traces>> = {71 platform: "kept",72 sandbox: "unsampled",73 web: "unsampled",74};7576const LOCAL = "local";7778/** The top level is stage `local`, never deployed; each deployed stage is an `env` block. */79const named = [LOCAL, ...stages] as const;8081interface Staged {82 readonly worker: Worker;83 readonly stage: (typeof named)[number];84 readonly top: Block;85 readonly block: Block | undefined;86}8788const wranglerOf = (worker: Worker) =>89 Effect.gen(function* () {90 const fs = yield* FileSystem.FileSystem;91 const path = yield* Path.Path;92 const file = path.join(yield* repoRoot, `apps/${worker}/wrangler.jsonc`);93 const text = yield* fs.readFileString(file);9495 return yield* Schema.decodeEffect(Wrangler)(blankJsonc(text));96 });9798const everyStage = Effect.map(99 Effect.forEach(WORKERS, (worker) =>100 Effect.map(wranglerOf(worker), (top) =>101 named.map((stage): Staged => ({102 block: stage === LOCAL ? top : top.env[stage],103 stage,104 top,105 worker,106 }))107 )108 ),109 (all) => all.flat()110).pipe(Effect.provide(NodeServices.layer));111112// Wrangler gives a stage the top level's `observability` and113// `upload_source_maps` unless the stage sets its own, but never its114// `containers`: a stage without them declares none.115const inherited = <K extends "observability" | "upload_source_maps">(116 { block, top }: Staged,117 key: K118) => block?.[key] ?? top[key];119120const each = <A>(all: readonly Staged[], read: (staged: Staged) => A) =>121 Object.fromEntries(122 all.map((staged) => [`${staged.worker} ${staged.stage}`, read(staged)])123 );124125describe("every Worker, in every stage", () => {126 it.effect("logs each invocation and reports its Issues, unsampled", () =>127 Effect.gen(function* () {128 const all = yield* everyStage;129130 expect(131 each(all, (staged) => isRecorded(inherited(staged, "observability")))132 ).toStrictEqual(each(all, () => true));133 })134 );135136 it.effect(137 "traces each invocation, unsampled, and sends the platform's nowhere",138 () =>139 Effect.gen(function* () {140 const all = yield* everyStage;141142 expect(143 each(all, (staged) =>144 Schema.is(Traces[TRACING[staged.worker]])(145 inherited(staged, "observability")146 )147 )148 ).toStrictEqual(each(all, () => true));149 })150 );151152 it.effect(153 "uploads its source maps, so an uncaught exception's stack names the source",154 () =>155 Effect.gen(function* () {156 const all = yield* everyStage;157158 expect(159 each(all, (staged) => inherited(staged, "upload_source_maps"))160 ).toStrictEqual(each(all, () => true));161 })162 );163});164165// A held-back check's sealed files and output are in a Checker's container,166// so its own logs stay off (principle 7). Every container says what it keeps:167// one that leaves `observability` out keeps what its application last had.168describe("every container, in every stage", () => {169 it.effect(170 "is a Checker with its logs off or a Lander with them on, set in the stage itself",171 () =>172 Effect.gen(function* () {173 const all = yield* everyStage;174175 const declared = Object.fromEntries(176 all.flatMap(({ block, stage, worker }) =>177 (block?.containers ?? []).map(178 ({ class_name, observability }) =>179 [`${worker} ${stage} ${class_name}`, observability] as const180 )181 )182 );183184 expect(declared).toStrictEqual(185 Object.fromEntries(186 named.flatMap((stage) => [187 [`sandbox ${stage} Checker`, { enabled: false }],188 [`sandbox ${stage} Lander`, { enabled: true }],189 ])190 )191 );192 })193 );194});