The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Axiom from "@alchemy.run/axiom"; |
| 2 | import { Stack } from "alchemy/Stack"; |
| 3 | import * as Config from "effect/Config"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Layer from "effect/Layer"; |
| 6 | import * as Option from "effect/Option"; |
| 7 | |
| 8 | /** |
| 9 | * The stage, the same in both phases: the Stack's at deploy, and the value |
| 10 | * bound onto the Worker at runtime. Datasets are named after it, so every |
| 11 | * stage (and every developer's) gets its own. |
| 12 | */ |
| 13 | const stage = Effect.gen(function* () { |
| 14 | const stack = yield* Effect.serviceOption(Stack); |
| 15 | if (Option.isSome(stack)) return stack.value.stage; |
| 16 | return yield* Config.String("ALCHEMY_STAGE").pipe(Effect.orElseSucceed(() => "dev")); |
| 17 | }); |
| 18 | |
| 19 | /** |
| 20 | * The bank's telemetry in Axiom: a traces and a logs dataset, and an ingest |
| 21 | * token that may only write to them. The Worker exports its own spans and |
| 22 | * logs here, and relays the app's spans here too, with this token, so it |
| 23 | * never reaches a browser. |
| 24 | */ |
| 25 | export const Observability = Layer.unwrap( |
| 26 | Effect.gen(function* () { |
| 27 | const name = `bank-${(yield* stage).replace(/_/g, "-")}`; |
| 28 | const traces = yield* Axiom.Dataset("Traces", { |
| 29 | name: `${name}-traces`, |
| 30 | kind: "otel:traces:v1", |
| 31 | }); |
| 32 | const logs = yield* Axiom.Dataset("Logs", { name: `${name}-logs`, kind: "otel:logs:v1" }); |
| 33 | const ingest = yield* Axiom.ApiToken("Ingest", { |
| 34 | name: `${name}-ingest`, |
| 35 | datasetCapabilities: { |
| 36 | [`${name}-traces`]: { ingest: ["create"] }, |
| 37 | [`${name}-logs`]: { ingest: ["create"] }, |
| 38 | }, |
| 39 | }); |
| 40 | return Axiom.Telemetry({ token: ingest, traces, logs, serviceName: "bank" }); |
| 41 | }), |
| 42 | ); |