The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as GCP from "@alchemy.run/gcp"; |
| 2 | import { expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as monitoring from "@distilled.cloud/gcp/monitoring_v3"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import { MinimumLogLevel } from "effect/References"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | |
| 9 | const { test } = Test.make({ providers: GCP.providers() }); |
| 10 | |
| 11 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 12 | |
| 13 | const latencyFilter = |
| 14 | 'metric.type="serviceruntime.googleapis.com/api/request_latencies" AND resource.type="consumed_api"'; |
| 15 | |
| 16 | const waitUntilGone = (name: string) => |
| 17 | monitoring.getServicesServiceLevelObjectives({ name }).pipe( |
| 18 | Effect.as("found" as const), |
| 19 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 20 | Effect.repeat({ |
| 21 | schedule: Schedule.spaced("1 second"), |
| 22 | until: (status) => status === "gone", |
| 23 | times: 10, |
| 24 | }), |
| 25 | ); |
| 26 | |
| 27 | test.provider( |
| 28 | "create, update, and delete a service level objective", |
| 29 | (stack) => |
| 30 | Effect.gen(function* () { |
| 31 | yield* stack.destroy(); |
| 32 | |
| 33 | const created = yield* stack.deploy( |
| 34 | Effect.gen(function* () { |
| 35 | const checkout = yield* GCP.Monitoring.Service("Checkout", { |
| 36 | displayName: "Checkout", |
| 37 | labels: { env: "test" }, |
| 38 | }); |
| 39 | const slo = yield* GCP.Monitoring.ServicesServiceLevelObjective("Latency", { |
| 40 | service: checkout.name, |
| 41 | displayName: "99% latency", |
| 42 | goal: 0.99, |
| 43 | rollingPeriod: "86400s", |
| 44 | serviceLevelIndicator: { |
| 45 | requestBased: { |
| 46 | distributionCut: { |
| 47 | distributionFilter: latencyFilter, |
| 48 | range: { max: 500 }, |
| 49 | }, |
| 50 | }, |
| 51 | }, |
| 52 | labels: { env: "test" }, |
| 53 | }); |
| 54 | return { checkout, slo }; |
| 55 | }), |
| 56 | ); |
| 57 | |
| 58 | expect(created.slo.name).toContain("/serviceLevelObjectives/"); |
| 59 | expect(created.slo.service).toEqual(created.checkout.name); |
| 60 | expect(created.slo.displayName).toEqual("99% latency"); |
| 61 | expect(created.slo.goal).toEqual(0.99); |
| 62 | expect(created.slo.rollingPeriod).toEqual("86400s"); |
| 63 | expect(created.slo.labels).toMatchObject({ env: "test" }); |
| 64 | expect(created.slo.serviceLevelIndicator?.requestBased?.distributionCut?.range?.max).toEqual( |
| 65 | 500, |
| 66 | ); |
| 67 | |
| 68 | const fetched = yield* monitoring.getServicesServiceLevelObjectives({ |
| 69 | name: created.slo.name, |
| 70 | }); |
| 71 | expect(fetched.name).toEqual(created.slo.name); |
| 72 | expect(fetched.goal).toEqual(0.99); |
| 73 | expect(fetched.userLabels?.env).toEqual("test"); |
| 74 | expect( |
| 75 | Object.keys(fetched.userLabels ?? {}).some((key) => key.startsWith("alchemy-")), |
| 76 | ).toEqual(true); |
| 77 | |
| 78 | const updated = yield* stack.deploy( |
| 79 | Effect.gen(function* () { |
| 80 | const checkout = yield* GCP.Monitoring.Service("Checkout", { |
| 81 | serviceId: created.checkout.serviceId, |
| 82 | displayName: "Checkout", |
| 83 | labels: { env: "test" }, |
| 84 | }); |
| 85 | const slo = yield* GCP.Monitoring.ServicesServiceLevelObjective("Latency", { |
| 86 | service: checkout.name, |
| 87 | serviceLevelObjectiveId: created.slo.serviceLevelObjectiveId, |
| 88 | displayName: "95% latency", |
| 89 | goal: 0.95, |
| 90 | rollingPeriod: "604800s", |
| 91 | serviceLevelIndicator: { |
| 92 | requestBased: { |
| 93 | distributionCut: { |
| 94 | distributionFilter: latencyFilter, |
| 95 | range: { max: 1000 }, |
| 96 | }, |
| 97 | }, |
| 98 | }, |
| 99 | labels: { env: "prod" }, |
| 100 | }); |
| 101 | return { checkout, slo }; |
| 102 | }), |
| 103 | ); |
| 104 | |
| 105 | expect(updated.slo.name).toEqual(created.slo.name); |
| 106 | expect(updated.slo.displayName).toEqual("95% latency"); |
| 107 | expect(updated.slo.goal).toEqual(0.95); |
| 108 | expect(updated.slo.rollingPeriod).toEqual("604800s"); |
| 109 | expect(updated.slo.serviceLevelIndicator?.requestBased?.distributionCut?.range?.max).toEqual( |
| 110 | 1000, |
| 111 | ); |
| 112 | expect(updated.slo.labels).toMatchObject({ env: "prod" }); |
| 113 | |
| 114 | const fetchedUpdate = yield* monitoring.getServicesServiceLevelObjectives({ |
| 115 | name: updated.slo.name, |
| 116 | }); |
| 117 | expect(fetchedUpdate.goal).toEqual(0.95); |
| 118 | expect(fetchedUpdate.displayName).toEqual("95% latency"); |
| 119 | expect(fetchedUpdate.userLabels?.env).toEqual("prod"); |
| 120 | |
| 121 | yield* stack.destroy(); |
| 122 | |
| 123 | const gone = yield* waitUntilGone(created.slo.name); |
| 124 | expect(gone).toEqual("gone"); |
| 125 | }).pipe(logLevel), |
| 126 | { |
| 127 | tags: ["provider:gcp", "provider:gcp:monitoring", "live"], |
| 128 | timeout: 90_000, |
| 129 | }, |
| 130 | ); |