| 1 | import * as Axiom from "@/Axiom"; |
| 2 | import * as Cloudflare from "@/Cloudflare/index.ts"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import { expect } from "alchemy-test"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import * as HttpClient from "effect/http/HttpClient"; |
| 9 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 10 | import { expectUrlContains } from "../Cloudflare/Utils/Http.ts"; |
| 11 | import AxiomTracedWorker, { |
| 12 | Ingest, |
| 13 | Logs, |
| 14 | TRACES_DATASET, |
| 15 | Traces, |
| 16 | } from "./fixtures/axiom-traced-worker.ts"; |
| 17 | |
| 18 | const { test } = Test.make({ |
| 19 | providers: Layer.mergeAll(Cloudflare.providers(), Axiom.providers()), |
| 20 | }); |
| 21 | |
| 22 | const hasAxiomCreds = !!(process.env.AXIOM_TOKEN || process.env.AXIOM_API_KEY); |
| 23 | |
| 24 | // Query recent trace data out-of-band with the deployer's org token. The |
| 25 | // worker ingests with its own least-privilege token; this read proves the |
| 26 | // data actually landed in Axiom. |
| 27 | const queryTraces = Effect.gen(function* () { |
| 28 | const response = yield* HttpClient.execute( |
| 29 | HttpClientRequest.post( |
| 30 | "https://api.axiom.co/v1/datasets/_apl?format=legacy", |
| 31 | ).pipe( |
| 32 | HttpClientRequest.setHeaders({ |
| 33 | Authorization: `Bearer ${process.env.AXIOM_TOKEN ?? process.env.AXIOM_API_KEY}`, |
| 34 | "Content-Type": "application/json", |
| 35 | }), |
| 36 | HttpClientRequest.bodyJsonUnsafe({ |
| 37 | apl: `['${TRACES_DATASET}'] | where _time > ago(10m) | limit 1000`, |
| 38 | }), |
| 39 | ), |
| 40 | ); |
| 41 | return yield* response.text; |
| 42 | }); |
| 43 | |
| 44 | test.provider.skipIf(!hasAxiomCreds)( |
| 45 | "Worker exports telemetry to Axiom via the Axiom.Telemetry binding layer", |
| 46 | (stack) => |
| 47 | Effect.gen(function* () { |
| 48 | yield* stack.destroy(); |
| 49 | |
| 50 | const { worker } = yield* stack.deploy( |
| 51 | Effect.gen(function* () { |
| 52 | yield* Traces; |
| 53 | yield* Logs; |
| 54 | yield* Ingest; |
| 55 | const worker = yield* AxiomTracedWorker; |
| 56 | return { worker }; |
| 57 | }), |
| 58 | ); |
| 59 | |
| 60 | // Drive one traced request (fresh workers.dev URLs take a few |
| 61 | // seconds to start serving). |
| 62 | const url = worker.url as string; |
| 63 | yield* expectUrlContains(`${url}/work`, "axiom-did-work", { |
| 64 | timeout: "240 seconds", |
| 65 | }); |
| 66 | |
| 67 | // The request scope's flush ships the trace via ctx.waitUntil; poll |
| 68 | // Axiom until it is queryable. |
| 69 | const body = yield* queryTraces.pipe( |
| 70 | Effect.repeat({ |
| 71 | schedule: Schedule.spaced("5 seconds"), |
| 72 | until: (text) => text.includes("otel-axiom-e2e"), |
| 73 | times: 36, |
| 74 | }), |
| 75 | ); |
| 76 | expect(body).toContain("otel-axiom-e2e"); |
| 77 | expect(body).toContain("axiom.child-span"); |
| 78 | expect(body).toContain("http.server GET"); |
| 79 | |
| 80 | yield* stack.destroy(); |
| 81 | }), |
| 82 | { |
| 83 | tags: [ |
| 84 | "provider:axiom", |
| 85 | "provider:axiom:apitoken", |
| 86 | "provider:axiom:dataset", |
| 87 | "provider:cloudflare", |
| 88 | "provider:cloudflare:worker", |
| 89 | "live", |
| 90 | ], |
| 91 | timeout: 600_000, |
| 92 | }, |
| 93 | ); |