| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import * as logging from "@distilled.cloud/gcp/logging_v2"; |
| 4 | import { expect } from "alchemy-test"; |
| 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( |
| 12 | MinimumLogLevel, |
| 13 | process.env.DEBUG ? "Debug" : "Info", |
| 14 | ); |
| 15 | |
| 16 | const waitUntilGone = (metricName: string) => |
| 17 | logging.getProjectsMetrics({ metricName }).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, replace, and delete a logs-based metric", |
| 29 | (stack) => |
| 30 | Effect.gen(function* () { |
| 31 | yield* stack.destroy(); |
| 32 | |
| 33 | const created = yield* stack.deploy( |
| 34 | Effect.gen(function* () { |
| 35 | return yield* GCP.Logging.Metric("Errors", { |
| 36 | filter: "severity>=ERROR", |
| 37 | description: "count errors", |
| 38 | }); |
| 39 | }), |
| 40 | ); |
| 41 | |
| 42 | expect(created.metricId).toEqual(expect.any(String)); |
| 43 | expect(created.name).toContain("/metrics/"); |
| 44 | expect(created.filter).toEqual("severity>=ERROR"); |
| 45 | expect(created.description).toEqual("count errors"); |
| 46 | expect(created.disabled).toEqual(false); |
| 47 | expect(created.metricDescriptor?.metricKind).toEqual("DELTA"); |
| 48 | expect(created.metricDescriptor?.valueType).toEqual("INT64"); |
| 49 | |
| 50 | const fetched = yield* logging.getProjectsMetrics({ |
| 51 | metricName: created.name, |
| 52 | }); |
| 53 | expect(fetched.name).toEqual(created.metricId); |
| 54 | expect(fetched.filter).toEqual("severity>=ERROR"); |
| 55 | expect(fetched.description).toContain("alchemy-id="); |
| 56 | expect(fetched.description).toContain("count errors"); |
| 57 | expect(fetched.disabled ?? false).toEqual(false); |
| 58 | |
| 59 | const updated = yield* stack.deploy( |
| 60 | Effect.gen(function* () { |
| 61 | return yield* GCP.Logging.Metric("Errors", { |
| 62 | metricId: created.metricId, |
| 63 | filter: "severity>=WARNING", |
| 64 | description: "count warnings", |
| 65 | disabled: true, |
| 66 | }); |
| 67 | }), |
| 68 | ); |
| 69 | |
| 70 | expect(updated.name).toEqual(created.name); |
| 71 | expect(updated.metricId).toEqual(created.metricId); |
| 72 | expect(updated.filter).toEqual("severity>=WARNING"); |
| 73 | expect(updated.description).toEqual("count warnings"); |
| 74 | expect(updated.disabled).toEqual(true); |
| 75 | expect(updated.createTime).toEqual(created.createTime); |
| 76 | |
| 77 | const fetchedUpdate = yield* logging.getProjectsMetrics({ |
| 78 | metricName: updated.name, |
| 79 | }); |
| 80 | expect(fetchedUpdate.filter).toEqual("severity>=WARNING"); |
| 81 | expect(fetchedUpdate.disabled).toEqual(true); |
| 82 | expect(fetchedUpdate.description).toContain("count warnings"); |
| 83 | |
| 84 | const last = created.metricId.at(-1) ?? "a"; |
| 85 | const nextMetricId = `${created.metricId.slice(0, -1)}${last === "z" ? "0" : "z"}`; |
| 86 | |
| 87 | const replaced = yield* stack.deploy( |
| 88 | Effect.gen(function* () { |
| 89 | return yield* GCP.Logging.Metric("Errors", { |
| 90 | metricId: nextMetricId, |
| 91 | filter: "severity>=ERROR", |
| 92 | description: "replaced counter", |
| 93 | }); |
| 94 | }), |
| 95 | ); |
| 96 | |
| 97 | expect(replaced.metricId).not.toEqual(created.metricId); |
| 98 | expect(replaced.filter).toEqual("severity>=ERROR"); |
| 99 | expect(replaced.disabled).toEqual(false); |
| 100 | expect(replaced.description).toEqual("replaced counter"); |
| 101 | |
| 102 | const fetchedReplace = yield* logging.getProjectsMetrics({ |
| 103 | metricName: replaced.name, |
| 104 | }); |
| 105 | expect(fetchedReplace.name).toEqual(replaced.metricId); |
| 106 | expect(fetchedReplace.filter).toEqual("severity>=ERROR"); |
| 107 | |
| 108 | const oldGone = yield* waitUntilGone(created.name); |
| 109 | expect(oldGone).toEqual("gone"); |
| 110 | |
| 111 | yield* stack.destroy(); |
| 112 | |
| 113 | const gone = yield* waitUntilGone(replaced.name); |
| 114 | expect(gone).toEqual("gone"); |
| 115 | }).pipe(logLevel), |
| 116 | { tags: ["provider:gcp", "provider:gcp:logging", "live"], timeout: 90_000 }, |
| 117 | ); |