@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 405 lines 14.5 KB
Raw
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 import type { SinkBigQueryOptions, SinkExclusion } from "./Sink.ts";
24
25 export type BillingSinkExclusion = SinkExclusion;
26 export type BillingSinkBigQueryOptions = SinkBigQueryOptions;
27
28 export type BillingSinkProps = {
29 /**
30 * Sink id (the `{sink}` segment of
31 * `billingAccounts/{billingAccount}/sinks/{sink}`). If omitted, a unique
32 * name is generated from the stack, stage, and logical id. Limited to
33 * 100 characters: letters, digits, underscores, hyphens, periods; first
34 * character must be alphanumeric. Immutable — changing it replaces the
35 * sink.
36 */
37 sinkId?: string;
38 /**
39 * Billing account id (`XXXXXX-XXXXXX-XXXXXX` or
40 * `billingAccounts/{id}`). If omitted, Alchemy uses the billing
41 * account linked to the current project. Immutable — changing it
42 * replaces the sink.
43 */
44 billingAccountId?: string;
45 /**
46 * Export destination. One of:
47 * `storage.googleapis.com/[GCS_BUCKET]`,
48 * `bigquery.googleapis.com/projects/[PROJECT_ID]/datasets/[DATASET]`,
49 * `pubsub.googleapis.com/projects/[PROJECT_ID]/topics/[TOPIC_ID]`,
50 * `logging.googleapis.com/projects/[PROJECT_ID]`,
51 * `logging.googleapis.com/billingAccounts/[BILLING_ACCOUNT_ID]/locations/[LOCATION_ID]/buckets/[BUCKET_ID]`.
52 */
53 destination: string;
54 /**
55 * Advanced logs filter. Only matching entries in the sink's parent are
56 * exported. Empty or omitted exports every entry.
57 */
58 filter?: string;
59 /**
60 * Human-readable description (max 8000 characters). Logging sinks have
61 * no labels field, so Alchemy ownership is stored in a `[alchemy …]`
62 * prefix and stripped from attributes.
63 */
64 description?: string;
65 /**
66 * When true, the sink exists but does not export any log entries.
67 * @default false
68 */
69 disabled?: boolean;
70 /**
71 * Exclusion filters. An entry matching `filter` and any exclusion is
72 * not exported.
73 */
74 exclusions?: BillingSinkExclusion[];
75 /**
76 * BigQuery-specific export options. Ignored unless `destination` is a
77 * BigQuery dataset.
78 */
79 bigqueryOptions?: BillingSinkBigQueryOptions;
80 /**
81 * When true, Cloud Logging assigns a unique writer service account as
82 * `writerIdentity`. Billing-account sinks typically use a service
83 * agent.
84 */
85 uniqueWriterIdentity?: boolean;
86 /**
87 * Caller-provided writer service account
88 * (`serviceAccount:some@email`). Only valid when routing to a log
89 * bucket in a different project.
90 */
91 customWriterIdentity?: string;
92 };
93
94 export type BillingSink = Resource<
95 "GCP.Logging.BillingSink",
96 BillingSinkProps,
97 {
98 /** Full resource name `billingAccounts/{billingAccount}/sinks/{sink}`. */
99 name: string;
100 /** Sink id (last path segment). */
101 sinkId: string;
102 /** Billing account id. */
103 billingAccountId: string;
104 /** Export destination. */
105 destination: string;
106 /** Advanced logs filter, if set. */
107 filter: string | undefined;
108 /** User description with the Alchemy ownership prefix stripped. */
109 description: string | undefined;
110 /** Whether the sink is disabled. */
111 disabled: boolean;
112 /** Exclusion filters. */
113 exclusions: BillingSinkExclusion[];
114 /** BigQuery export options, if set. */
115 bigqueryOptions: BillingSinkBigQueryOptions | undefined;
116 /**
117 * IAM identity Cloud Logging uses to write to `destination`.
118 */
119 writerIdentity: string | undefined;
120 /** RFC3339 creation timestamp. */
121 createTime: string | undefined;
122 /** RFC3339 last-update timestamp. */
123 updateTime: string | undefined;
124 },
125 never,
126 Providers
127 >;
128
129 /**
130 * A Cloud Logging sink that routes billing-account log entries to a
131 * destination.
132 *
133 * Logging sinks have no labels field, so Alchemy stamps ownership into
134 * the description for `list` / nuke. Name is identity — changing `sinkId`
135 * or `billingAccountId` replaces the sink. Filter, destination,
136 * description, disabled flag, exclusions, and BigQuery options update in
137 * place.
138 *
139 * ### Creating a Billing Sink
140 * **Example:** Route errors to the `_Default` log bucket
141 * ```typescript
142 * const sink = yield* GCP.Logging.BillingSink("Errors", {
143 * destination:
144 * "logging.googleapis.com/billingAccounts/AAAAAA-BBBBBB-CCCCCC/locations/global/buckets/_Default",
145 * filter: "severity>=ERROR",
146 * });
147 * ```
148 *
149 * **Example:** Named sink with a description
150 * ```typescript
151 * const sink = yield* GCP.Logging.BillingSink("Errors", {
152 * sinkId: "app-errors",
153 * billingAccountId: "AAAAAA-BBBBBB-CCCCCC",
154 * destination:
155 * "logging.googleapis.com/billingAccounts/AAAAAA-BBBBBB-CCCCCC/locations/global/buckets/_Default",
156 * filter: "severity>=ERROR",
157 * description: "application errors",
158 * });
159 * ```
160 *
161 * ### Updating a Billing Sink
162 * **Example:** Change the filter and add an exclusion
163 * ```typescript
164 * const sink = yield* GCP.Logging.BillingSink("Errors", {
165 * destination:
166 * "logging.googleapis.com/billingAccounts/AAAAAA-BBBBBB-CCCCCC/locations/global/buckets/_Default",
167 * filter: "severity>=WARNING",
168 * description: "warnings and errors",
169 * exclusions: [
170 * {
171 * name: "drop-healthchecks",
172 * filter: 'httpRequest.requestUrl="/healthz"',
173 * },
174 * ],
175 * });
176 * ```
177 *
178 * @resource
179 * @category Logging
180 */
181 export const BillingSink = Resource<BillingSink>("GCP.Logging.BillingSink");
182
183 export class BillingSinkNotResolved extends Data.TaggedError("GCP.Logging.BillingSinkNotResolved")<{
184 resourceName: string;
185 }> {}
186
187 const resourceName = (billingAccountId: string, sinkId: string) =>
188 `${billingAccountParent(billingAccountId)}/sinks/${sinkId}`;
189
190 const sinkIdOf = (sink: logging.LogSink) => {
191 const raw = sink.name ?? sink.resourceName ?? "";
192 return raw.includes("/") ? lastSegment(raw) : raw;
193 };
194
195 const billingAccountOfName = (name: string, fallback: string) => {
196 const match = name.match(/^billingAccounts\/([^/]+)\//);
197 return match?.[1] ?? fallback;
198 };
199
200 const exclusionsOf = (
201 list: readonly logging.LogExclusion[] | readonly BillingSinkExclusion[] | undefined,
202 ): BillingSinkExclusion[] =>
203 (list ?? [])
204 .map((exclusion) => ({
205 name: exclusion.name ?? "",
206 filter: exclusion.filter ?? "",
207 description: exclusion.description,
208 disabled: exclusion.disabled === true,
209 }))
210 .sort((left, right) => left.name.localeCompare(right.name));
211
212 const sameExclusions = (
213 left: readonly logging.LogExclusion[] | readonly BillingSinkExclusion[] | undefined,
214 right: readonly logging.LogExclusion[] | readonly BillingSinkExclusion[] | undefined,
215 ) => JSON.stringify(exclusionsOf(left)) === JSON.stringify(exclusionsOf(right));
216
217 const toExclusionsBody = (
218 list: readonly BillingSinkExclusion[] | undefined,
219 ): logging.LogExclusion[] | undefined => {
220 if (list === undefined) return undefined;
221 return list.map((exclusion) => ({
222 name: exclusion.name,
223 filter: exclusion.filter,
224 description: exclusion.description,
225 disabled: exclusion.disabled === true ? true : undefined,
226 }));
227 };
228
229 const toAttrs = (sink: logging.LogSink, billingAccountId: string) => {
230 const sinkId = sinkIdOf(sink);
231 const parsed = parseDescription(sink.description);
232 const bq = sink.bigqueryOptions;
233 const account = billingAccountOfName(sink.resourceName ?? sink.name ?? "", billingAccountId);
234 return {
235 name: sink.resourceName ?? (sinkId ? resourceName(account, sinkId) : ""),
236 sinkId,
237 billingAccountId: account,
238 destination: sink.destination ?? "",
239 filter: sink.filter,
240 description: parsed.description,
241 disabled: sink.disabled === true,
242 exclusions: exclusionsOf(sink.exclusions),
243 bigqueryOptions:
244 bq?.usePartitionedTables !== undefined
245 ? { usePartitionedTables: bq.usePartitionedTables }
246 : undefined,
247 writerIdentity: sink.writerIdentity,
248 createTime: sink.createTime,
249 updateTime: sink.updateTime,
250 };
251 };
252
253 const getByName = (name: string) =>
254 logging
255 .getBillingAccountsSinks({ sinkName: name })
256 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
257
258 const toCreateBody = (
259 sinkId: string,
260 props: BillingSinkProps,
261 description: string,
262 ): logging.LogSink => ({
263 name: sinkId,
264 destination: props.destination,
265 filter: props.filter,
266 description,
267 disabled: props.disabled === true ? true : undefined,
268 exclusions: toExclusionsBody(props.exclusions),
269 bigqueryOptions: props.bigqueryOptions,
270 });
271
272 export const BillingSinkProvider = () =>
273 Provider.succeed(BillingSink, {
274 stables: ["name", "sinkId", "billingAccountId", "createTime"],
275
276 diff: Effect.fn(function* ({ news, olds, output }) {
277 if (!isResolved(news)) return undefined;
278 const previous = olds?.sinkId ?? output?.sinkId;
279 const idChanged =
280 previous !== undefined && news.sinkId !== undefined && news.sinkId !== previous;
281 const previousAccount = olds?.billingAccountId ?? output?.billingAccountId;
282 const accountChanged =
283 previousAccount !== undefined &&
284 news.billingAccountId !== undefined &&
285 billingAccountIdOf(news.billingAccountId) !== billingAccountIdOf(previousAccount);
286 if (!idChanged && !accountChanged) return undefined;
287 return { action: "replace" as const, deleteFirst: false };
288 }),
289
290 read: Effect.fn(function* ({ id, olds, output }) {
291 const billingAccountId = yield* resolveBillingAccountId(
292 olds?.billingAccountId,
293 output?.billingAccountId,
294 );
295 const sinkId = yield* toPhysicalId(id, olds?.sinkId, output?.sinkId, "s");
296 const name = output?.name ?? resourceName(billingAccountId, sinkId);
297 const existing = yield* getByName(name);
298 if (existing === undefined) return undefined;
299 const attrs = toAttrs(existing, billingAccountId);
300 const { labels } = parseDescription(existing.description);
301 return (yield* hasAlchemyLabels(id, labels)) ? attrs : Unowned(attrs);
302 }),
303
304 list: () =>
305 Effect.gen(function* () {
306 const env = yield* GcpEnvironment.current;
307 const billingAccountId = yield* lookupProjectBillingAccountId(env.project);
308 if (billingAccountId === undefined) return [];
309 return yield* logging.listBillingAccountsSinks
310 .pages({
311 parent: billingAccountParent(billingAccountId),
312 pageSize: 1000,
313 })
314 .pipe(
315 Stream.flatMap((page) => Stream.fromIterable(page.sinks ?? [])),
316 Stream.filter((sink) => hasOwnershipMarker(sink.description)),
317 Stream.map((sink) => toAttrs(sink, billingAccountId)),
318 Stream.runCollect,
319 Effect.map((chunk) => Array.from(chunk)),
320 Effect.catchTag("NotFound", () => Effect.succeed([] as BillingSink["Attributes"][])),
321 );
322 }),
323
324 reconcile: Effect.fn(function* ({ id, news, output }) {
325 const billingAccountId = yield* resolveBillingAccountId(
326 news.billingAccountId,
327 output?.billingAccountId,
328 );
329 const sinkId = yield* toPhysicalId(id, news.sinkId, output?.sinkId, "s");
330 const name = resourceName(billingAccountId, sinkId);
331 const ownership = yield* createInternalLabels(id);
332 const desiredDescription = encodeDescription(ownership, news.description);
333
334 let current = yield* getByName(output?.name ?? name);
335
336 if (current === undefined) {
337 const created = yield* logging
338 .createBillingAccountsSinks({
339 parent: billingAccountParent(billingAccountId),
340 uniqueWriterIdentity: news.uniqueWriterIdentity,
341 customWriterIdentity: news.customWriterIdentity,
342 body: toCreateBody(sinkId, news, desiredDescription),
343 })
344 .pipe(Effect.catchTag("Conflict", () => getByName(name)));
345 current = created ?? undefined;
346 }
347
348 if (current === undefined) {
349 return yield* new BillingSinkNotResolved({ resourceName: name });
350 }
351
352 const desiredDisabled = news.disabled === true;
353 const desiredBq = news.bigqueryOptions?.usePartitionedTables === true;
354 const observedBq = current.bigqueryOptions?.usePartitionedTables === true;
355
356 const destinationChanged = (current.destination ?? "") !== news.destination;
357 const filterChanged = (current.filter ?? "") !== (news.filter ?? "");
358 const descriptionChanged = (current.description ?? "") !== desiredDescription;
359 const disabledChanged = (current.disabled === true) !== desiredDisabled;
360 const exclusionsChanged = !sameExclusions(current.exclusions, news.exclusions);
361 const bqChanged = news.bigqueryOptions !== undefined && desiredBq !== observedBq;
362
363 const updateMask = [
364 destinationChanged ? "destination" : undefined,
365 filterChanged ? "filter" : undefined,
366 descriptionChanged ? "description" : undefined,
367 disabledChanged ? "disabled" : undefined,
368 exclusionsChanged ? "exclusions" : undefined,
369 bqChanged ? "bigqueryOptions.usePartitionedTables" : undefined,
370 ].filter((field): field is string => field !== undefined);
371
372 if (updateMask.length > 0) {
373 const body: logging.LogSink = {
374 name: sinkId,
375 destination: news.destination,
376 filter: news.filter ?? "",
377 description: desiredDescription,
378 disabled: desiredDisabled,
379 };
380 if (exclusionsChanged) {
381 body.exclusions = toExclusionsBody(news.exclusions) ?? [];
382 }
383 if (bqChanged) {
384 body.bigqueryOptions = {
385 usePartitionedTables: desiredBq,
386 };
387 }
388 current = yield* logging.patchBillingAccountsSinks({
389 sinkName: current.resourceName ?? name,
390 uniqueWriterIdentity: news.uniqueWriterIdentity,
391 customWriterIdentity: news.customWriterIdentity,
392 updateMask: updateMask.join(","),
393 body,
394 });
395 }
396
397 return toAttrs(current, billingAccountId);
398 }),
399
400 delete: Effect.fn(function* ({ output }) {
401 yield* logging
402 .deleteBillingAccountsSinks({ sinkName: output.name })
403 .pipe(Effect.catchTag("NotFound", () => Effect.void));
404 }),
405 });