The best way to control Claude Code, Codex and more from any computer or phone

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