@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 362 lines 12.8 KB
Raw
1 import * as amp from "@distilled.cloud/aws/amp";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import * as Provider from "alchemy/Provider";
5 import { Resource } from "alchemy/Resource";
6 import { createInternalTags, hasAlchemyTags } from "alchemy/Tags";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10 import * as Stream from "effect/Stream";
11 import type { Providers } from "../Providers.ts";
12 import { decodeDefinition, encodeDefinition, syncAmpTags, toTagRecord } from "./internal.ts";
13
14 export class ScraperNotReady extends Data.TaggedError("ScraperNotReady")<{
15 readonly message: string;
16 readonly cause?: unknown;
17 }> {}
18
19 export interface ScraperEksSource {
20 /**
21 * ARN of the Amazon EKS cluster the scraper collects metrics from.
22 */
23 clusterArn: string;
24 /**
25 * IDs of the subnets the scraper's network interfaces are placed in. Must
26 * be subnets of the cluster's VPC.
27 */
28 subnetIds: string[];
29 /**
30 * IDs of the security groups applied to the scraper's network interfaces.
31 * If omitted, the cluster's default security group is used.
32 */
33 securityGroupIds?: string[];
34 }
35
36 export interface ScraperVpcSource {
37 /**
38 * IDs of the subnets the scraper's network interfaces are placed in.
39 */
40 subnetIds: string[];
41 /**
42 * IDs of the security groups applied to the scraper's network interfaces.
43 */
44 securityGroupIds: string[];
45 }
46
47 /**
48 * Where the scraper collects metrics from — an Amazon EKS cluster or a
49 * VPC-based source (Amazon MSK, self-managed Kubernetes, or any
50 * Prometheus-compatible endpoint discoverable via DNS in the VPC).
51 */
52 export type ScraperSource =
53 | { eksConfiguration: ScraperEksSource }
54 | { vpcConfiguration: ScraperVpcSource };
55
56 export interface ScraperRoleConfiguration {
57 /**
58 * ARN of the role used by the scraper to discover and collect metrics on
59 * your behalf (cross-account collection).
60 */
61 sourceRoleArn?: string;
62 /**
63 * ARN of the role used by the scraper to write to the destination
64 * workspace (cross-account collection).
65 */
66 targetRoleArn?: string;
67 }
68
69 export interface ScraperProps {
70 /**
71 * A human-readable alias for the scraper. Aliases are not unique.
72 * Updating the alias is an in-place update.
73 */
74 alias?: string;
75 /**
76 * The scraper's configuration as Prometheus scrape-configuration YAML
77 * text. It is base64-blob encoded on the wire automatically. Use the
78 * {@link GetDefaultScraperConfiguration} binding (or the
79 * `GetDefaultScraperConfiguration` API) to fetch the AWS-managed default
80 * as a starting point. Updating the configuration is an in-place update.
81 */
82 scrapeConfiguration: string;
83 /**
84 * Where the scraper collects metrics from. The source is immutable —
85 * changing it replaces the scraper.
86 */
87 source: ScraperSource;
88 /**
89 * ARN of the Amazon Managed Service for Prometheus workspace the scraped
90 * metrics are written to. Updating the destination is an in-place update.
91 */
92 destinationWorkspaceArn: string;
93 /**
94 * Cross-account role configuration for the scraper. If omitted, the
95 * scraper collects and writes within the current account using the
96 * service-created role.
97 */
98 roleConfiguration?: ScraperRoleConfiguration;
99 /**
100 * User-defined tags for the scraper.
101 */
102 tags?: Record<string, string>;
103 }
104
105 export interface Scraper extends Resource<
106 "AWS.AMP.Scraper",
107 ScraperProps,
108 {
109 scraperId: string;
110 scraperArn: string;
111 roleArn: string;
112 alias: string | undefined;
113 status: string;
114 },
115 never,
116 Providers
117 > {}
118
119 /**
120 * An Amazon Managed Service for Prometheus scraper — a fully-managed,
121 * agentless collector that pulls metrics from an Amazon EKS cluster (or a
122 * VPC-based Prometheus-compatible source) and remote-writes them into an AMP
123 * workspace.
124 *
125 * Scraper provisioning is slow (the service creates network interfaces and
126 * an IAM role; expect several minutes to reach `ACTIVE`).
127 *
128 * ### Creating a Scraper
129 * **Example:** Scrape an EKS Cluster into a Workspace
130 * ```typescript
131 * const workspace = yield* AMP.Workspace("Metrics", {});
132 * const scraper = yield* AMP.Scraper("ClusterScraper", {
133 * alias: "eks-metrics",
134 * scrapeConfiguration: defaultScrapeConfigYaml,
135 * source: {
136 * eksConfiguration: {
137 * clusterArn: cluster.clusterArn,
138 * subnetIds: [subnetA.subnetId, subnetB.subnetId],
139 * },
140 * },
141 * destinationWorkspaceArn: workspace.workspaceArn,
142 * });
143 * ```
144 *
145 * **Example:** Scrape a VPC-Based Source
146 * ```typescript
147 * const scraper = yield* AMP.Scraper("VpcScraper", {
148 * scrapeConfiguration: scrapeConfigYaml,
149 * source: {
150 * vpcConfiguration: {
151 * subnetIds: [subnet.subnetId],
152 * securityGroupIds: [securityGroup.securityGroupId],
153 * },
154 * },
155 * destinationWorkspaceArn: workspace.workspaceArn,
156 * });
157 * ```
158 *
159 * @resource
160 */
161 export const Scraper = Resource<Scraper>("AWS.AMP.Scraper");
162
163 /** Canonical JSON of a scraper source for observed-vs-desired comparison. */
164 const canonicalSource = (source: ScraperSource): string => {
165 const canonical = (config: {
166 clusterArn?: string;
167 subnetIds: string[];
168 securityGroupIds?: string[];
169 }) => ({
170 clusterArn: config.clusterArn ?? null,
171 subnetIds: [...config.subnetIds].sort(),
172 securityGroupIds: [...(config.securityGroupIds ?? [])].sort(),
173 });
174 return "eksConfiguration" in source
175 ? JSON.stringify({ eks: canonical(source.eksConfiguration) })
176 : JSON.stringify({ vpc: canonical(source.vpcConfiguration) });
177 };
178
179 export const ScraperProvider = () =>
180 Provider.effect(
181 Scraper,
182 Effect.gen(function* () {
183 const toAttrs = (scraper: {
184 scraperId: string;
185 arn: string;
186 roleArn: string;
187 alias?: string;
188 status: amp.ScraperStatus;
189 }) => ({
190 scraperId: scraper.scraperId,
191 scraperArn: scraper.arn,
192 roleArn: scraper.roleArn,
193 alias: scraper.alias,
194 status: scraper.status.statusCode,
195 });
196
197 /** Describe a scraper by id; typed not-found → undefined. */
198 const describe = Effect.fn(function* (scraperId: string) {
199 const response = yield* amp
200 .describeScraper({ scraperId })
201 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
202 return response?.scraper;
203 });
204
205 const toWireSource = (source: ScraperSource): amp.Source =>
206 "eksConfiguration" in source
207 ? {
208 eksConfiguration: {
209 clusterArn: source.eksConfiguration.clusterArn,
210 subnetIds: source.eksConfiguration.subnetIds,
211 securityGroupIds: source.eksConfiguration.securityGroupIds,
212 },
213 }
214 : {
215 vpcConfiguration: {
216 subnetIds: source.vpcConfiguration.subnetIds,
217 securityGroupIds: source.vpcConfiguration.securityGroupIds,
218 },
219 };
220
221 /**
222 * Poll until the scraper leaves CREATING/UPDATING. Scraper
223 * provisioning creates network interfaces in each subnet and can take
224 * many minutes — the wait is bounded at 25 minutes and fails fast on a
225 * *_FAILED terminal status.
226 */
227 const waitActive = Effect.fn(function* (scraperId: string) {
228 const scraper = yield* amp.describeScraper({ scraperId }).pipe(
229 Effect.map((r) => r.scraper),
230 Effect.repeat({
231 schedule: Schedule.max([Schedule.fixed("15 seconds"), Schedule.recurs(100)]),
232 until: (s): boolean =>
233 s.status.statusCode !== "CREATING" && s.status.statusCode !== "UPDATING",
234 }),
235 );
236 if (scraper.status.statusCode !== "ACTIVE") {
237 return yield* new ScraperNotReady({
238 message: `AMP scraper ${scraperId} did not become ACTIVE (status: ${scraper.status.statusCode}${
239 scraper.statusReason ? `: ${scraper.statusReason}` : ""
240 })`,
241 });
242 }
243 return scraper;
244 });
245
246 return {
247 stables: ["scraperId", "scraperArn", "roleArn"],
248
249 diff: Effect.fn(function* ({ olds, news }) {
250 if (!isResolved(news)) return undefined;
251 // The metric source is immutable — a change replaces the scraper.
252 if (olds !== undefined && canonicalSource(olds.source) !== canonicalSource(news.source)) {
253 return { action: "replace" } as const;
254 }
255 }),
256
257 read: Effect.fn(function* ({ id, output }) {
258 // Scraper ids are server-assigned; without an output cache there
259 // is no deterministic identity to look up.
260 if (!output?.scraperId) return undefined;
261 const scraper = yield* describe(output.scraperId);
262 if (scraper === undefined) return undefined;
263 const attrs = toAttrs(scraper);
264 const tags = toTagRecord(scraper.tags);
265 return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs);
266 }),
267
268 reconcile: Effect.fn(function* ({ id, news, output, session }) {
269 const internalTags = yield* createInternalTags(id);
270 const desiredTags = { ...internalTags, ...news!.tags };
271 const desiredConfig = news!.scrapeConfiguration;
272
273 // 1. Observe — cloud state is authoritative; output is an id cache.
274 let scraper =
275 output?.scraperId !== undefined ? yield* describe(output.scraperId) : undefined;
276
277 // 2. Ensure — create if missing, then wait for ACTIVE.
278 if (scraper === undefined) {
279 const created = yield* amp.createScraper({
280 alias: news!.alias,
281 scrapeConfiguration: {
282 configurationBlob: yield* encodeDefinition(desiredConfig),
283 },
284 source: toWireSource(news!.source),
285 destination: {
286 ampConfiguration: {
287 workspaceArn: news!.destinationWorkspaceArn,
288 },
289 },
290 roleConfiguration: news!.roleConfiguration,
291 tags: desiredTags,
292 });
293 scraper = yield* waitActive(created.scraperId);
294 }
295
296 const scraperId = scraper.scraperId;
297
298 // 3. Sync mutable aspects — diff OBSERVED state against desired
299 // and issue a single update only when something drifted.
300 const observedConfig = yield* decodeDefinition(
301 scraper.scrapeConfiguration.configurationBlob,
302 );
303 const observedDestination = scraper.destination.ampConfiguration?.workspaceArn;
304 const aliasDrifts = (news!.alias ?? undefined) !== (scraper.alias ?? undefined);
305 const configDrifts = observedConfig !== desiredConfig;
306 const destinationDrifts = observedDestination !== news!.destinationWorkspaceArn;
307 const roleDrifts =
308 news!.roleConfiguration !== undefined &&
309 ((news!.roleConfiguration.sourceRoleArn ?? undefined) !==
310 (scraper.roleConfiguration?.sourceRoleArn ?? undefined) ||
311 (news!.roleConfiguration.targetRoleArn ?? undefined) !==
312 (scraper.roleConfiguration?.targetRoleArn ?? undefined));
313
314 if (aliasDrifts || configDrifts || destinationDrifts || roleDrifts) {
315 yield* amp.updateScraper({
316 scraperId,
317 alias: news!.alias,
318 scrapeConfiguration: configDrifts
319 ? {
320 configurationBlob: yield* encodeDefinition(desiredConfig),
321 }
322 : undefined,
323 destination: destinationDrifts
324 ? {
325 ampConfiguration: {
326 workspaceArn: news!.destinationWorkspaceArn,
327 },
328 }
329 : undefined,
330 roleConfiguration: roleDrifts ? news!.roleConfiguration : undefined,
331 });
332 scraper = yield* waitActive(scraperId);
333 }
334
335 // 3b. Sync tags — diff against OBSERVED cloud tags.
336 yield* syncAmpTags(scraper.arn, desiredTags);
337
338 yield* session.note(scraperId);
339 return toAttrs(scraper);
340 }),
341
342 delete: Effect.fn(function* ({ output }) {
343 yield* amp.deleteScraper({ scraperId: output.scraperId }).pipe(
344 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
345 // A scraper mid-transition rejects deletion; retry briefly.
346 Effect.retry({
347 while: (e) => e._tag === "ConflictException",
348 schedule: Schedule.max([Schedule.fixed("10 seconds"), Schedule.recurs(30)]),
349 }),
350 );
351 }),
352
353 list: () =>
354 amp.listScrapers.pages({}).pipe(
355 Stream.runCollect,
356 Effect.map((chunk) =>
357 Array.from(chunk).flatMap((page) => page.scrapers.map((summary) => toAttrs(summary))),
358 ),
359 ),
360 };
361 }),
362 );