The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as logging from "@distilled.cloud/gcp/logging_v2"; |
| 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 * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Stream from "effect/Stream"; |
| 9 | import { GcpEnvironment } from "../Environment.ts"; |
| 10 | import { createInternalLabels, hasAlchemyLabels } from "../Labels.ts"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | import { |
| 13 | billingAccountIdOf, |
| 14 | billingAccountParent, |
| 15 | encodeDescription, |
| 16 | hasOwnershipMarker, |
| 17 | lastSegment, |
| 18 | lookupProjectBillingAccountId, |
| 19 | parseDescription, |
| 20 | resolveBillingAccountId, |
| 21 | toPhysicalId, |
| 22 | } from "./internal.ts"; |
| 23 | |
| 24 | export type BillingExclusionProps = { |
| 25 | /** |
| 26 | * Exclusion id (the `{exclusion}` segment of |
| 27 | * `billingAccounts/{billingAccount}/exclusions/{exclusion}`). If omitted, |
| 28 | * a unique name is generated from the stack, stage, and logical id. |
| 29 | * Limited to 100 characters: letters, digits, underscores, hyphens, |
| 30 | * periods; first character must be alphanumeric. Immutable — changing |
| 31 | * it replaces the exclusion. |
| 32 | */ |
| 33 | exclusionId?: string; |
| 34 | /** |
| 35 | * Billing account id (`XXXXXX-XXXXXX-XXXXXX` or |
| 36 | * `billingAccounts/{id}`). If omitted, Alchemy uses the billing |
| 37 | * account linked to the current project. Immutable — changing it |
| 38 | * replaces the exclusion. |
| 39 | */ |
| 40 | billingAccountId?: string; |
| 41 | /** |
| 42 | * Advanced logs filter matching entries to exclude from the `_Default` |
| 43 | * sink. Required. |
| 44 | */ |
| 45 | filter: string; |
| 46 | /** |
| 47 | * Human-readable description. Logging exclusions have no labels field, |
| 48 | * so Alchemy ownership is stored in a `[alchemy …]` prefix and stripped |
| 49 | * from attributes. |
| 50 | */ |
| 51 | description?: string; |
| 52 | /** |
| 53 | * When true, the exclusion exists but does not exclude any entries. |
| 54 | * @default false |
| 55 | */ |
| 56 | disabled?: boolean; |
| 57 | }; |
| 58 | |
| 59 | export type BillingExclusion = Resource< |
| 60 | "GCP.Logging.BillingExclusion", |
| 61 | BillingExclusionProps, |
| 62 | { |
| 63 | /** Full resource name `billingAccounts/{billingAccount}/exclusions/{exclusionId}`. */ |
| 64 | name: string; |
| 65 | /** Exclusion id (last path segment). */ |
| 66 | exclusionId: string; |
| 67 | /** Billing account id. */ |
| 68 | billingAccountId: string; |
| 69 | /** Advanced logs filter. */ |
| 70 | filter: string; |
| 71 | /** User description with the Alchemy ownership prefix stripped. */ |
| 72 | description: string | undefined; |
| 73 | /** Whether the exclusion is disabled. */ |
| 74 | disabled: boolean; |
| 75 | /** RFC3339 creation timestamp. */ |
| 76 | createTime: string | undefined; |
| 77 | /** RFC3339 last-update timestamp. */ |
| 78 | updateTime: string | undefined; |
| 79 | }, |
| 80 | never, |
| 81 | Providers |
| 82 | >; |
| 83 | |
| 84 | /** |
| 85 | * A Cloud Logging exclusion on a billing account `_Default` sink. |
| 86 | * |
| 87 | * Exclusions drop matching log entries before they are stored in the |
| 88 | * `_Default` bucket (they do not apply to `_Required`). Logging exclusions |
| 89 | * have no labels field, so Alchemy stamps ownership into the description |
| 90 | * for `list` / nuke. Name is identity — changing `exclusionId` or |
| 91 | * `billingAccountId` replaces the exclusion. |
| 92 | * |
| 93 | * ### Creating a Billing Exclusion |
| 94 | * **Example:** Generated name, drop debug logs |
| 95 | * ```typescript |
| 96 | * const exclusion = yield* GCP.Logging.BillingExclusion("DropDebug", { |
| 97 | * filter: "severity=DEBUG", |
| 98 | * description: "drop debug entries", |
| 99 | * }); |
| 100 | * ``` |
| 101 | * |
| 102 | * **Example:** Named exclusion on an explicit billing account |
| 103 | * ```typescript |
| 104 | * const exclusion = yield* GCP.Logging.BillingExclusion("DropDebug", { |
| 105 | * billingAccountId: "AAAAAA-BBBBBB-CCCCCC", |
| 106 | * exclusionId: "drop-debug", |
| 107 | * filter: "severity=DEBUG", |
| 108 | * }); |
| 109 | * ``` |
| 110 | * |
| 111 | * ### Updating a Billing Exclusion |
| 112 | * **Example:** Change the filter and disable |
| 113 | * ```typescript |
| 114 | * const exclusion = yield* GCP.Logging.BillingExclusion("DropDebug", { |
| 115 | * filter: "severity<ERROR", |
| 116 | * description: "drop non-errors", |
| 117 | * disabled: true, |
| 118 | * }); |
| 119 | * ``` |
| 120 | * |
| 121 | * @resource |
| 122 | * @category Logging |
| 123 | */ |
| 124 | export const BillingExclusion = Resource<BillingExclusion>("GCP.Logging.BillingExclusion"); |
| 125 | |
| 126 | export class BillingExclusionNotResolved extends Data.TaggedError( |
| 127 | "GCP.Logging.BillingExclusionNotResolved", |
| 128 | )<{ |
| 129 | resourceName: string; |
| 130 | }> {} |
| 131 | |
| 132 | const resourceName = (billingAccountId: string, exclusionId: string) => |
| 133 | `${billingAccountParent(billingAccountId)}/exclusions/${exclusionId}`; |
| 134 | |
| 135 | const exclusionIdOf = (exclusion: logging.LogExclusion) => { |
| 136 | const raw = exclusion.name ?? ""; |
| 137 | return raw.includes("/") ? lastSegment(raw) : raw; |
| 138 | }; |
| 139 | |
| 140 | const billingAccountOfName = (name: string, fallback: string) => { |
| 141 | const match = name.match(/^billingAccounts\/([^/]+)\//); |
| 142 | return match?.[1] ?? fallback; |
| 143 | }; |
| 144 | |
| 145 | const toAttrs = (exclusion: logging.LogExclusion, billingAccountId: string) => { |
| 146 | const exclusionId = exclusionIdOf(exclusion); |
| 147 | const parsed = parseDescription(exclusion.description); |
| 148 | const account = billingAccountOfName(exclusion.name ?? "", billingAccountId); |
| 149 | return { |
| 150 | name: |
| 151 | exclusion.name?.includes("/") === true ? exclusion.name : resourceName(account, exclusionId), |
| 152 | exclusionId, |
| 153 | billingAccountId: account, |
| 154 | filter: exclusion.filter ?? "", |
| 155 | description: parsed.description, |
| 156 | disabled: exclusion.disabled === true, |
| 157 | createTime: exclusion.createTime, |
| 158 | updateTime: exclusion.updateTime, |
| 159 | }; |
| 160 | }; |
| 161 | |
| 162 | const getByName = (name: string) => |
| 163 | logging |
| 164 | .getBillingAccountsExclusions({ name }) |
| 165 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 166 | |
| 167 | export const BillingExclusionProvider = () => |
| 168 | Provider.succeed(BillingExclusion, { |
| 169 | stables: ["name", "exclusionId", "billingAccountId", "createTime"], |
| 170 | |
| 171 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 172 | if (!isResolved(news)) return undefined; |
| 173 | const previousId = olds?.exclusionId ?? output?.exclusionId; |
| 174 | const idChanged = |
| 175 | previousId !== undefined && |
| 176 | news.exclusionId !== undefined && |
| 177 | news.exclusionId !== previousId; |
| 178 | const previousAccount = olds?.billingAccountId ?? output?.billingAccountId; |
| 179 | const accountChanged = |
| 180 | previousAccount !== undefined && |
| 181 | news.billingAccountId !== undefined && |
| 182 | billingAccountIdOf(news.billingAccountId) !== billingAccountIdOf(previousAccount); |
| 183 | if (!idChanged && !accountChanged) return undefined; |
| 184 | return { action: "replace" as const, deleteFirst: false }; |
| 185 | }), |
| 186 | |
| 187 | read: Effect.fn(function* ({ id, olds, output }) { |
| 188 | const billingAccountId = yield* resolveBillingAccountId( |
| 189 | olds?.billingAccountId, |
| 190 | output?.billingAccountId, |
| 191 | ); |
| 192 | const exclusionId = yield* toPhysicalId(id, olds?.exclusionId, output?.exclusionId, "e"); |
| 193 | const name = output?.name ?? resourceName(billingAccountId, exclusionId); |
| 194 | const existing = yield* getByName(name); |
| 195 | if (existing === undefined) return undefined; |
| 196 | const attrs = toAttrs(existing, billingAccountId); |
| 197 | const { labels } = parseDescription(existing.description); |
| 198 | return (yield* hasAlchemyLabels(id, labels)) ? attrs : Unowned(attrs); |
| 199 | }), |
| 200 | |
| 201 | list: () => |
| 202 | Effect.gen(function* () { |
| 203 | const env = yield* GcpEnvironment.current; |
| 204 | const billingAccountId = yield* lookupProjectBillingAccountId(env.project); |
| 205 | if (billingAccountId === undefined) return []; |
| 206 | return yield* logging.listBillingAccountsExclusions |
| 207 | .pages({ |
| 208 | parent: billingAccountParent(billingAccountId), |
| 209 | pageSize: 1000, |
| 210 | }) |
| 211 | .pipe( |
| 212 | Stream.flatMap((page) => Stream.fromIterable(page.exclusions ?? [])), |
| 213 | Stream.filter((exclusion) => hasOwnershipMarker(exclusion.description)), |
| 214 | Stream.map((exclusion) => toAttrs(exclusion, billingAccountId)), |
| 215 | Stream.runCollect, |
| 216 | Effect.map((chunk) => Array.from(chunk)), |
| 217 | Effect.catchTag("NotFound", () => |
| 218 | Effect.succeed([] as BillingExclusion["Attributes"][]), |
| 219 | ), |
| 220 | ); |
| 221 | }), |
| 222 | |
| 223 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 224 | const billingAccountId = yield* resolveBillingAccountId( |
| 225 | news.billingAccountId, |
| 226 | output?.billingAccountId, |
| 227 | ); |
| 228 | const exclusionId = yield* toPhysicalId(id, news.exclusionId, output?.exclusionId, "e"); |
| 229 | const name = resourceName(billingAccountId, exclusionId); |
| 230 | const ownership = yield* createInternalLabels(id); |
| 231 | const desiredDescription = encodeDescription(ownership, news.description); |
| 232 | |
| 233 | let current = yield* getByName(output?.name ?? name); |
| 234 | |
| 235 | if (current === undefined) { |
| 236 | const created = yield* logging |
| 237 | .createBillingAccountsExclusions({ |
| 238 | parent: billingAccountParent(billingAccountId), |
| 239 | body: { |
| 240 | name: exclusionId, |
| 241 | filter: news.filter, |
| 242 | description: desiredDescription, |
| 243 | disabled: news.disabled === true ? true : undefined, |
| 244 | }, |
| 245 | }) |
| 246 | .pipe(Effect.catchTag("Conflict", () => getByName(name))); |
| 247 | current = created ?? undefined; |
| 248 | } |
| 249 | |
| 250 | if (current === undefined) { |
| 251 | return yield* new BillingExclusionNotResolved({ resourceName: name }); |
| 252 | } |
| 253 | |
| 254 | const desiredDisabled = news.disabled === true; |
| 255 | const filterChanged = (current.filter ?? "") !== news.filter; |
| 256 | const descriptionChanged = (current.description ?? "") !== desiredDescription; |
| 257 | const disabledChanged = (current.disabled === true) !== desiredDisabled; |
| 258 | |
| 259 | const updateMask = [ |
| 260 | filterChanged ? "filter" : undefined, |
| 261 | descriptionChanged ? "description" : undefined, |
| 262 | disabledChanged ? "disabled" : undefined, |
| 263 | ].filter((field): field is string => field !== undefined); |
| 264 | |
| 265 | if (updateMask.length > 0) { |
| 266 | current = yield* logging.patchBillingAccountsExclusions({ |
| 267 | name: current.name?.includes("/") ? current.name : name, |
| 268 | updateMask: updateMask.join(","), |
| 269 | body: { |
| 270 | filter: news.filter, |
| 271 | description: desiredDescription, |
| 272 | disabled: desiredDisabled, |
| 273 | }, |
| 274 | }); |
| 275 | } |
| 276 | |
| 277 | return toAttrs(current, billingAccountId); |
| 278 | }), |
| 279 | |
| 280 | delete: Effect.fn(function* ({ output }) { |
| 281 | yield* logging |
| 282 | .deleteBillingAccountsExclusions({ name: output.name }) |
| 283 | .pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 284 | }), |
| 285 | }); |