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
| 1 | import * as amp from "@distilled.cloud/aws/amp"; |
| 2 | import { isResolved } from "alchemy/Diff"; |
| 3 | import * as Provider from "alchemy/Provider"; |
| 4 | import { Resource } from "alchemy/Resource"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Schedule from "effect/Schedule"; |
| 7 | import type { Providers } from "../Providers.ts"; |
| 8 | import { normalizeAmpLogGroupArn } from "./internal.ts"; |
| 9 | |
| 10 | export interface ScraperComponentConfig { |
| 11 | /** |
| 12 | * The scraper component the logging applies to (e.g. `"SERVICE_DISCOVERY"`, |
| 13 | * `"COLLECTOR"`, `"EXPORTER"`). |
| 14 | */ |
| 15 | type: string; |
| 16 | /** |
| 17 | * Component-specific options (e.g. `{ loggingLevel: "DEBUG" }`). |
| 18 | */ |
| 19 | options?: Record<string, string>; |
| 20 | } |
| 21 | |
| 22 | export interface ScraperLoggingConfigurationProps { |
| 23 | /** |
| 24 | * Id of the AMP scraper whose component logs are shipped. A scraper has |
| 25 | * at most one logging configuration. Changing the scraper replaces the |
| 26 | * configuration. |
| 27 | */ |
| 28 | scraperId: string; |
| 29 | /** |
| 30 | * ARN of the CloudWatch Logs log group that receives the scraper's |
| 31 | * component logs. AMP expects the ARN with a trailing `:*` — one is |
| 32 | * appended automatically when missing. |
| 33 | */ |
| 34 | logGroupArn: string; |
| 35 | /** |
| 36 | * Per-component logging configuration. If omitted, the service default |
| 37 | * component set is logged. |
| 38 | */ |
| 39 | components?: ScraperComponentConfig[]; |
| 40 | } |
| 41 | |
| 42 | export interface ScraperLoggingConfiguration extends Resource< |
| 43 | "AWS.AMP.ScraperLoggingConfiguration", |
| 44 | ScraperLoggingConfigurationProps, |
| 45 | { |
| 46 | scraperId: string; |
| 47 | logGroupArn: string; |
| 48 | status: string; |
| 49 | }, |
| 50 | never, |
| 51 | Providers |
| 52 | > {} |
| 53 | |
| 54 | /** |
| 55 | * The logging configuration of an Amazon Managed Service for Prometheus |
| 56 | * scraper — ships the scraper's component logs (service discovery, |
| 57 | * collection, export) to a CloudWatch Logs log group. A scraper has at most |
| 58 | * one. |
| 59 | * |
| 60 | * ### Creating a Scraper Logging Configuration |
| 61 | * **Example:** Ship Scraper Logs to CloudWatch Logs |
| 62 | * ```typescript |
| 63 | * const logs = yield* Logs.LogGroup("ScraperLogs", { |
| 64 | * logGroupName: "/aws/vendedlogs/prometheus/scraper", |
| 65 | * }); |
| 66 | * const logging = yield* AMP.ScraperLoggingConfiguration("ScraperLogging", { |
| 67 | * scraperId: scraper.scraperId, |
| 68 | * logGroupArn: logs.logGroupArn, |
| 69 | * }); |
| 70 | * ``` |
| 71 | * |
| 72 | * @resource |
| 73 | */ |
| 74 | export const ScraperLoggingConfiguration = Resource<ScraperLoggingConfiguration>( |
| 75 | "AWS.AMP.ScraperLoggingConfiguration", |
| 76 | ); |
| 77 | |
| 78 | export const ScraperLoggingConfigurationProvider = () => |
| 79 | Provider.effect( |
| 80 | ScraperLoggingConfiguration, |
| 81 | Effect.gen(function* () { |
| 82 | /** Describe the configuration; typed not-found → undefined. */ |
| 83 | const describe = Effect.fn(function* (scraperId: string) { |
| 84 | return yield* amp |
| 85 | .describeScraperLoggingConfiguration({ scraperId }) |
| 86 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined)); |
| 87 | }); |
| 88 | |
| 89 | const toAttrs = (config: amp.DescribeScraperLoggingConfigurationResponse) => ({ |
| 90 | scraperId: config.scraperId, |
| 91 | logGroupArn: config.loggingDestination.cloudWatchLogs.logGroupArn, |
| 92 | status: config.status.statusCode, |
| 93 | }); |
| 94 | |
| 95 | /** Canonical component list for observed-vs-desired comparison. */ |
| 96 | const canonicalComponents = ( |
| 97 | components: { |
| 98 | type: string; |
| 99 | config?: { options?: Record<string, string | undefined> }; |
| 100 | }[], |
| 101 | ) => |
| 102 | JSON.stringify( |
| 103 | components |
| 104 | .map((component) => ({ |
| 105 | type: component.type, |
| 106 | options: Object.fromEntries( |
| 107 | Object.entries(component.config?.options ?? {}) |
| 108 | .filter((kv): kv is [string, string] => typeof kv[1] === "string") |
| 109 | .sort(([a], [b]) => a.localeCompare(b)), |
| 110 | ), |
| 111 | })) |
| 112 | .sort((a, b) => a.type.localeCompare(b.type)), |
| 113 | ); |
| 114 | |
| 115 | const toWireComponents = ( |
| 116 | components: ScraperComponentConfig[] | undefined, |
| 117 | ): amp.ScraperComponent[] | undefined => |
| 118 | components?.map((component) => ({ |
| 119 | type: component.type, |
| 120 | config: component.options !== undefined ? { options: component.options } : undefined, |
| 121 | })); |
| 122 | |
| 123 | return { |
| 124 | stables: ["scraperId"], |
| 125 | |
| 126 | diff: Effect.fn(function* ({ olds, news }) { |
| 127 | if (!isResolved(news)) return undefined; |
| 128 | if (olds?.scraperId !== news.scraperId) { |
| 129 | return { action: "replace" } as const; |
| 130 | } |
| 131 | }), |
| 132 | |
| 133 | read: Effect.fn(function* ({ olds, output }) { |
| 134 | const scraperId = output?.scraperId ?? olds?.scraperId; |
| 135 | if (!scraperId) return undefined; |
| 136 | const config = yield* describe(scraperId); |
| 137 | if (config === undefined) return undefined; |
| 138 | // Scraper logging configurations are not taggable — ownership is |
| 139 | // implied by the owned parent scraper. |
| 140 | return toAttrs(config); |
| 141 | }), |
| 142 | |
| 143 | reconcile: Effect.fn(function* ({ news, session }) { |
| 144 | const scraperId = news!.scraperId; |
| 145 | const desiredArn = normalizeAmpLogGroupArn(news!.logGroupArn); |
| 146 | const desiredComponents = toWireComponents(news!.components); |
| 147 | |
| 148 | // 1. Observe — the live configuration is authoritative. |
| 149 | const observed = yield* describe(scraperId); |
| 150 | |
| 151 | // 2/3. Ensure + sync — `updateScraperLoggingConfiguration` is an |
| 152 | // upsert (there is no separate create operation); apply it only |
| 153 | // when the destination or components drift. |
| 154 | const drifts = |
| 155 | observed === undefined || |
| 156 | observed.loggingDestination.cloudWatchLogs.logGroupArn !== desiredArn || |
| 157 | (desiredComponents !== undefined && |
| 158 | canonicalComponents(desiredComponents) !== |
| 159 | canonicalComponents(observed.scraperComponents)); |
| 160 | |
| 161 | if (drifts) { |
| 162 | yield* amp |
| 163 | .updateScraperLoggingConfiguration({ |
| 164 | scraperId, |
| 165 | loggingDestination: { |
| 166 | cloudWatchLogs: { logGroupArn: desiredArn }, |
| 167 | }, |
| 168 | scraperComponents: desiredComponents, |
| 169 | }) |
| 170 | .pipe( |
| 171 | // The scraper (or a previous logging update) may still be |
| 172 | // transitioning — retry conflicts briefly. |
| 173 | Effect.retry({ |
| 174 | while: (e) => e._tag === "ConflictException", |
| 175 | schedule: Schedule.max([Schedule.fixed("6 seconds"), Schedule.recurs(15)]), |
| 176 | }), |
| 177 | ); |
| 178 | } |
| 179 | |
| 180 | // Bounded best-effort wait toward ACTIVE — a still-transitioning |
| 181 | // configuration converges on a later reconcile. |
| 182 | const fresh = yield* amp.describeScraperLoggingConfiguration({ scraperId }).pipe( |
| 183 | Effect.repeat({ |
| 184 | schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(20)]), |
| 185 | until: (c): boolean => c.status.statusCode === "ACTIVE", |
| 186 | }), |
| 187 | ); |
| 188 | yield* session.note(scraperId); |
| 189 | return toAttrs(fresh); |
| 190 | }), |
| 191 | |
| 192 | delete: Effect.fn(function* ({ output }) { |
| 193 | yield* amp |
| 194 | .deleteScraperLoggingConfiguration({ |
| 195 | scraperId: output.scraperId, |
| 196 | }) |
| 197 | .pipe( |
| 198 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 199 | Effect.retry({ |
| 200 | while: (e) => e._tag === "ConflictException", |
| 201 | schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(20)]), |
| 202 | }), |
| 203 | ); |
| 204 | }), |
| 205 | |
| 206 | // Singleton sub-resource keyed by its parent scraper. |
| 207 | list: () => Effect.succeed([]), |
| 208 | }; |
| 209 | }), |
| 210 | ); |