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
| 1 | import type { |
| 2 | OtlpEndpointCheckInput, |
| 3 | OtlpEndpointCheckResult, |
| 4 | OtlpSignal, |
| 5 | } from "@t3tools/contracts"; |
| 6 | import { layerOtlpSerialization } from "@t3tools/shared/observability"; |
| 7 | import * as Clock from "effect/Clock"; |
| 8 | import * as Context from "effect/Context"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as Layer from "effect/Layer"; |
| 11 | import * as Option from "effect/Option"; |
| 12 | import { FetchHttpClient, HttpClient } from "effect/http"; |
| 13 | import { OtlpSerialization } from "effect/observability"; |
| 14 | |
| 15 | import * as ServerConfig from "../config.ts"; |
| 16 | |
| 17 | const CHECK_TIMEOUT = "5 seconds"; |
| 18 | |
| 19 | export class OtlpEndpointCheck extends Context.Service< |
| 20 | OtlpEndpointCheck, |
| 21 | { |
| 22 | readonly check: (input: OtlpEndpointCheckInput) => Effect.Effect<OtlpEndpointCheckResult>; |
| 23 | } |
| 24 | >()("t3/observability/OtlpEndpointCheck") {} |
| 25 | |
| 26 | const emptyExport = ( |
| 27 | serialization: OtlpSerialization.OtlpSerialization["Service"], |
| 28 | signal: OtlpSignal, |
| 29 | ) => { |
| 30 | switch (signal) { |
| 31 | case "traces": |
| 32 | return serialization.traces({ resourceSpans: [] }); |
| 33 | case "metrics": |
| 34 | return serialization.metrics({ resourceMetrics: [] }); |
| 35 | case "logs": |
| 36 | return serialization.logs({ resourceLogs: [] }); |
| 37 | } |
| 38 | }; |
| 39 | |
| 40 | const make = Effect.gen(function* () { |
| 41 | const config = yield* ServerConfig.ServerConfig; |
| 42 | const httpClient = yield* HttpClient.HttpClient; |
| 43 | |
| 44 | const running = { |
| 45 | traces: { url: config.otlpTracesUrl, export: config.otlpTracesExport }, |
| 46 | metrics: { url: config.otlpMetricsUrl, export: config.otlpMetricsExport }, |
| 47 | logs: { url: config.otlpLogsUrl, export: config.otlpLogsExport }, |
| 48 | } satisfies Record<OtlpSignal, unknown>; |
| 49 | |
| 50 | const check = Effect.fn("OtlpEndpointCheck.check")(function* ({ |
| 51 | signal, |
| 52 | url, |
| 53 | }: OtlpEndpointCheckInput) { |
| 54 | const { url: runningUrl, export: signalExport } = running[signal]; |
| 55 | const headers = url === runningUrl ? signalExport.headers : undefined; |
| 56 | const body = yield* Effect.gen(function* () { |
| 57 | const serialization = yield* OtlpSerialization.OtlpSerialization; |
| 58 | return emptyExport(serialization, signal); |
| 59 | }).pipe(Effect.provide(layerOtlpSerialization(signalExport.protocol))); |
| 60 | const startedAt = yield* Clock.currentTimeMillis; |
| 61 | // Close after reading status so open bodies cannot hang; disable tracing to keep URLs and credentials out of spans. |
| 62 | const response = yield* HttpClient.withScope(httpClient) |
| 63 | .post(url, { body, headers }) |
| 64 | .pipe( |
| 65 | Effect.map(({ status }) => ({ status })), |
| 66 | Effect.scoped, |
| 67 | Effect.provideService(FetchHttpClient.RequestInit, { redirect: "manual" }), |
| 68 | Effect.withTracerEnabled(false), |
| 69 | Effect.timeoutOption(CHECK_TIMEOUT), |
| 70 | Effect.option, |
| 71 | ); |
| 72 | if (Option.isNone(response)) { |
| 73 | return { _tag: "Unreachable", timedOut: false } as const; |
| 74 | } |
| 75 | if (Option.isNone(response.value)) { |
| 76 | return { _tag: "Unreachable", timedOut: true } as const; |
| 77 | } |
| 78 | const { status } = response.value.value; |
| 79 | if (status < 200 || status >= 300) { |
| 80 | return { _tag: "Rejected", status } as const; |
| 81 | } |
| 82 | const latencyMs = (yield* Clock.currentTimeMillis) - startedAt; |
| 83 | return { _tag: "Accepted", latencyMs } as const; |
| 84 | }); |
| 85 | |
| 86 | return OtlpEndpointCheck.of({ check }); |
| 87 | }); |
| 88 | |
| 89 | export const layer = Layer.effect(OtlpEndpointCheck, make); |