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
| 1 | import * as oam from "@distilled.cloud/aws/oam"; |
| 2 | import { isResolved } from "alchemy/Diff"; |
| 3 | import * as Provider from "alchemy/Provider"; |
| 4 | import { Resource } from "alchemy/Resource"; |
| 5 | import { hasAlchemyTags } from "alchemy/Tags"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Stream from "effect/Stream"; |
| 8 | import type { Providers } from "../Providers.ts"; |
| 9 | import { deleteLinkAndWait, readOamTags, retryOamMutation, syncOamTags } from "./internal.ts"; |
| 10 | |
| 11 | /** |
| 12 | * The telemetry resource types that can be shared over a link. |
| 13 | */ |
| 14 | export type LinkResourceType = |
| 15 | | "AWS::CloudWatch::Metric" |
| 16 | | "AWS::Logs::LogGroup" |
| 17 | | "AWS::XRay::Trace" |
| 18 | | "AWS::ApplicationInsights::Application" |
| 19 | | "AWS::InternetMonitor::Monitor" |
| 20 | | "AWS::ApplicationSignals::Service" |
| 21 | | "AWS::ApplicationSignals::ServiceLevelObjective" |
| 22 | | (string & {}); |
| 23 | |
| 24 | export interface LinkProps { |
| 25 | /** |
| 26 | * How the link's label appears in the monitoring account. Variables: |
| 27 | * `$AccountName`, `$AccountEmail`, `$AccountEmailNoDomain`. |
| 28 | * Changing the label template replaces the link. |
| 29 | */ |
| 30 | labelTemplate: string; |
| 31 | |
| 32 | /** |
| 33 | * The telemetry resource types shared from this source account to the |
| 34 | * monitoring account, e.g. `AWS::CloudWatch::Metric`, |
| 35 | * `AWS::Logs::LogGroup`, `AWS::XRay::Trace`. |
| 36 | */ |
| 37 | resourceTypes: LinkResourceType[]; |
| 38 | |
| 39 | /** |
| 40 | * The ARN of the monitoring-account sink to attach to. The sink's policy |
| 41 | * must permit this account to link. Changing the sink replaces the link. |
| 42 | * Must be in a **different** account — OAM rejects a link to a sink in |
| 43 | * the same account. |
| 44 | */ |
| 45 | sinkIdentifier: string; |
| 46 | |
| 47 | /** |
| 48 | * Optional filters restricting which log groups and metric namespaces |
| 49 | * are shared to the monitoring account. |
| 50 | */ |
| 51 | linkConfiguration?: { |
| 52 | /** |
| 53 | * Filter (OAM filter syntax) selecting which log groups are shared, |
| 54 | * e.g. `LogGroupName LIKE 'aws/lambda/%'`. |
| 55 | */ |
| 56 | logGroupConfiguration?: { filter: string }; |
| 57 | /** |
| 58 | * Filter selecting which metric namespaces are shared, e.g. |
| 59 | * `Namespace NOT LIKE 'AWS/%'`. |
| 60 | */ |
| 61 | metricConfiguration?: { filter: string }; |
| 62 | }; |
| 63 | |
| 64 | /** |
| 65 | * User tags to attach to the link. Merged with internal Alchemy tags. |
| 66 | */ |
| 67 | tags?: Record<string, string>; |
| 68 | } |
| 69 | |
| 70 | export interface Link extends Resource< |
| 71 | "AWS.OAM.Link", |
| 72 | LinkProps, |
| 73 | { |
| 74 | /** The ARN of the link. */ |
| 75 | linkArn: string; |
| 76 | /** The random ID string that AWS generated as part of the link ARN. */ |
| 77 | linkId: string; |
| 78 | /** The label that this link displays in the monitoring account. */ |
| 79 | label: string; |
| 80 | /** The ARN of the sink this link is attached to. */ |
| 81 | sinkArn: string; |
| 82 | }, |
| 83 | never, |
| 84 | Providers |
| 85 | > {} |
| 86 | |
| 87 | /** |
| 88 | * A CloudWatch cross-account observability **link** — created in a source |
| 89 | * account, it attaches to a monitoring-account {@link Sink} and shares the |
| 90 | * selected telemetry types (metrics, log groups, traces, Application |
| 91 | * Signals) with that account. |
| 92 | * |
| 93 | * The sink must live in a **different** account and its sink policy must |
| 94 | * authorize this account to link. |
| 95 | * |
| 96 | * ### Creating a Link |
| 97 | * **Example:** Share metrics and logs with a monitoring account |
| 98 | * ```typescript |
| 99 | * import * as OAM from "@alchemy.run/aws/OAM"; |
| 100 | * |
| 101 | * const link = yield* OAM.Link("ToMonitoring", { |
| 102 | * labelTemplate: "$AccountName", |
| 103 | * resourceTypes: ["AWS::CloudWatch::Metric", "AWS::Logs::LogGroup"], |
| 104 | * sinkIdentifier: |
| 105 | * "arn:aws:oam:us-west-2:111122223333:sink/1c72e9ec-4d4a-4e...", |
| 106 | * }); |
| 107 | * ``` |
| 108 | * |
| 109 | * **Example:** Filter what is shared |
| 110 | * ```typescript |
| 111 | * const link = yield* OAM.Link("FilteredLink", { |
| 112 | * labelTemplate: "$AccountName", |
| 113 | * resourceTypes: ["AWS::CloudWatch::Metric", "AWS::Logs::LogGroup"], |
| 114 | * sinkIdentifier: sinkArn, |
| 115 | * linkConfiguration: { |
| 116 | * logGroupConfiguration: { filter: "LogGroupName LIKE 'aws/lambda/%'" }, |
| 117 | * metricConfiguration: { filter: "Namespace NOT LIKE 'AWS/%'" }, |
| 118 | * }, |
| 119 | * }); |
| 120 | * ``` |
| 121 | * |
| 122 | * @resource |
| 123 | */ |
| 124 | export const Link = Resource<Link>("AWS.OAM.Link"); |
| 125 | |
| 126 | const toLinkConfiguration = ( |
| 127 | config: LinkProps["linkConfiguration"], |
| 128 | ): oam.LinkConfiguration | undefined => |
| 129 | config === undefined |
| 130 | ? undefined |
| 131 | : { |
| 132 | ...(config.logGroupConfiguration |
| 133 | ? { |
| 134 | LogGroupConfiguration: { |
| 135 | Filter: config.logGroupConfiguration.filter, |
| 136 | }, |
| 137 | } |
| 138 | : {}), |
| 139 | ...(config.metricConfiguration |
| 140 | ? { |
| 141 | MetricConfiguration: { |
| 142 | Filter: config.metricConfiguration.filter, |
| 143 | }, |
| 144 | } |
| 145 | : {}), |
| 146 | }; |
| 147 | |
| 148 | const sameResourceTypes = ( |
| 149 | a: readonly string[] | undefined, |
| 150 | b: readonly string[] | undefined, |
| 151 | ): boolean => JSON.stringify([...(a ?? [])].sort()) === JSON.stringify([...(b ?? [])].sort()); |
| 152 | |
| 153 | const sameLinkConfiguration = ( |
| 154 | a: oam.LinkConfiguration | undefined, |
| 155 | b: oam.LinkConfiguration | undefined, |
| 156 | ): boolean => |
| 157 | (a?.LogGroupConfiguration?.Filter ?? null) === (b?.LogGroupConfiguration?.Filter ?? null) && |
| 158 | (a?.MetricConfiguration?.Filter ?? null) === (b?.MetricConfiguration?.Filter ?? null); |
| 159 | |
| 160 | export const LinkProvider = () => |
| 161 | Provider.effect( |
| 162 | Link, |
| 163 | Effect.gen(function* () { |
| 164 | const getLinkByArn = Effect.fn(function* (linkArn: string) { |
| 165 | return yield* oam |
| 166 | .getLink({ Identifier: linkArn }) |
| 167 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined)); |
| 168 | }); |
| 169 | |
| 170 | return Link.Provider.of({ |
| 171 | stables: ["linkArn", "linkId", "label", "sinkArn"], |
| 172 | list: () => |
| 173 | oam.listLinks.pages({}).pipe( |
| 174 | Stream.runCollect, |
| 175 | Effect.map((chunk) => |
| 176 | Array.from(chunk).flatMap((page) => |
| 177 | page.Items.filter((item) => item.Arn != null && item.Id != null).map((item) => ({ |
| 178 | linkArn: item.Arn!, |
| 179 | linkId: item.Id!, |
| 180 | label: item.Label ?? "", |
| 181 | sinkArn: item.SinkArn ?? "", |
| 182 | })), |
| 183 | ), |
| 184 | ), |
| 185 | ), |
| 186 | read: Effect.fn(function* ({ id, output }) { |
| 187 | // Links are identified by an auto-assigned ARN. With a cached ARN |
| 188 | // we look it up directly; without one (state persistence failed |
| 189 | // before the ARN landed) we scan the account's links for the one |
| 190 | // carrying our ownership tags. |
| 191 | if (output?.linkArn) { |
| 192 | const found = yield* getLinkByArn(output.linkArn); |
| 193 | if (!found?.Arn) return undefined; |
| 194 | return { |
| 195 | linkArn: found.Arn, |
| 196 | linkId: found.Id!, |
| 197 | label: found.Label ?? "", |
| 198 | sinkArn: found.SinkArn ?? "", |
| 199 | }; |
| 200 | } |
| 201 | const pages = yield* oam.listLinks.pages({}).pipe(Stream.runCollect); |
| 202 | const items = Array.from(pages).flatMap((page) => page.Items); |
| 203 | for (const item of items) { |
| 204 | if (item.Arn == null) continue; |
| 205 | const tags = yield* readOamTags(item.Arn); |
| 206 | if (yield* hasAlchemyTags(id, tags)) { |
| 207 | return { |
| 208 | linkArn: item.Arn, |
| 209 | linkId: item.Id!, |
| 210 | label: item.Label ?? "", |
| 211 | sinkArn: item.SinkArn ?? "", |
| 212 | }; |
| 213 | } |
| 214 | } |
| 215 | return undefined; |
| 216 | }), |
| 217 | diff: Effect.fn(function* ({ news, olds }) { |
| 218 | if (!isResolved(news)) return undefined; |
| 219 | // The label template and the target sink are immutable — |
| 220 | // UpdateLink only changes resource types and filters. |
| 221 | if (news.labelTemplate !== olds.labelTemplate) { |
| 222 | return { action: "replace" } as const; |
| 223 | } |
| 224 | if (news.sinkIdentifier !== olds.sinkIdentifier) { |
| 225 | return { action: "replace" } as const; |
| 226 | } |
| 227 | return undefined; |
| 228 | }), |
| 229 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 230 | const desiredConfiguration = toLinkConfiguration(news.linkConfiguration); |
| 231 | |
| 232 | // OBSERVE — the cached ARN is only a hint; a deleted link falls |
| 233 | // through to create. |
| 234 | let live = output?.linkArn ? yield* getLinkByArn(output.linkArn) : undefined; |
| 235 | |
| 236 | // ENSURE |
| 237 | if (live?.Arn == null) { |
| 238 | live = yield* retryOamMutation( |
| 239 | oam.createLink({ |
| 240 | LabelTemplate: news.labelTemplate, |
| 241 | ResourceTypes: news.resourceTypes, |
| 242 | SinkIdentifier: news.sinkIdentifier, |
| 243 | LinkConfiguration: desiredConfiguration, |
| 244 | Tags: news.tags, |
| 245 | }), |
| 246 | ); |
| 247 | } |
| 248 | const linkArn = live!.Arn!; |
| 249 | |
| 250 | // SYNC — resource types + filters, diffed against observed state. |
| 251 | if ( |
| 252 | !sameResourceTypes(live!.ResourceTypes, news.resourceTypes) || |
| 253 | !sameLinkConfiguration(live!.LinkConfiguration, desiredConfiguration) |
| 254 | ) { |
| 255 | live = yield* retryOamMutation( |
| 256 | oam.updateLink({ |
| 257 | Identifier: linkArn, |
| 258 | ResourceTypes: news.resourceTypes, |
| 259 | LinkConfiguration: desiredConfiguration, |
| 260 | }), |
| 261 | ); |
| 262 | } |
| 263 | |
| 264 | // SYNC tags — against observed cloud tags (adoption-safe). |
| 265 | yield* syncOamTags(linkArn, id, news.tags); |
| 266 | |
| 267 | yield* session.note(linkArn); |
| 268 | return { |
| 269 | linkArn, |
| 270 | linkId: live!.Id!, |
| 271 | label: live!.Label ?? "", |
| 272 | sinkArn: live!.SinkArn ?? "", |
| 273 | }; |
| 274 | }), |
| 275 | delete: Effect.fn(function* ({ output }) { |
| 276 | yield* deleteLinkAndWait(output.linkArn); |
| 277 | }), |
| 278 | }); |
| 279 | }), |
| 280 | ); |