@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, 04:45 UTC

main
ts 310 lines 11.2 KB
Raw
1 import * as ApplicationSignals from "@alchemy.run/aws/ApplicationSignals";
2 import * as Lambda from "@alchemy.run/aws/Lambda";
3 import type * as appsignals from "@distilled.cloud/aws/application-signals";
4 import * as Effect from "effect/Effect";
5 import { HttpServerRequest } from "effect/http/HttpServerRequest";
6 import * as HttpServerResponse from "effect/http/HttpServerResponse";
7 import * as Layer from "effect/Layer";
8 import path from "pathe";
9
10 const main = path.resolve(import.meta.dirname, "handler.ts");
11
12 // KeyAttributes of a service that does NOT exist — the discovery reads are
13 // account-scoped time-range queries that return well-formed empty results
14 // for unknown services, which is exactly what a fresh test account has.
15 const PROBE_KEY_ATTRIBUTES = {
16 Type: "Service",
17 Name: "alchemy-test-appsignals-bindings-probe",
18 Environment: "alchemy-test-env",
19 };
20
21 // A period-based SLI over an arbitrary CloudWatch metric (the metric does
22 // not need to exist for the SLO to be valid).
23 const SLI_CONFIG: appsignals.ServiceLevelIndicatorConfig = {
24 SliMetricConfig: {
25 MetricDataQueries: [
26 {
27 Id: "m1",
28 MetricStat: {
29 Metric: {
30 Namespace: "Alchemy/Test",
31 MetricName: "AppSignalsBindingsLatency",
32 Dimensions: [{ Name: "Fixture", Value: "bindings" }],
33 },
34 Period: 60,
35 Stat: "Average",
36 },
37 ReturnData: true,
38 },
39 ],
40 },
41 MetricThreshold: 2000,
42 ComparisonOperator: "LessThanOrEqualTo",
43 };
44
45 export class ApplicationSignalsTestFunction extends Lambda.Function<Lambda.Function>()(
46 "ApplicationSignalsTestFunction",
47 ) {}
48
49 export default ApplicationSignalsTestFunction.make(
50 {
51 main,
52 functionUrl: true,
53 },
54 Effect.gen(function* () {
55 const slo = yield* ApplicationSignals.ServiceLevelObjective("BindingsSlo", {
56 description: "alchemy application-signals bindings fixture slo",
57 sliConfig: SLI_CONFIG,
58 goal: {
59 Interval: { RollingInterval: { DurationUnit: "DAY", Duration: 7 } },
60 AttainmentGoal: 99,
61 WarningThreshold: 50,
62 },
63 });
64 const probe = yield* ApplicationSignals.InstrumentationConfiguration("BindingsProbe", {
65 instrumentationType: "PROBE",
66 service: "alchemy-test-appsignals-bindings-ic",
67 environment: "alchemy-test-env",
68 signalType: "SNAPSHOT",
69 location: {
70 Language: "Python",
71 CodeUnit: "app.main",
72 MethodName: "handler",
73 FilePath: "app/main.py",
74 LineNumber: 10,
75 },
76 captureConfiguration: {
77 CaptureLocals: ["x"],
78 CaptureLimits: { MaxHits: 1 },
79 },
80 });
81
82 // --- account-level discovery bindings ---
83 const listServices = yield* ApplicationSignals.ListServices();
84 const getService = yield* ApplicationSignals.GetService();
85 const listServiceDependencies = yield* ApplicationSignals.ListServiceDependencies();
86 const listServiceDependents = yield* ApplicationSignals.ListServiceDependents();
87 const listServiceOperations = yield* ApplicationSignals.ListServiceOperations();
88 const listServiceStates = yield* ApplicationSignals.ListServiceStates();
89 const listEntityEvents = yield* ApplicationSignals.ListEntityEvents();
90 const listAuditFindings = yield* ApplicationSignals.ListAuditFindings();
91
92 // --- SLO-scoped bindings ---
93 const listSlos = yield* ApplicationSignals.ListServiceLevelObjectives();
94 const getSlo = yield* ApplicationSignals.GetServiceLevelObjective(slo);
95 const getBudgetReport =
96 yield* ApplicationSignals.BatchGetServiceLevelObjectiveBudgetReport(slo);
97 const listExclusionWindows =
98 yield* ApplicationSignals.ListServiceLevelObjectiveExclusionWindows(slo);
99 const updateExclusionWindows = yield* ApplicationSignals.BatchUpdateExclusionWindows(slo);
100
101 // --- instrumentation-configuration-scoped binding ---
102 const getInstrumentationStatus =
103 yield* ApplicationSignals.GetInstrumentationConfigurationStatus(probe);
104
105 const bound = {
106 listServices,
107 getService,
108 listServiceDependencies,
109 listServiceDependents,
110 listServiceOperations,
111 listServiceStates,
112 listEntityEvents,
113 listAuditFindings,
114 listSlos,
115 getSlo,
116 getBudgetReport,
117 listExclusionWindows,
118 updateExclusionWindows,
119 getInstrumentationStatus,
120 };
121
122 const window = () => {
123 const EndTime = new Date();
124 const StartTime = new Date(EndTime.getTime() - 3600_000);
125 return { StartTime, EndTime };
126 };
127
128 return {
129 fetch: Effect.gen(function* () {
130 const request = yield* HttpServerRequest;
131 const url = new URL(request.originalUrl);
132 const pathname = url.pathname;
133
134 if (request.method === "GET" && pathname === "/bindings") {
135 return yield* HttpServerResponse.json({
136 bound: Object.keys(bound),
137 });
138 }
139
140 if (request.method === "GET" && pathname === "/services") {
141 const result = yield* listServices(window());
142 return yield* HttpServerResponse.json({
143 count: result.ServiceSummaries.length,
144 });
145 }
146
147 if (request.method === "GET" && pathname === "/service") {
148 const result = yield* getService({
149 ...window(),
150 KeyAttributes: PROBE_KEY_ATTRIBUTES,
151 });
152 // An unknown service returns an empty Service object.
153 return yield* HttpServerResponse.json({
154 discovered: result.Service.KeyAttributes !== undefined,
155 });
156 }
157
158 if (request.method === "GET" && pathname === "/dependencies") {
159 const result = yield* listServiceDependencies({
160 ...window(),
161 KeyAttributes: PROBE_KEY_ATTRIBUTES,
162 });
163 return yield* HttpServerResponse.json({
164 count: result.ServiceDependencies.length,
165 });
166 }
167
168 if (request.method === "GET" && pathname === "/dependents") {
169 const result = yield* listServiceDependents({
170 ...window(),
171 KeyAttributes: PROBE_KEY_ATTRIBUTES,
172 });
173 return yield* HttpServerResponse.json({
174 count: result.ServiceDependents.length,
175 });
176 }
177
178 if (request.method === "GET" && pathname === "/operations") {
179 const result = yield* listServiceOperations({
180 ...window(),
181 KeyAttributes: PROBE_KEY_ATTRIBUTES,
182 });
183 return yield* HttpServerResponse.json({
184 count: result.ServiceOperations.length,
185 });
186 }
187
188 if (request.method === "GET" && pathname === "/states") {
189 const result = yield* listServiceStates(window());
190 return yield* HttpServerResponse.json({
191 count: result.ServiceStates.length,
192 });
193 }
194
195 if (request.method === "GET" && pathname === "/entity-events") {
196 const result = yield* listEntityEvents({
197 ...window(),
198 Entity: PROBE_KEY_ATTRIBUTES,
199 });
200 return yield* HttpServerResponse.json({
201 count: result.ChangeEvents.length,
202 });
203 }
204
205 if (request.method === "GET" && pathname === "/audit-findings") {
206 const result = yield* listAuditFindings({
207 ...window(),
208 AuditTargets: [{ Type: "service", Data: { Service: PROBE_KEY_ATTRIBUTES } }],
209 });
210 return yield* HttpServerResponse.json({
211 count: result.AuditFindings.length,
212 });
213 }
214
215 if (request.method === "GET" && pathname === "/slos") {
216 // The fixture SLO must be among the account's SLOs.
217 const result = yield* listSlos({});
218 const summaries = result.SloSummaries ?? [];
219 return yield* HttpServerResponse.json({
220 count: summaries.length,
221 names: summaries.map((summary) => summary.Name),
222 });
223 }
224
225 if (request.method === "GET" && pathname === "/slo") {
226 const result = yield* getSlo();
227 return yield* HttpServerResponse.json({
228 name: result.Slo.Name,
229 arn: result.Slo.Arn,
230 attainmentGoal: result.Slo.Goal.AttainmentGoal,
231 });
232 }
233
234 if (request.method === "GET" && pathname === "/budget-report") {
235 const result = yield* getBudgetReport({ Timestamp: new Date() });
236 return yield* HttpServerResponse.json({
237 reports: result.Reports.length,
238 errors: result.Errors.length,
239 budgetStatus: result.Reports[0]?.BudgetStatus ?? null,
240 });
241 }
242
243 if (request.method === "GET" && pathname === "/exclusion-windows") {
244 const result = yield* listExclusionWindows();
245 return yield* HttpServerResponse.json({
246 count: result.ExclusionWindows.length,
247 });
248 }
249
250 if (request.method === "POST" && pathname === "/exclusion-windows") {
251 // Add a maintenance window, observe it, then remove it again so
252 // the fixture SLO stays clean between requests.
253 const exclusionWindow: appsignals.ExclusionWindow = {
254 StartTime: new Date(Date.now() + 3600_000),
255 Window: { DurationUnit: "HOUR", Duration: 1 },
256 Reason: "alchemy bindings test",
257 };
258 const added = yield* updateExclusionWindows({
259 AddExclusionWindows: [exclusionWindow],
260 });
261 const afterAdd = yield* listExclusionWindows();
262 const removed = yield* updateExclusionWindows({
263 RemoveExclusionWindows: [exclusionWindow],
264 });
265 const afterRemove = yield* listExclusionWindows();
266 return yield* HttpServerResponse.json({
267 addErrors: added.Errors.length,
268 afterAdd: afterAdd.ExclusionWindows.length,
269 removeErrors: removed.Errors.length,
270 afterRemove: afterRemove.ExclusionWindows.length,
271 });
272 }
273
274 if (request.method === "GET" && pathname === "/ic-status") {
275 // No SDK agent is attached to the synthetic service, so the
276 // status history is empty — the call itself proves the identity
277 // injection and the IAM grant.
278 const result = yield* getInstrumentationStatus();
279 return yield* HttpServerResponse.json({
280 events: result.Events.length,
281 });
282 }
283
284 return yield* HttpServerResponse.json(
285 { error: "Not found", method: request.method, pathname },
286 { status: 404 },
287 );
288 }).pipe(Effect.orDie),
289 };
290 }).pipe(
291 Effect.provide(
292 Layer.mergeAll(
293 ApplicationSignals.ListServicesHttp,
294 ApplicationSignals.GetServiceHttp,
295 ApplicationSignals.ListServiceDependenciesHttp,
296 ApplicationSignals.ListServiceDependentsHttp,
297 ApplicationSignals.ListServiceOperationsHttp,
298 ApplicationSignals.ListServiceStatesHttp,
299 ApplicationSignals.ListEntityEventsHttp,
300 ApplicationSignals.ListAuditFindingsHttp,
301 ApplicationSignals.ListServiceLevelObjectivesHttp,
302 ApplicationSignals.GetServiceLevelObjectiveHttp,
303 ApplicationSignals.BatchGetServiceLevelObjectiveBudgetReportHttp,
304 ApplicationSignals.ListServiceLevelObjectiveExclusionWindowsHttp,
305 ApplicationSignals.BatchUpdateExclusionWindowsHttp,
306 ApplicationSignals.GetInstrumentationConfigurationStatusHttp,
307 ),
308 ),
309 ),
310 );