@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 118 lines 4.84 KB
Raw
1 import * as Lambda from "@alchemy.run/aws/Lambda";
2 import * as Telemetry from "alchemy/Telemetry";
3 import * as Config from "effect/Config";
4 import * as Duration from "effect/Duration";
5 import * as Effect from "effect/Effect";
6 import { HttpServerRequest } from "effect/http/HttpServerRequest";
7 import * as HttpServerResponse from "effect/http/HttpServerResponse";
8 import * as Layer from "effect/Layer";
9
10 /**
11 * Lambda fixture for Telemetry.test.ts: exercises the `Telemetry.layerOtlp`
12 * binding layer. Building it at deploy time binds the collector url
13 * (resolved from the deployer's `COLLECTOR_URL` config, provided by the
14 * test's ConfigProvider override) and the service name onto the Function;
15 * at runtime the exporter reads them back per invocation.
16 *
17 * `GET /work` runs a child span and a log so the test can assert traces AND
18 * logs arrive at the collector after the invocation scope flushes.
19 *
20 * The function times out after 3 s, with a 2 s margin (`timeoutMargin`), so
21 * Lambda itself is configured for 5 s.
22 *
23 * `GET /slow` outlives the timeout inside a child span. At 3 s the runtime
24 * interrupts it: the finalizer runs and logs, the child span ends with the
25 * interruption, and the root span ends with
26 * `AWS.Lambda.InvocationTimeoutError`. The invocation is then held until
27 * Lambda's 5 s timeout, which Lambda reports as a timeout and which shuts
28 * the sandbox down.
29 *
30 * `GET /late` finishes just inside the timeout. It is never interrupted.
31 *
32 * `GET /stuck` outlives the timeout and its finalizer hangs past Lambda's own
33 * timeout. The backstop flush ships the root span — marked
34 * `aws.lambda.timeout.imminent` — before Lambda kills the process.
35 */
36 export class OtelTestFunction extends Lambda.Function<Lambda.Function>()("OtelTelemetryFunction") {}
37
38 export const OtelTestFunctionLive = OtelTestFunction.make(
39 {
40 main: import.meta.url,
41 functionUrl: true,
42 timeout: Duration.seconds(3),
43 timeoutMargin: Duration.seconds(2),
44 },
45 Effect.gen(function* () {
46 const doWork = Effect.fn("lambda.child-span")(function* () {
47 yield* Effect.log("lambda-work-log");
48 return "lambda-did-work";
49 });
50 // The call that hangs: interrupted at the timeout, it ends its span and
51 // runs its finalizer.
52 const doSlowWork = Effect.fn("lambda.slow-span")(function* () {
53 yield* Effect.log("lambda-slow-log");
54 yield* Effect.sleep("60 seconds").pipe(Effect.ensuring(Effect.log("lambda-slow-finalizer")));
55 return "never";
56 });
57 const doStuckWork = Effect.never.pipe(
58 Effect.ensuring(Effect.sleep("60 seconds")),
59 Effect.as("never"),
60 );
61
62 return {
63 fetch: Effect.gen(function* () {
64 const request = yield* HttpServerRequest;
65 const url = new URL(request.url, "http://x");
66 if (url.pathname === "/work") {
67 const marker = yield* doWork();
68 return yield* HttpServerResponse.json({ marker });
69 }
70 if (url.pathname === "/late") {
71 yield* Effect.sleep("2500 millis");
72 return yield* HttpServerResponse.json({ marker: "lambda-late-done" });
73 }
74 if (url.pathname === "/slow") {
75 const marker = yield* doSlowWork();
76 return yield* HttpServerResponse.json({ marker });
77 }
78 if (url.pathname === "/stuck") {
79 const marker = yield* doStuckWork;
80 return yield* HttpServerResponse.json({ marker });
81 }
82 // Readiness gate for the test: the collector's workers.dev URL
83 // propagates per-PoP, so it can serve placeholder 404s to the
84 // Lambda's region long after the test machine sees it. The test
85 // polls this route until it reports 200 before asserting on
86 // exported telemetry.
87 if (url.pathname === "/probe") {
88 const endpoint = yield* Config.String("COLLECTOR_URL").pipe(Effect.orDie);
89 const result = yield* Effect.tryPromise(() =>
90 fetch(`${endpoint}/v1/probe`, {
91 method: "POST",
92 body: JSON.stringify({ probe: true }),
93 }).then(async (r) => ({
94 status: r.status,
95 body: (await r.text()).slice(0, 200),
96 })),
97 ).pipe(Effect.catchCause((cause) => Effect.succeed({ status: -1, body: String(cause) })));
98 return yield* HttpServerResponse.json(result);
99 }
100 return HttpServerResponse.text("otel-lambda-ok");
101 }),
102 };
103 }).pipe(
104 // The telemetry binding layer, composed like any other binding: the
105 // collector url resolves once at deploy time and binds onto the
106 // Function; nothing telemetry-specific appears in handler code.
107 Effect.provide(
108 Layer.unwrap(
109 Effect.gen(function* () {
110 const url = yield* Config.String("COLLECTOR_URL");
111 return Telemetry.layerOtlp({ url, serviceName: "otel-lambda-test" });
112 }),
113 ),
114 ),
115 ),
116 );
117
118 export default OtelTestFunctionLive;