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 * as Cloudflare from "@alchemy.run/cloudflare"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as HttpClient from "effect/http/HttpClient"; |
| 7 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 8 | import * as Layer from "effect/Layer"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import { expectUrlContains } from "../Cloudflare/Utils/Http.ts"; |
| 11 | import AxiomTracedWorker, { |
| 12 | Ingest, |
| 13 | Logs, |
| 14 | METRICS_DATASET, |
| 15 | Metrics, |
| 16 | TRACES_DATASET, |
| 17 | Traces, |
| 18 | } from "./fixtures/axiom-traced-worker.ts"; |
| 19 | |
| 20 | const { test } = Test.make({ |
| 21 | providers: Layer.mergeAll(Cloudflare.providers(), Axiom.providers()), |
| 22 | }); |
| 23 | |
| 24 | const hasAxiomCreds = !!(process.env.AXIOM_TOKEN || process.env.AXIOM_API_KEY); |
| 25 | |
| 26 | // Query a dataset's recent data out-of-band with the deployer's org token. The |
| 27 | // worker ingests with its own least-privilege token; this read proves the |
| 28 | // data actually landed in Axiom. |
| 29 | const queryDataset = (dataset: string) => |
| 30 | Effect.gen(function* () { |
| 31 | const response = yield* HttpClient.execute( |
| 32 | HttpClientRequest.post("https://api.axiom.co/v1/datasets/_apl?format=legacy").pipe( |
| 33 | HttpClientRequest.setHeaders({ |
| 34 | Authorization: `Bearer ${process.env.AXIOM_TOKEN ?? process.env.AXIOM_API_KEY}`, |
| 35 | "Content-Type": "application/json", |
| 36 | }), |
| 37 | HttpClientRequest.bodyJsonUnsafe({ |
| 38 | apl: `['${dataset}'] | where _time > ago(10m) | limit 1000`, |
| 39 | }), |
| 40 | ), |
| 41 | ); |
| 42 | return yield* response.text; |
| 43 | }); |
| 44 | |
| 45 | interface MetricsResult { |
| 46 | series?: { metric: string; summary?: number | null }[]; |
| 47 | } |
| 48 | |
| 49 | // Metrics datasets are queried with MPL on the dataset's own edge. Returns |
| 50 | // the counter's total over the window, 0 until a data point has landed. |
| 51 | const queryWorkCount = (edgeUrl: string) => |
| 52 | Effect.gen(function* () { |
| 53 | const response = yield* HttpClient.execute( |
| 54 | HttpClientRequest.post(`${edgeUrl.replace(/\/$/, "")}/v1/query/_mpl`).pipe( |
| 55 | HttpClientRequest.setHeaders({ |
| 56 | Authorization: `Bearer ${process.env.AXIOM_TOKEN ?? process.env.AXIOM_API_KEY}`, |
| 57 | "Content-Type": "application/json", |
| 58 | }), |
| 59 | HttpClientRequest.bodyJsonUnsafe({ |
| 60 | startTime: "now-10m", |
| 61 | endTime: "now", |
| 62 | mpl: `\`${METRICS_DATASET}\`:\`axiom_e2e_work_total\` | align to 1m using sum`, |
| 63 | }), |
| 64 | ), |
| 65 | ); |
| 66 | if (response.status !== 200) return 0; |
| 67 | const result = (yield* response.json) as MetricsResult; |
| 68 | return (result.series ?? []) |
| 69 | .filter((series) => series.metric === "axiom_e2e_work_total") |
| 70 | .reduce((total, series) => total + (series.summary ?? 0), 0); |
| 71 | }); |
| 72 | |
| 73 | test.provider.skipIf(!hasAxiomCreds)( |
| 74 | "Worker exports telemetry to Axiom via the Axiom.Telemetry binding layer", |
| 75 | (stack) => |
| 76 | Effect.gen(function* () { |
| 77 | yield* stack.destroy(); |
| 78 | |
| 79 | const { worker, metrics } = yield* stack.deploy( |
| 80 | Effect.gen(function* () { |
| 81 | yield* Traces; |
| 82 | yield* Logs; |
| 83 | const metrics = yield* Metrics; |
| 84 | yield* Ingest; |
| 85 | const worker = yield* AxiomTracedWorker; |
| 86 | return { worker, metrics }; |
| 87 | }), |
| 88 | ); |
| 89 | |
| 90 | // Drive one traced request (fresh workers.dev URLs take a few |
| 91 | // seconds to start serving). |
| 92 | const url = worker.url as string; |
| 93 | yield* expectUrlContains(`${url}/work`, "axiom-did-work", { |
| 94 | timeout: "240 seconds", |
| 95 | }); |
| 96 | |
| 97 | // The request scope's flush ships the trace via ctx.waitUntil; poll |
| 98 | // Axiom until it is queryable. |
| 99 | const body = yield* queryDataset(TRACES_DATASET).pipe( |
| 100 | Effect.repeat({ |
| 101 | schedule: Schedule.spaced("5 seconds"), |
| 102 | until: (text) => text.includes("otel-axiom-e2e"), |
| 103 | times: 36, |
| 104 | }), |
| 105 | ); |
| 106 | expect(body).toContain("otel-axiom-e2e"); |
| 107 | expect(body).toContain("axiom.child-span"); |
| 108 | // The HttpMiddleware tracer's server span for the request: Effect's |
| 109 | // default name, the request method. |
| 110 | const spans = yield* Effect.try( |
| 111 | () => (JSON.parse(body) as { matches: { data: { name: string; kind: string } }[] }).matches, |
| 112 | ); |
| 113 | expect(spans.map((match) => match.data)).toContainEqual( |
| 114 | expect.objectContaining({ name: "GET", kind: "server" }), |
| 115 | ); |
| 116 | |
| 117 | // Metrics export as OTLP protobuf (Axiom rejects JSON on /v1/metrics |
| 118 | // with 415) and route with X-Axiom-Metrics-Dataset. Metrics datasets |
| 119 | // are queried with MPL on the dataset's edge, not APL. |
| 120 | const workCount = yield* queryWorkCount(metrics.edgeDeploymentUrl).pipe( |
| 121 | Effect.repeat({ |
| 122 | schedule: Schedule.spaced("5 seconds"), |
| 123 | until: (count) => count > 0, |
| 124 | times: 36, |
| 125 | }), |
| 126 | ); |
| 127 | expect(workCount).toBeGreaterThan(0); |
| 128 | |
| 129 | yield* stack.destroy(); |
| 130 | }), |
| 131 | { |
| 132 | tags: [ |
| 133 | "provider:axiom", |
| 134 | "provider:axiom:apitoken", |
| 135 | "provider:axiom:dataset", |
| 136 | "provider:cloudflare", |
| 137 | "provider:cloudflare:worker", |
| 138 | "live", |
| 139 | ], |
| 140 | timeout: 600_000, |
| 141 | }, |
| 142 | ); |