1import * as Lambda from "@/AWS/Lambda";
2import * as RUM from "@/AWS/RUM";
3import * as Duration from "effect/Duration";
4import * as Effect from "effect/Effect";
5import * as Layer from "effect/Layer";
6import * as Result from "effect/Result";
7import { HttpServerRequest } from "effect/http/HttpServerRequest";
8import * as HttpServerResponse from "effect/http/HttpServerResponse";
9import path from "pathe";
10
11const main = path.resolve(import.meta.dirname, "handler.ts");
12
13export class RumBindingsFunction extends Lambda.Function<Lambda.Function>()(
14 "RumBindingsFunction",
15) {}
16
17export default RumBindingsFunction.make(
18 {
19 main,
20 functionUrl: true,
21 timeout: Duration.seconds(120),
22 },
23 Effect.gen(function* () {
24 const monitor = yield* RUM.AppMonitor("BindingsMonitor", {
25 domain: "example.com",
26 appMonitorConfiguration: {
27 sessionSampleRate: 1,
28 telemetries: ["errors", "performance"],
29 },
30 });
31
32 const bound = {
33 putRumEvents: yield* RUM.PutRumEvents(monitor),
34 getAppMonitorData: yield* RUM.GetAppMonitorData(monitor),
35 };
36
37 return {
38 fetch: Effect.gen(function* () {
39 const request = yield* HttpServerRequest;
40 const url = new URL(request.originalUrl);
41 const pathname = url.pathname;
42
43 if (request.method === "GET" && pathname === "/bindings") {
44 return yield* HttpServerResponse.json({
45 bound: Object.keys(bound),
46 });
47 }
48
49 // Data plane: send one synthetic session's events (the same call the
50 // RUM web client makes) — the monitor id and AppMonitorDetails are
51 // injected by the binding. A typed failure reports its tag so a
52 // grant gap is diagnosable from the JSON.
53 if (request.method === "POST" && pathname === "/events") {
54 const ids = yield* Effect.sync(() => ({
55 batch: crypto.randomUUID(),
56 event: crypto.randomUUID(),
57 session: crypto.randomUUID(),
58 user: crypto.randomUUID(),
59 }));
60 const result = yield* Effect.result(
61 bound.putRumEvents({
62 BatchId: ids.batch,
63 UserDetails: { userId: ids.user, sessionId: ids.session },
64 RumEvents: [
65 {
66 id: ids.event,
67 timestamp: new Date(),
68 type: "com.amazon.rum.session_start_event",
69 details: "{}",
70 },
71 ],
72 }),
73 );
74 return yield* HttpServerResponse.json(
75 Result.isSuccess(result)
76 ? { tag: "ok" }
77 : { tag: result.failure._tag, error: String(result.failure) },
78 );
79 }
80
81 // Read the trailing hour of collected events back out of the monitor.
82 if (request.method === "GET" && pathname === "/data") {
83 const now = yield* Effect.sync(() => Date.now());
84 const result = yield* Effect.result(
85 bound.getAppMonitorData({
86 TimeRange: { After: now - 3_600_000, Before: now },
87 }),
88 );
89 return yield* HttpServerResponse.json(
90 Result.isSuccess(result)
91 ? { tag: "ok", count: (result.success.Events ?? []).length }
92 : { tag: result.failure._tag, error: String(result.failure) },
93 );
94 }
95
96 return yield* HttpServerResponse.json(
97 { error: "Not found", method: request.method, pathname },
98 { status: 404 },
99 );
100 }).pipe(Effect.orDie),
101 };
102 }).pipe(
103 Effect.provide(
104 Layer.mergeAll(RUM.PutRumEventsHttp, RUM.GetAppMonitorDataHttp),
105 ),
106 ),
107);