The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { describe, expect, it } from "@alchemy.run/test"; |
| 2 | import * as ConfigProvider from "effect/ConfigProvider"; |
| 3 | import * as Context from "effect/Context"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as HttpClient from "effect/http/HttpClient"; |
| 6 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 7 | import * as HttpClientResponse from "effect/http/HttpClientResponse"; |
| 8 | import * as Layer from "effect/Layer"; |
| 9 | import * as Metric from "effect/Metric"; |
| 10 | import * as Redacted from "effect/Redacted"; |
| 11 | import { packEnvValue } from "@/RuntimeContext.ts"; |
| 12 | import { buildEventTelemetry, EXPORTERS_KEY } from "@/TelemetryRuntime.ts"; |
| 13 | |
| 14 | const runEvent = ( |
| 15 | provider: ConfigProvider.ConfigProvider, |
| 16 | client: HttpClient.HttpClient, |
| 17 | override?: Layer.Layer<never>, |
| 18 | ) => { |
| 19 | const context = Context.make(ConfigProvider.ConfigProvider, provider).pipe( |
| 20 | Context.add(HttpClient.HttpClient, client), |
| 21 | ); |
| 22 | return Effect.scoped( |
| 23 | Effect.gen(function* () { |
| 24 | const telemetry = yield* buildEventTelemetry(context, yield* Effect.scope, override); |
| 25 | yield* Effect.void.pipe(Effect.withSpan("event"), Effect.provideContext(telemetry)); |
| 26 | yield* Metric.update(eventCounter, 1).pipe(Effect.provideContext(telemetry)); |
| 27 | }), |
| 28 | ).pipe(Effect.provideContext(context)); |
| 29 | }; |
| 30 | |
| 31 | const eventCounter = Metric.counter("telemetry_runtime_test_events_total"); |
| 32 | |
| 33 | const recordingClient = (requests: HttpClientRequest.HttpClientRequest[]) => |
| 34 | HttpClient.make((request) => |
| 35 | Effect.sync(() => { |
| 36 | requests.push(request); |
| 37 | return HttpClientResponse.fromWeb(request, Response.json({})); |
| 38 | }), |
| 39 | ); |
| 40 | |
| 41 | describe("event telemetry configuration", { tags: ["unit", "local"] }, () => { |
| 42 | it.effect("reads each destination setting once per event and releases custom telemetry", () => |
| 43 | Effect.gen(function* () { |
| 44 | const reads: string[] = []; |
| 45 | const requests: HttpClientRequest.HttpClientRequest[] = []; |
| 46 | let acquired = 0; |
| 47 | let released = 0; |
| 48 | const provider = ConfigProvider.make((path) => |
| 49 | Effect.sync(() => { |
| 50 | reads.push(path.join("_")); |
| 51 | return undefined; |
| 52 | }), |
| 53 | ); |
| 54 | const custom = Layer.effectDiscard( |
| 55 | Effect.acquireRelease( |
| 56 | Effect.sync(() => acquired++), |
| 57 | () => Effect.sync(() => released++), |
| 58 | ), |
| 59 | ); |
| 60 | for (let event = 1; event <= 2; event++) { |
| 61 | reads.length = 0; |
| 62 | yield* runEvent(provider, recordingClient(requests), custom); |
| 63 | expect(reads.sort()).toEqual([ |
| 64 | EXPORTERS_KEY, |
| 65 | "OTEL_EXPORTER_OTLP_ENDPOINT", |
| 66 | "OTEL_EXPORTER_OTLP_LOGS_ENDPOINT", |
| 67 | "OTEL_EXPORTER_OTLP_METRICS_ENDPOINT", |
| 68 | "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT", |
| 69 | ]); |
| 70 | expect(acquired).toBe(event); |
| 71 | expect(released).toBe(event); |
| 72 | } |
| 73 | expect(requests).toHaveLength(0); |
| 74 | }), |
| 75 | ); |
| 76 | |
| 77 | it.effect("rereads mutable scalar configuration and flushes each event to its destination", () => |
| 78 | Effect.gen(function* () { |
| 79 | const values = new Map<string, ConfigProvider.Node>(); |
| 80 | const provider = ConfigProvider.make((path) => Effect.sync(() => values.get(path.join("_")))); |
| 81 | const requests: HttpClientRequest.HttpClientRequest[] = []; |
| 82 | const client = recordingClient(requests); |
| 83 | yield* runEvent(provider, client); |
| 84 | expect(requests).toHaveLength(0); |
| 85 | |
| 86 | values.set( |
| 87 | "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT", |
| 88 | ConfigProvider.makeRecord( |
| 89 | new Set(["child"]), |
| 90 | packEnvValue(Redacted.make("https://first.example/v1/traces")), |
| 91 | ), |
| 92 | ); |
| 93 | values.set( |
| 94 | "OTEL_EXPORTER_OTLP_TRACES_HEADERS", |
| 95 | ConfigProvider.makeValue(packEnvValue(Redacted.make("authorization=first%20token"))), |
| 96 | ); |
| 97 | values.set("OTEL_EXPORTER_OTLP_HEADERS", ConfigProvider.makeValue("authorization=fallback")); |
| 98 | yield* runEvent(provider, client); |
| 99 | expect(requests).toHaveLength(1); |
| 100 | expect(requests[0].url).toBe("https://first.example/v1/traces"); |
| 101 | expect(requests[0].headers.authorization).toBe("first token"); |
| 102 | |
| 103 | values.set( |
| 104 | "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT", |
| 105 | ConfigProvider.makeArray(0, "https://second.example/v1/traces"), |
| 106 | ); |
| 107 | values.delete("OTEL_EXPORTER_OTLP_TRACES_HEADERS"); |
| 108 | yield* runEvent(provider, client); |
| 109 | expect(requests).toHaveLength(2); |
| 110 | expect(requests[1].url).toBe("https://second.example/v1/traces"); |
| 111 | expect(requests[1].headers.authorization).toBe("fallback"); |
| 112 | }), |
| 113 | ); |
| 114 | |
| 115 | it.effect("exports metrics as protobuf and traces as JSON", () => |
| 116 | Effect.gen(function* () { |
| 117 | const values = new Map<string, ConfigProvider.Node>([ |
| 118 | ["OTEL_EXPORTER_OTLP_ENDPOINT", ConfigProvider.makeValue("https://otlp.example")], |
| 119 | ]); |
| 120 | const provider = ConfigProvider.make((path) => Effect.sync(() => values.get(path.join("_")))); |
| 121 | const requests: HttpClientRequest.HttpClientRequest[] = []; |
| 122 | yield* runEvent(provider, recordingClient(requests)); |
| 123 | |
| 124 | const contentType = (path: string) => { |
| 125 | const body = requests.find( |
| 126 | (request) => request.url === `https://otlp.example${path}`, |
| 127 | )?.body; |
| 128 | return body?._tag === "Uint8Array" ? body.contentType : undefined; |
| 129 | }; |
| 130 | // OTLP/HTTP receivers must accept protobuf; Axiom rejects JSON metrics (415). |
| 131 | expect(contentType("/v1/metrics")).toBe("application/x-protobuf"); |
| 132 | expect(contentType("/v1/traces")).toBe("application/json"); |
| 133 | }), |
| 134 | ); |
| 135 | |
| 136 | it.effect("treats source failures as absent configuration without swallowing other defects", () => |
| 137 | Effect.gen(function* () { |
| 138 | const sourceError = new ConfigProvider.SourceError({ |
| 139 | message: "unavailable", |
| 140 | }); |
| 141 | for (const [failure, exports] of [ |
| 142 | [Effect.fail(sourceError), 1], |
| 143 | [Effect.die(sourceError), 1], |
| 144 | [Effect.die("unexpected"), 0], |
| 145 | ] as const) { |
| 146 | const requests: HttpClientRequest.HttpClientRequest[] = []; |
| 147 | const provider = ConfigProvider.make((path) => |
| 148 | path[0] === "OTEL_EXPORTER_OTLP_ENDPOINT" |
| 149 | ? failure |
| 150 | : Effect.succeed( |
| 151 | path[0] === "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT" |
| 152 | ? ConfigProvider.makeValue("https://trace.example/v1/traces") |
| 153 | : undefined, |
| 154 | ), |
| 155 | ); |
| 156 | yield* runEvent(provider, recordingClient(requests)); |
| 157 | expect(requests).toHaveLength(exports); |
| 158 | } |
| 159 | }), |
| 160 | ); |
| 161 | }); |