@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 238 lines 10.5 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
3 import { describe, expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as workers from "@distilled.cloud/cloudflare/workers";
6 import * as ConfigProvider from "effect/ConfigProvider";
7 import * as Effect from "effect/Effect";
8 import * as HttpClient from "effect/http/HttpClient";
9 import * as Schedule from "effect/Schedule";
10 import * as Schema from "effect/Schema";
11 import * as pathe from "pathe";
12 import { stageSuffix } from "../../names.ts";
13 import { expectUrlContains } from "../Utils/Http.ts";
14 import type { OtelSink } from "./fixtures/otel-collector-worker.ts";
15 import OtelCustomWorker from "./fixtures/otel-custom-worker.ts";
16 import OtelEventFlushWorker from "./fixtures/otel-event-flush-worker.ts";
17 import OtelTracedWorker from "./fixtures/otel-traced-worker.ts";
18
19 const { test } = Test.make({ providers: Cloudflare.providers() });
20
21 const collectorMain = pathe.resolve(import.meta.dirname, "fixtures/otel-collector-worker.ts");
22
23 // The collector must be reachable *from another Worker*: same-account
24 // worker-to-worker fetches over workers.dev are blocked (error 1042), so
25 // the collector gets a deterministic custom hostname on the standing test
26 // zone instead.
27 const zoneName = process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us";
28 const collectorHost = `otel-collector-${stageSuffix()}.${zoneName}`;
29
30 // The OTLP sink both traced fixtures export to. Declared identically in
31 // both deploy steps so the second deploy keeps it.
32 const collectorWorker = () =>
33 Cloudflare.Worker("OtelCollector", {
34 main: collectorMain,
35 env: { SINK: Cloudflare.DurableObject<OtelSink>("OtelSink") },
36 domain: collectorHost,
37 compatibility: { date: "2024-09-23" },
38 });
39
40 /** The trace spans in the collector's `/collected` dump; other fields are stripped. */
41 const Collected = Schema.Struct({
42 items: Schema.Array(
43 Schema.Struct({
44 payload: Schema.Struct({
45 resourceSpans: Schema.optional(
46 Schema.Array(
47 Schema.Struct({
48 scopeSpans: Schema.Array(
49 Schema.Struct({
50 spans: Schema.Array(
51 Schema.Struct({
52 traceId: Schema.String,
53 spanId: Schema.String,
54 parentSpanId: Schema.optional(Schema.String),
55 name: Schema.String,
56 }),
57 ),
58 }),
59 ),
60 }),
61 ),
62 ),
63 }),
64 }),
65 ),
66 });
67
68 describe(
69 "Cloudflare Worker Telemetry",
70 { tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"] },
71 () => {
72 test.provider(
73 "OTLP telemetry exports per request (otlp binding + custom Layer)",
74 (stack) =>
75 Effect.gen(function* () {
76 yield* stack.destroy();
77
78 // Deploy the collector first: the traced fixtures resolve
79 // `COLLECTOR_URL` from the deployer environment at
80 // deploy time, so its URL must exist before they deploy.
81 const collector = yield* stack.deploy(
82 Effect.gen(function* () {
83 return yield* collectorWorker();
84 }),
85 );
86 // Exporters (running inside the traced Workers) push to the custom
87 // domain; the test reads back over workers.dev, which is reachable
88 // from outside but blocked worker-to-worker (error 1042). With a
89 // custom domain attached, `collector.url` is the domain, so derive
90 // the workers.dev URL from the account subdomain.
91 const { accountId } = yield* yield* CloudflareEnvironment;
92 const { subdomain } = yield* workers.getSubdomain({ accountId });
93 const collectorUrl = `https://${collectorHost}`;
94 const collected = `https://${collector.workerName}.${subdomain}.workers.dev/collected`;
95
96 yield* expectUrlContains(collected, "otel-collector-ok", { timeout: "180 seconds" });
97
98 // The traced fixtures read the collector URL via `Config` at deploy
99 // time. The test harness snapshots its ConfigProvider before the
100 // test body runs, so layer the just-learned collector URL on top of
101 // the current provider for this deploy only.
102 const currentConfig = yield* ConfigProvider.ConfigProvider;
103 const { traced, custom, flush } = yield* stack
104 .deploy(
105 Effect.gen(function* () {
106 yield* collectorWorker();
107 const traced = yield* OtelTracedWorker;
108 const custom = yield* OtelCustomWorker;
109 const flush = yield* OtelEventFlushWorker;
110 return { traced, custom, flush };
111 }),
112 )
113 .pipe(
114 Effect.provideService(
115 ConfigProvider.ConfigProvider,
116 ConfigProvider.orElse(
117 ConfigProvider.fromUnknown({
118 COLLECTOR_URL: collectorUrl,
119 OTLP_EVENT_FLUSH_URL: `${collectorUrl}/v1/traces`,
120 }),
121 currentConfig,
122 ),
123 ),
124 );
125 const tracedUrl = traced.url as string;
126 const customUrl = custom.url as string;
127
128 // Wait for the traced workers to serve (fresh workers.dev
129 // subdomains can take minutes to start routing), then for the
130 // collector's custom domain to be reachable from *inside* a Worker
131 // (DNS + edge certificate issuance).
132 yield* expectUrlContains(`${tracedUrl}/probe`, '"status":200', {
133 timeout: "240 seconds",
134 label: "collector reachable from a Worker",
135 });
136
137 // Drive one traced request on each worker now that exports can
138 // reach the collector.
139 yield* expectUrlContains(`${tracedUrl}/work`, "did-work", { timeout: "180 seconds" });
140 yield* expectUrlContains(`${customUrl}/work`, "custom-did-work", {
141 timeout: "180 seconds",
142 });
143
144 // The otlp binding-layer path: the request scope finalizer flushes
145 // the OTLP buffers via ctx.waitUntil, so everything the request
146 // produced arrives at the collector shortly after the response.
147 yield* expectUrlContains(collected, "otel-traced-test", {
148 timeout: "120 seconds",
149 label: "default-path service.name",
150 });
151 // HTTP server root span from the HttpMiddleware tracer, named
152 // after the request method.
153 yield* expectUrlContains(collected, '"name":"GET"');
154 // Child span from Effect.fn instrumentation.
155 yield* expectUrlContains(collected, "test.child-span");
156 // Log record shipped by the OTLP logger.
157 yield* expectUrlContains(collected, "did-work-log");
158 // Default resource attributes stamped by the exporter.
159 yield* expectUrlContains(collected, "alchemy.stack");
160 // Exporter composition: the fixture merges a second otlp layer
161 // whose traces url lands under /v1/second-traces — same spans,
162 // second destination.
163 yield* expectUrlContains(collected, "second-traces");
164
165 // Custom exporter path: Effect.provide(Telemetry.layer(...)) on
166 // the Worker init Effect replaces the built-in OTLP exporter.
167 yield* expectUrlContains(collected, "otel-custom-test", {
168 timeout: "120 seconds",
169 label: "custom-path service.name",
170 });
171 yield* expectUrlContains(collected, "custom.child-span");
172 yield* expectUrlContains(collected, "custom-work-log");
173
174 // Durable Object event paths on deployed workerd: the Worker calls
175 // its DO over HTTP and over RPC; each DO event is its own telemetry
176 // scope. `state.waitUntil` is a no-op in DOs, so DO batches only
177 // arrive if the DurableObjectBridge's flush actually completes
178 // before/despite the event's I/O context winding down.
179 const flushUrl = flush.url as string;
180 yield* expectUrlContains(`${flushUrl}/`, "worker-saw:durable-object-ok", {
181 timeout: "240 seconds",
182 label: "event-flush worker via DO fetch",
183 });
184 yield* expectUrlContains(`${flushUrl}/rpc`, "worker-saw:durable-object-rpc-ok", {
185 timeout: "120 seconds",
186 label: "event-flush worker via DO rpc",
187 });
188 // The DO fetch event's child span…
189 yield* expectUrlContains(collected, "otel-event-flush.child", {
190 timeout: "120 seconds",
191 label: "deployed DO fetch event batch",
192 });
193 // …and the DO RPC method event's child span both reach the
194 // collector.
195 yield* expectUrlContains(collected, "otel-event-flush.rpc", {
196 timeout: "120 seconds",
197 label: "deployed DO rpc event batch",
198 });
199 // The DO RPC event continues the caller's trace, parented on the
200 // Worker span that made the call, as the DO fetch event does. The
201 // Worker's own batch can land after the DO's, so poll for it.
202 const client = yield* HttpClient.HttpClient;
203 const spans = client.get(collected).pipe(
204 Effect.flatMap((response) => response.json),
205 Effect.flatMap(Schema.decodeUnknownEffect(Collected)),
206 Effect.map(({ items }) =>
207 items.flatMap(({ payload }) =>
208 (payload.resourceSpans ?? []).flatMap((resource) =>
209 resource.scopeSpans.flatMap((scope) => scope.spans),
210 ),
211 ),
212 ),
213 );
214 const rpcCallers = yield* spans.pipe(
215 Effect.map((all) =>
216 all
217 .filter((span) => span.name === "otel-event-flush.rpc")
218 .map(
219 (span) =>
220 all.find(
221 (caller) =>
222 caller.traceId === span.traceId && caller.spanId === span.parentSpanId,
223 )?.name,
224 ),
225 ),
226 Effect.repeat({
227 schedule: Schedule.spaced("2 seconds"),
228 until: (callers) => callers.length > 0 && callers.every((name) => name !== undefined),
229 times: 30,
230 }),
231 );
232 expect(rpcCallers.length).toBeGreaterThan(0);
233 expect(rpcCallers.every((name) => name === "otel-event-flush.worker")).toBe(true);
234 }),
235 { timeout: 600_000 },
236 );
237 },
238 );