The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as apigee from "@distilled.cloud/gcp/apigee_v1"; |
| 2 | import { isResolved } from "alchemy/Diff"; |
| 3 | import * as Provider from "alchemy/Provider"; |
| 4 | import { Resource } from "alchemy/Resource"; |
| 5 | import * as Data from "effect/Data"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Stream from "effect/Stream"; |
| 8 | import { GcpEnvironment } from "../Environment.ts"; |
| 9 | import type { Providers } from "../Providers.ts"; |
| 10 | import { |
| 11 | environmentIdOf, |
| 12 | environmentNameOf, |
| 13 | lastSegment, |
| 14 | listProjectEnvironments, |
| 15 | missingToUndefined, |
| 16 | organizationIdOf, |
| 17 | parseOrgEnv, |
| 18 | sameText, |
| 19 | } from "./common.ts"; |
| 20 | |
| 21 | export type TraceSamplingConfig = { |
| 22 | /** |
| 23 | * Sampler. `OFF` is the default; `PROBABILITY` uses `samplingRate`. |
| 24 | */ |
| 25 | sampler?: apigee.GoogleCloudApigeeV1TraceSamplingConfigSamplerEnum | (string & {}); |
| 26 | /** |
| 27 | * Sampling rate when using `PROBABILITY`. Must be `> 0` and `<= 0.5`. |
| 28 | */ |
| 29 | samplingRate?: number; |
| 30 | }; |
| 31 | |
| 32 | export type EnvironmentsTraceConfigOverrideProps = { |
| 33 | /** |
| 34 | * Apigee organization id or `organizations/{org}`. Defaults to the |
| 35 | * current GCP project id. Immutable — changing it replaces the override. |
| 36 | */ |
| 37 | organization?: string; |
| 38 | /** |
| 39 | * Environment id or `organizations/{org}/environments/{env}`. Immutable — |
| 40 | * changing it replaces the override. |
| 41 | */ |
| 42 | environment: string; |
| 43 | /** |
| 44 | * API proxy whose trace configuration is overridden. |
| 45 | */ |
| 46 | apiProxy: string; |
| 47 | /** |
| 48 | * Trace sampling configuration to apply. |
| 49 | */ |
| 50 | samplingConfig?: TraceSamplingConfig; |
| 51 | }; |
| 52 | |
| 53 | export type EnvironmentsTraceConfigOverride = Resource< |
| 54 | "GCP.Apigee.EnvironmentsTraceConfigOverride", |
| 55 | EnvironmentsTraceConfigOverrideProps, |
| 56 | { |
| 57 | /** Full resource name `organizations/{org}/environments/{env}/traceConfig/overrides/{id}`. */ |
| 58 | name: string; |
| 59 | /** System-generated override id (UUID). */ |
| 60 | traceConfigOverrideId: string; |
| 61 | /** Apigee organization id. */ |
| 62 | organizationId: string; |
| 63 | /** Environment id. */ |
| 64 | environmentId: string; |
| 65 | /** API proxy id. */ |
| 66 | apiProxy: string; |
| 67 | /** Sampling configuration. */ |
| 68 | samplingConfig: TraceSamplingConfig | undefined; |
| 69 | }, |
| 70 | never, |
| 71 | Providers |
| 72 | >; |
| 73 | |
| 74 | /** |
| 75 | * A distributed-trace configuration override for one API proxy in an |
| 76 | * Apigee environment. |
| 77 | * |
| 78 | * Overrides have no labels; `list` enumerates every override in Apigee |
| 79 | * environments mapped to this GCP project. The override id is assigned by |
| 80 | * Apigee. Organization and environment are identity; `apiProxy` and |
| 81 | * `samplingConfig` update in place. |
| 82 | * |
| 83 | * ### Creating an Override |
| 84 | * **Example:** Probability sampler |
| 85 | * ```typescript |
| 86 | * const override = yield* GCP.Apigee.EnvironmentsTraceConfigOverride("ProxyTrace", { |
| 87 | * environment: "eval", |
| 88 | * apiProxy: "hello", |
| 89 | * samplingConfig: { sampler: "PROBABILITY", samplingRate: 0.1 }, |
| 90 | * }); |
| 91 | * ``` |
| 92 | * |
| 93 | * @resource |
| 94 | * @category Apigee |
| 95 | */ |
| 96 | export const EnvironmentsTraceConfigOverride = Resource<EnvironmentsTraceConfigOverride>( |
| 97 | "GCP.Apigee.EnvironmentsTraceConfigOverride", |
| 98 | ); |
| 99 | |
| 100 | export class EnvironmentsTraceConfigOverrideNotResolved extends Data.TaggedError( |
| 101 | "GCP.Apigee.EnvironmentsTraceConfigOverrideNotResolved", |
| 102 | )<{ |
| 103 | resourceName: string; |
| 104 | }> {} |
| 105 | |
| 106 | const traceConfigParent = (organizationId: string, environmentId: string) => |
| 107 | `${environmentNameOf(organizationId, environmentId)}/traceConfig`; |
| 108 | |
| 109 | const samplingOf = ( |
| 110 | config: apigee.GoogleCloudApigeeV1TraceSamplingConfig | undefined, |
| 111 | ): TraceSamplingConfig | undefined => { |
| 112 | if (config === undefined) return undefined; |
| 113 | return { sampler: config.sampler, samplingRate: config.samplingRate }; |
| 114 | }; |
| 115 | |
| 116 | const toAttrs = ( |
| 117 | override: apigee.GoogleCloudApigeeV1TraceConfigOverride, |
| 118 | organizationId: string, |
| 119 | environmentId: string, |
| 120 | ) => { |
| 121 | const raw = override.name ?? ""; |
| 122 | const parsed = parseOrgEnv(raw); |
| 123 | return { |
| 124 | name: raw.includes("/") |
| 125 | ? raw |
| 126 | : `${traceConfigParent(organizationId, environmentId)}/overrides/${raw}`, |
| 127 | traceConfigOverrideId: lastSegment(raw), |
| 128 | organizationId: parsed.organizationId || organizationId, |
| 129 | environmentId: parsed.environmentId || environmentId, |
| 130 | apiProxy: override.apiProxy ?? "", |
| 131 | samplingConfig: samplingOf(override.samplingConfig), |
| 132 | }; |
| 133 | }; |
| 134 | |
| 135 | const getByName = (name: string) => |
| 136 | missingToUndefined(apigee.getOrganizationsEnvironmentsTraceConfigOverrides({ name })); |
| 137 | |
| 138 | const listOverrides = (parent: string) => |
| 139 | apigee.listOrganizationsEnvironmentsTraceConfigOverrides.pages({ parent, pageSize: 100 }).pipe( |
| 140 | Stream.flatMap((page) => Stream.fromIterable(page.traceConfigOverrides ?? [])), |
| 141 | Stream.runCollect, |
| 142 | Effect.map((chunk) => Array.from(chunk)), |
| 143 | Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => |
| 144 | Effect.succeed([] as apigee.GoogleCloudApigeeV1TraceConfigOverride[]), |
| 145 | ), |
| 146 | ); |
| 147 | |
| 148 | const jsonOf = (value: unknown) => JSON.stringify(value ?? null); |
| 149 | |
| 150 | export const EnvironmentsTraceConfigOverrideProvider = () => |
| 151 | Provider.succeed(EnvironmentsTraceConfigOverride, { |
| 152 | stables: ["name", "traceConfigOverrideId", "organizationId", "environmentId"], |
| 153 | |
| 154 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 155 | if (!isResolved(news)) return undefined; |
| 156 | const previousOrg = olds?.organization ?? output?.organizationId; |
| 157 | const previousEnv = olds?.environment ?? output?.environmentId; |
| 158 | const orgChanged = |
| 159 | previousOrg !== undefined && |
| 160 | news.organization !== undefined && |
| 161 | organizationIdOf(news.organization, "") !== organizationIdOf(previousOrg, ""); |
| 162 | const envChanged = |
| 163 | previousEnv !== undefined && |
| 164 | environmentIdOf(news.environment) !== environmentIdOf(previousEnv); |
| 165 | if (orgChanged || envChanged) { |
| 166 | return { action: "replace" as const, deleteFirst: false }; |
| 167 | } |
| 168 | return undefined; |
| 169 | }), |
| 170 | |
| 171 | read: Effect.fn(function* ({ olds, output }) { |
| 172 | const { project } = yield* GcpEnvironment.current; |
| 173 | const organizationId = organizationIdOf( |
| 174 | olds?.organization ?? output?.organizationId, |
| 175 | project, |
| 176 | ); |
| 177 | const environmentId = environmentIdOf(olds?.environment ?? output?.environmentId ?? ""); |
| 178 | if (output?.name) { |
| 179 | const existing = yield* getByName(output.name); |
| 180 | if (existing === undefined) return undefined; |
| 181 | return toAttrs(existing, organizationId, environmentId); |
| 182 | } |
| 183 | const listed = yield* listOverrides(traceConfigParent(organizationId, environmentId)); |
| 184 | const match = listed.find((item) => item.apiProxy === (olds?.apiProxy ?? "")); |
| 185 | if (match === undefined) return undefined; |
| 186 | return toAttrs(match, organizationId, environmentId); |
| 187 | }), |
| 188 | |
| 189 | list: () => |
| 190 | Effect.gen(function* () { |
| 191 | const environments = yield* listProjectEnvironments(); |
| 192 | const found: EnvironmentsTraceConfigOverride["Attributes"][] = []; |
| 193 | for (const item of environments) { |
| 194 | const listed = yield* listOverrides( |
| 195 | traceConfigParent(item.organizationId, item.environmentId), |
| 196 | ); |
| 197 | for (const override of listed) { |
| 198 | found.push(toAttrs(override, item.organizationId, item.environmentId)); |
| 199 | } |
| 200 | } |
| 201 | return found; |
| 202 | }), |
| 203 | |
| 204 | reconcile: Effect.fn(function* ({ news, output }) { |
| 205 | const { project } = yield* GcpEnvironment.current; |
| 206 | const organizationId = organizationIdOf(news.organization, project); |
| 207 | const environmentId = environmentIdOf(news.environment); |
| 208 | const parent = traceConfigParent(organizationId, environmentId); |
| 209 | |
| 210 | let current = output?.name !== undefined ? yield* getByName(output.name) : undefined; |
| 211 | if (current === undefined) { |
| 212 | const listed = yield* listOverrides(parent); |
| 213 | current = listed.find((item) => item.apiProxy === news.apiProxy); |
| 214 | } |
| 215 | |
| 216 | if (current === undefined) { |
| 217 | const created = yield* apigee |
| 218 | .createOrganizationsEnvironmentsTraceConfigOverrides({ |
| 219 | parent, |
| 220 | body: { |
| 221 | apiProxy: news.apiProxy, |
| 222 | samplingConfig: news.samplingConfig, |
| 223 | }, |
| 224 | }) |
| 225 | .pipe( |
| 226 | Effect.catchTag("Conflict", () => |
| 227 | listOverrides(parent).pipe( |
| 228 | Effect.map((items) => items.find((item) => item.apiProxy === news.apiProxy)), |
| 229 | ), |
| 230 | ), |
| 231 | ); |
| 232 | current = created ?? undefined; |
| 233 | } |
| 234 | |
| 235 | if (current === undefined || !current.name) { |
| 236 | return yield* new EnvironmentsTraceConfigOverrideNotResolved({ |
| 237 | resourceName: `${parent}/overrides`, |
| 238 | }); |
| 239 | } |
| 240 | |
| 241 | const proxyChanged = !sameText(current.apiProxy, news.apiProxy); |
| 242 | const samplingChanged = |
| 243 | jsonOf(samplingOf(current.samplingConfig)) !== jsonOf(news.samplingConfig); |
| 244 | if (proxyChanged || samplingChanged) { |
| 245 | current = yield* apigee.patchOrganizationsEnvironmentsTraceConfigOverrides({ |
| 246 | name: current.name, |
| 247 | updateMask: [ |
| 248 | proxyChanged ? "api_proxy" : undefined, |
| 249 | samplingChanged ? "sampling_config" : undefined, |
| 250 | ] |
| 251 | .filter((field): field is string => field !== undefined) |
| 252 | .join(","), |
| 253 | body: { |
| 254 | apiProxy: news.apiProxy, |
| 255 | samplingConfig: news.samplingConfig, |
| 256 | }, |
| 257 | }); |
| 258 | } |
| 259 | |
| 260 | return toAttrs(current, organizationId, environmentId); |
| 261 | }), |
| 262 | |
| 263 | delete: Effect.fn(function* ({ output }) { |
| 264 | yield* apigee |
| 265 | .deleteOrganizationsEnvironmentsTraceConfigOverrides({ |
| 266 | name: output.name, |
| 267 | }) |
| 268 | .pipe(Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => Effect.void)); |
| 269 | }), |
| 270 | }); |