@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, 04:45 UTC

main
ts 594 lines 19.3 KB
Raw
1 import * as monitoring from "@distilled.cloud/gcp/monitoring_v3";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import { createPhysicalName } from "alchemy/PhysicalName";
5 import * as Provider from "alchemy/Provider";
6 import { Resource } from "alchemy/Resource";
7 import { tagRecord } from "alchemy/Tags";
8 import * as Data from "effect/Data";
9 import * as Effect from "effect/Effect";
10 import * as Stream from "effect/Stream";
11 import { GcpEnvironment } from "../Environment.ts";
12 import {
13 createInternalLabels,
14 diffLabels,
15 hasAlchemyLabels,
16 stripInternalLabels,
17 toLabels,
18 } from "../Labels.ts";
19 import type { Providers } from "../Providers.ts";
20 import { jsonEqual, lastSegment, parentOf, toEmptyObject } from "./ownership.ts";
21
22 const MAX_SLO_ID_LENGTH = 63;
23 const DEFAULT_ROLLING_PERIOD = "86400s";
24
25 export type LatencyCriteria = {
26 /** Requests at or below this latency count as good. */
27 threshold?: string;
28 };
29
30 export type BasicSli = {
31 /** Locations this SLI applies to. Omitted means all. */
32 location?: string[];
33 /** Availability SLI (successful request count). */
34 availability?: Record<string, never>;
35 /** Latency SLI against `threshold`. */
36 latency?: LatencyCriteria;
37 /** RPC methods this SLI applies to. Omitted means all. */
38 method?: string[];
39 /** API versions this SLI applies to. Omitted means all. */
40 version?: string[];
41 };
42
43 export type ValueRange = {
44 /** Inclusive maximum. Omit (or use Infinity) for an open upper bound. */
45 max?: number;
46 /** Inclusive minimum. Omit (or use -Infinity) for an open lower bound. */
47 min?: number;
48 };
49
50 export type MetricRange = {
51 /** Range of values considered good. */
52 range?: ValueRange;
53 /** Monitoring filter selecting a GAUGE INT64 or DOUBLE time series. */
54 timeSeries?: string;
55 };
56
57 export type DistributionCut = {
58 /** Range of distribution bucket values considered good. */
59 range?: ValueRange;
60 /**
61 * Monitoring filter selecting a DISTRIBUTION DELTA or CUMULATIVE
62 * time series.
63 */
64 distributionFilter?: string;
65 };
66
67 export type TimeSeriesRatio = {
68 /** Filter quantifying good service. */
69 goodServiceFilter?: string;
70 /** Filter quantifying bad service. */
71 badServiceFilter?: string;
72 /** Filter quantifying total demanded service. */
73 totalServiceFilter?: string;
74 };
75
76 export type RequestBasedSli = {
77 /** Good service is the count of distribution values in `range`. */
78 distributionCut?: DistributionCut;
79 /** Good / total ratio from a pair of time series. */
80 goodTotalRatio?: TimeSeriesRatio;
81 };
82
83 export type PerformanceThreshold = {
84 /** Window is good when performance is at least this fraction. */
85 threshold?: number;
86 /** Request-based SLI used to judge the window. */
87 performance?: RequestBasedSli;
88 /** Basic SLI used to judge the window. */
89 basicSliPerformance?: BasicSli;
90 };
91
92 export type WindowsBasedSli = {
93 /**
94 * Window duration. Must be an integer fraction of a day and at least
95 * 60s.
96 */
97 windowPeriod?: string;
98 /** Window is good when the summed metric is in `range`. */
99 metricSumInRange?: MetricRange;
100 /** BOOL time series; the window is good if any true value appears. */
101 goodBadMetricFilter?: string;
102 /** Window is good when performance is above `threshold`. */
103 goodTotalRatioThreshold?: PerformanceThreshold;
104 /** Window is good when the mean metric is in `range`. */
105 metricMeanInRange?: MetricRange;
106 };
107
108 export type ServiceLevelIndicator = {
109 /** Basic SLI on a well-known service type. */
110 basicSli?: BasicSli;
111 /** Windows-based SLI. */
112 windowsBased?: WindowsBasedSli;
113 /** Request-based SLI. */
114 requestBased?: RequestBasedSli;
115 };
116
117 export type CalendarPeriod = "DAY" | "WEEK" | "FORTNIGHT" | "MONTH" | (string & {});
118
119 export type ServicesServiceLevelObjectiveProps = {
120 /**
121 * Parent service resource name
122 * (`projects/{project}/services/{service}`) or service id. Immutable
123 * — changing it replaces the SLO.
124 */
125 service: string;
126 /**
127 * SLO id (the last path segment). If omitted, a unique id is
128 * generated from the stack, stage, and logical id. Must match
129 * `[a-zA-Z0-9-_:.]`. Immutable — changing it replaces the SLO.
130 */
131 serviceLevelObjectiveId?: string;
132 /**
133 * Human-readable display name. If omitted, the SLO id is used.
134 */
135 displayName?: string;
136 /**
137 * Fraction of service that must be good (`0 < goal <= 0.9999`).
138 */
139 goal: number;
140 /**
141 * Rolling window (for example `"86400s"`). Mutually exclusive with
142 * `calendarPeriod`. Defaults to `"86400s"` when neither is set.
143 */
144 rollingPeriod?: string;
145 /**
146 * Calendar period (`DAY`, `WEEK`, `FORTNIGHT`, `MONTH`). Mutually
147 * exclusive with `rollingPeriod`.
148 */
149 calendarPeriod?: CalendarPeriod;
150 /**
151 * Service-level indicator defining good service.
152 */
153 serviceLevelIndicator: ServiceLevelIndicator;
154 /**
155 * User labels. Alchemy ownership labels are merged in automatically.
156 */
157 labels?: Record<string, string>;
158 };
159
160 export type ServicesServiceLevelObjective = Resource<
161 "GCP.Monitoring.ServicesServiceLevelObjective",
162 ServicesServiceLevelObjectiveProps,
163 {
164 /** Full resource name `projects/{project}/services/{service}/serviceLevelObjectives/{slo}`. */
165 name: string;
166 /** SLO id (last path segment). */
167 serviceLevelObjectiveId: string;
168 /** Parent service resource name. */
169 service: string;
170 /** Parent service id. */
171 serviceId: string;
172 /** Project id. */
173 project: string;
174 /** Human-readable display name. */
175 displayName: string | undefined;
176 /** Target good-service fraction. */
177 goal: number;
178 /** Rolling window, if this SLO is rolling. */
179 rollingPeriod: string | undefined;
180 /** Calendar period, if this SLO is calendar-based. */
181 calendarPeriod: string | undefined;
182 /** Service-level indicator. */
183 serviceLevelIndicator: ServiceLevelIndicator | undefined;
184 /** User labels (Alchemy ownership labels stripped). */
185 labels: Record<string, string>;
186 },
187 never,
188 Providers
189 >;
190
191 /**
192 * A Cloud Monitoring service-level objective under a Monitoring
193 * Service.
194 *
195 * Alchemy stamps ownership into `userLabels` so `list` /
196 * `pnpm nuke:gcp` can find them. Changing `service` or
197 * `serviceLevelObjectiveId` replaces the SLO. Goal, period, SLI,
198 * display name, and labels update in place.
199 *
200 * ### Creating an SLO
201 * **Example:** Rolling request-latency SLO on a custom service
202 * ```typescript
203 * const checkout = yield* GCP.Monitoring.Service("Checkout", {});
204 * const slo = yield* GCP.Monitoring.ServicesServiceLevelObjective(
205 * "Latency",
206 * {
207 * service: checkout.name,
208 * goal: 0.99,
209 * rollingPeriod: "86400s",
210 * serviceLevelIndicator: {
211 * requestBased: {
212 * distributionCut: {
213 * distributionFilter:
214 * 'metric.type="serviceruntime.googleapis.com/api/request_latencies" AND resource.type="consumed_api"',
215 * range: { max: 500 },
216 * },
217 * },
218 * },
219 * },
220 * );
221 * ```
222 *
223 * ### Updating an SLO
224 * **Example:** Lower the goal and switch to a calendar month
225 * ```typescript
226 * const slo = yield* GCP.Monitoring.ServicesServiceLevelObjective(
227 * "Latency",
228 * {
229 * service: checkout.name,
230 * goal: 0.95,
231 * calendarPeriod: "MONTH",
232 * serviceLevelIndicator: {
233 * requestBased: {
234 * distributionCut: {
235 * distributionFilter:
236 * 'metric.type="serviceruntime.googleapis.com/api/request_latencies" AND resource.type="consumed_api"',
237 * range: { max: 500 },
238 * },
239 * },
240 * },
241 * },
242 * );
243 * ```
244 *
245 * @resource
246 * @category Monitoring
247 */
248 export const ServicesServiceLevelObjective = Resource<ServicesServiceLevelObjective>(
249 "GCP.Monitoring.ServicesServiceLevelObjective",
250 );
251
252 export class ServicesServiceLevelObjectiveNotResolved extends Data.TaggedError(
253 "GCP.Monitoring.ServicesServiceLevelObjectiveNotResolved",
254 )<{
255 resourceName: string;
256 }> {}
257
258 const SERVICES = "/services/";
259
260 const parseSloName = (name: string) => {
261 const parts = name.split("/").filter((part) => part.length > 0);
262 const projectsAt = parts.lastIndexOf("projects");
263 const servicesAt = parts.lastIndexOf("services");
264 const slosAt = parts.lastIndexOf("serviceLevelObjectives");
265 const project = projectsAt >= 0 && parts[projectsAt + 1] ? parts[projectsAt + 1]! : "";
266 const serviceId = servicesAt >= 0 && parts[servicesAt + 1] ? parts[servicesAt + 1]! : "";
267 const service = servicesAt >= 0 ? parts.slice(0, servicesAt + 2).join("/") : "";
268 const serviceLevelObjectiveId =
269 slosAt >= 0 && parts[slosAt + 1] ? parts[slosAt + 1]! : lastSegment(name);
270 return { project, service, serviceId, serviceLevelObjectiveId };
271 };
272
273 const resolveService = (project: string, service: string) => {
274 if (service.includes("/")) return service;
275 return `${parentOf(project)}/services/${service}`;
276 };
277
278 const resourceName = (service: string, sloId: string) =>
279 `${service}/serviceLevelObjectives/${sloId}`;
280
281 const userFacingLabels = (
282 labels: Record<string, string | undefined> | null | undefined,
283 ): Record<string, string> => stripInternalLabels(tagRecord(labels));
284
285 const toId = (id: string, sloId: string | undefined, existing?: string) =>
286 Effect.gen(function* () {
287 if (sloId !== undefined) return sloId;
288 if (existing !== undefined) return existing;
289 const generated = yield* createPhysicalName({
290 id,
291 maxLength: MAX_SLO_ID_LENGTH,
292 lowercase: true,
293 });
294 return generated.replace(/-+$/g, "").slice(0, MAX_SLO_ID_LENGTH);
295 });
296
297 const toBasicSli = (sli: monitoring.BasicSli | undefined): BasicSli | undefined => {
298 if (sli === undefined) return undefined;
299 return {
300 location: sli.location,
301 availability: toEmptyObject(sli.availability),
302 latency: sli.latency,
303 method: sli.method,
304 version: sli.version,
305 };
306 };
307
308 const toPerformanceThreshold = (
309 value: monitoring.PerformanceThreshold | undefined,
310 ): PerformanceThreshold | undefined => {
311 if (value === undefined) return undefined;
312 return {
313 threshold: value.threshold,
314 performance: value.performance,
315 basicSliPerformance: toBasicSli(value.basicSliPerformance),
316 };
317 };
318
319 const toWindowsBasedSli = (
320 sli: monitoring.WindowsBasedSli | undefined,
321 ): WindowsBasedSli | undefined => {
322 if (sli === undefined) return undefined;
323 return {
324 windowPeriod: sli.windowPeriod,
325 metricSumInRange: sli.metricSumInRange,
326 goodBadMetricFilter: sli.goodBadMetricFilter,
327 goodTotalRatioThreshold: toPerformanceThreshold(sli.goodTotalRatioThreshold),
328 metricMeanInRange: sli.metricMeanInRange,
329 };
330 };
331
332 const toSli = (
333 sli: monitoring.ServiceLevelIndicator | undefined,
334 ): ServiceLevelIndicator | undefined => {
335 if (sli === undefined) return undefined;
336 return {
337 basicSli: toBasicSli(sli.basicSli),
338 windowsBased: toWindowsBasedSli(sli.windowsBased),
339 requestBased: sli.requestBased,
340 };
341 };
342
343 const toAttrs = (slo: monitoring.ServiceLevelObjective, project: string) => {
344 const name = slo.name ?? "";
345 const parsed = parseSloName(name);
346 return {
347 name,
348 serviceLevelObjectiveId: parsed.serviceLevelObjectiveId,
349 service: parsed.service,
350 serviceId: parsed.serviceId,
351 project: parsed.project || project,
352 displayName: slo.displayName,
353 goal: slo.goal ?? 0,
354 rollingPeriod: slo.rollingPeriod,
355 calendarPeriod: slo.calendarPeriod,
356 serviceLevelIndicator: toSli(slo.serviceLevelIndicator),
357 labels: userFacingLabels(slo.userLabels),
358 };
359 };
360
361 const periodBody = (
362 news: ServicesServiceLevelObjectiveProps,
363 ): Pick<monitoring.ServiceLevelObjective, "rollingPeriod" | "calendarPeriod"> =>
364 news.calendarPeriod !== undefined
365 ? { calendarPeriod: news.calendarPeriod }
366 : { rollingPeriod: news.rollingPeriod ?? DEFAULT_ROLLING_PERIOD };
367
368 const toBody = (
369 news: ServicesServiceLevelObjectiveProps,
370 displayName: string,
371 desiredLabels: Record<string, string>,
372 ): monitoring.ServiceLevelObjective => ({
373 displayName,
374 goal: news.goal,
375 ...periodBody(news),
376 serviceLevelIndicator: news.serviceLevelIndicator as monitoring.ServiceLevelIndicator | undefined,
377 userLabels: desiredLabels,
378 });
379
380 const getByName = (name: string) =>
381 monitoring
382 .getServicesServiceLevelObjectives({ name })
383 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
384
385 const listSlos = (parent: string) =>
386 monitoring.listServicesServiceLevelObjectives
387 .pages({
388 parent,
389 pageSize: 1000,
390 })
391 .pipe(
392 Stream.flatMap((page) => Stream.fromIterable(page.serviceLevelObjectives ?? [])),
393 Stream.runCollect,
394 Effect.map((chunk) => Array.from(chunk)),
395 Effect.catchTag("NotFound", () => Effect.succeed([] as monitoring.ServiceLevelObjective[])),
396 );
397
398 const listServices = (project: string) =>
399 monitoring.listServices
400 .pages({
401 parent: parentOf(project),
402 pageSize: 1000,
403 })
404 .pipe(
405 Stream.flatMap((page) => Stream.fromIterable(page.services ?? [])),
406 Stream.runCollect,
407 Effect.map((chunk) => Array.from(chunk)),
408 Effect.catchTag("NotFound", () => Effect.succeed([])),
409 );
410
411 const listOwned = (project: string) =>
412 Effect.gen(function* () {
413 const services = yield* monitoring.listServices
414 .pages({
415 parent: parentOf(project),
416 pageSize: 1000,
417 filter: 'identifier_case = "CUSTOM"',
418 })
419 .pipe(
420 Stream.flatMap((page) => Stream.fromIterable(page.services ?? [])),
421 Stream.runCollect,
422 Effect.map((chunk) => Array.from(chunk)),
423 Effect.catchTag("NotFound", () => Effect.succeed([])),
424 );
425 const nested = yield* Effect.forEach(
426 services,
427 (service) =>
428 service.name !== undefined
429 ? listSlos(service.name)
430 : Effect.succeed([] as monitoring.ServiceLevelObjective[]),
431 { concurrency: 5 },
432 );
433 return nested
434 .flat()
435 .filter((slo) => Object.keys(slo.userLabels ?? {}).some((key) => key.startsWith("alchemy-")))
436 .map((slo) => toAttrs(slo, project));
437 });
438
439 const findOwned = (project: string, id: string, service: string | undefined) =>
440 Effect.gen(function* () {
441 const parents =
442 service !== undefined
443 ? [service]
444 : (yield* listServices(project)).flatMap((item) =>
445 item.name !== undefined ? [item.name] : [],
446 );
447 for (const parent of parents) {
448 const slos = yield* listSlos(parent);
449 for (const slo of slos) {
450 if (yield* hasAlchemyLabels(id, tagRecord(slo.userLabels))) {
451 return slo;
452 }
453 }
454 }
455 return undefined;
456 });
457
458 const observe = (
459 project: string,
460 id: string,
461 name: string | undefined,
462 service: string | undefined,
463 ) =>
464 Effect.gen(function* () {
465 if (name !== undefined) {
466 const existing = yield* getByName(name);
467 if (existing !== undefined) return existing;
468 }
469 return yield* findOwned(project, id, service);
470 });
471
472 export const ServicesServiceLevelObjectiveProvider = () =>
473 Provider.succeed(ServicesServiceLevelObjective, {
474 stables: ["name", "serviceLevelObjectiveId", "service", "serviceId", "project"],
475
476 diff: Effect.fn(function* ({ news, olds, output }) {
477 if (!isResolved(news)) return undefined;
478 const previousId = olds?.serviceLevelObjectiveId ?? output?.serviceLevelObjectiveId;
479 const idChanged =
480 news.serviceLevelObjectiveId !== undefined &&
481 previousId !== undefined &&
482 news.serviceLevelObjectiveId !== previousId;
483 const previousService = olds?.service ?? output?.service;
484 const nextService = news.service;
485 const serviceChanged =
486 previousService !== undefined &&
487 nextService !== previousService &&
488 !previousService.endsWith(`/${nextService}`) &&
489 !nextService.endsWith(`/${previousService.split(SERVICES).pop() ?? ""}`);
490 if (!idChanged && !serviceChanged) return undefined;
491 return {
492 action: "replace" as const,
493 deleteFirst: !idChanged,
494 };
495 }),
496
497 read: Effect.fn(function* ({ id, output }) {
498 const env = yield* GcpEnvironment.current;
499 const existing = yield* observe(env.project, id, output?.name, output?.service);
500 if (existing === undefined) return undefined;
501 const attrs = toAttrs(existing, env.project);
502 return (yield* hasAlchemyLabels(id, tagRecord(existing.userLabels))) ? attrs : Unowned(attrs);
503 }),
504
505 list: () =>
506 Effect.gen(function* () {
507 const env = yield* GcpEnvironment.current;
508 return yield* listOwned(env.project);
509 }),
510
511 reconcile: Effect.fn(function* ({ id, news, output }) {
512 const env = yield* GcpEnvironment.current;
513 const service = resolveService(env.project, news.service);
514 const sloId = yield* toId(id, news.serviceLevelObjectiveId, output?.serviceLevelObjectiveId);
515 const name = resourceName(service, sloId);
516 const displayName = news.displayName ?? sloId;
517 const desiredLabels = {
518 ...toLabels(news.labels),
519 ...(yield* createInternalLabels(id)),
520 };
521 const desiredPeriod = periodBody(news);
522
523 let current = yield* observe(env.project, id, output?.name ?? name, service);
524
525 const body = toBody(news, displayName, desiredLabels);
526
527 if (current === undefined) {
528 const created = yield* monitoring
529 .createServicesServiceLevelObjectives({
530 parent: service,
531 serviceLevelObjectiveId: sloId,
532 body,
533 })
534 .pipe(
535 Effect.catchTag("Conflict", () =>
536 getByName(name).pipe(
537 Effect.flatMap((existing) =>
538 existing !== undefined
539 ? Effect.succeed(existing)
540 : findOwned(env.project, id, service),
541 ),
542 ),
543 ),
544 );
545 current = created ?? undefined;
546 }
547
548 if (current === undefined) {
549 return yield* new ServicesServiceLevelObjectiveNotResolved({ resourceName: name });
550 }
551
552 const resource = current.name ?? name;
553 const observedLabels = tagRecord(current.userLabels);
554 const { upsert, removed } = diffLabels(observedLabels, desiredLabels);
555 const labelsChanged = upsert.length > 0 || removed.length > 0;
556 const displayNameChanged = (current.displayName ?? "") !== displayName;
557 const goalChanged = (current.goal ?? 0) !== news.goal;
558 const rollingChanged = (current.rollingPeriod ?? "") !== (desiredPeriod.rollingPeriod ?? "");
559 const calendarChanged =
560 (current.calendarPeriod ?? "") !== (desiredPeriod.calendarPeriod ?? "");
561 const sliChanged = !jsonEqual(
562 toSli(current.serviceLevelIndicator) ?? null,
563 news.serviceLevelIndicator,
564 );
565
566 const updateMask = [
567 displayNameChanged ? "display_name" : undefined,
568 labelsChanged ? "user_labels" : undefined,
569 goalChanged ? "goal" : undefined,
570 rollingChanged ? "rolling_period" : undefined,
571 calendarChanged ? "calendar_period" : undefined,
572 sliChanged ? "service_level_indicator" : undefined,
573 ].filter((field): field is string => field !== undefined);
574
575 if (updateMask.length > 0) {
576 current = yield* monitoring.patchServicesServiceLevelObjectives({
577 name: resource,
578 updateMask: updateMask.join(","),
579 body: {
580 ...body,
581 name: resource,
582 },
583 });
584 }
585
586 return toAttrs(current, env.project);
587 }),
588
589 delete: Effect.fn(function* ({ output }) {
590 yield* monitoring
591 .deleteServicesServiceLevelObjectives({ name: output.name })
592 .pipe(Effect.catchTag("NotFound", () => Effect.void));
593 }),
594 });