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, 06:00 UTC
| 1 | import type { Input } from "alchemy/Input"; |
| 2 | import * as Output from "alchemy/Output"; |
| 3 | import { layerOtlp, type OtlpSignalOptions } from "alchemy/Telemetry"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Layer from "effect/Layer"; |
| 6 | import * as Redacted from "effect/Redacted"; |
| 7 | import type { ApiToken } from "./ApiToken.ts"; |
| 8 | import type { Dataset } from "./Dataset.ts"; |
| 9 | import { otelDatasetHeader } from "./OtelHeaders.ts"; |
| 10 | |
| 11 | /** |
| 12 | * A resource passed to the layer: either the module-scope declaration (an |
| 13 | * Effect that resolves to the instance, same as what capability bindings |
| 14 | * accept) or an already-yielded instance. |
| 15 | */ |
| 16 | export type ResourceInput<T> = T | Effect.Effect<T, never, any>; |
| 17 | |
| 18 | /** |
| 19 | * Options for {@link Telemetry}: an ingest {@link ApiToken} plus one |
| 20 | * {@link Dataset} per OTel signal to export. |
| 21 | */ |
| 22 | export interface AxiomTelemetryProps { |
| 23 | /** |
| 24 | * The ingest credential. Must have `ingest: ["create"]` capability on |
| 25 | * every dataset passed below. |
| 26 | */ |
| 27 | token: ResourceInput<ApiToken>; |
| 28 | /** Dataset (kind `otel:traces:v1`) to export traces into. */ |
| 29 | traces?: ResourceInput<Dataset> | undefined; |
| 30 | /** Dataset (kind `otel:logs:v1`) to export logs into. */ |
| 31 | logs?: ResourceInput<Dataset> | undefined; |
| 32 | /** Dataset (kind `otel:metrics:v1`) to export metrics into. */ |
| 33 | metrics?: ResourceInput<Dataset> | undefined; |
| 34 | /** |
| 35 | * The exported `service.name`. |
| 36 | * @default the deployed Function/Worker's physical name |
| 37 | */ |
| 38 | serviceName?: Input<string> | undefined; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Resource declarations are Effects — yield them to get the instance with |
| 43 | * attribute Output accessors, same as `Binding.Service`'s callable does for |
| 44 | * capability bindings. |
| 45 | */ |
| 46 | const instance = <T>(resource: ResourceInput<T>): Effect.Effect<T> => |
| 47 | Effect.isEffect(resource) ? (resource as Effect.Effect<T>) : Effect.succeed(resource); |
| 48 | |
| 49 | const signal = ( |
| 50 | token: ApiToken, |
| 51 | dataset: Dataset | undefined, |
| 52 | urlAttr: "otelTracesEndpoint" | "otelLogsEndpoint" | "otelMetricsEndpoint", |
| 53 | ): OtlpSignalOptions | undefined => |
| 54 | dataset === undefined |
| 55 | ? undefined |
| 56 | : { |
| 57 | url: dataset[urlAttr], |
| 58 | headers: { |
| 59 | Authorization: Output.map( |
| 60 | token.token, |
| 61 | (bearer) => |
| 62 | Redacted.make(`Bearer ${Redacted.value(bearer)}`) as Redacted.Redacted<string>, |
| 63 | ), |
| 64 | [otelDatasetHeader(urlAttr === "otelMetricsEndpoint")]: dataset.name, |
| 65 | }, |
| 66 | }; |
| 67 | |
| 68 | /** |
| 69 | * Export a Function/Worker's telemetry to Axiom. |
| 70 | * |
| 71 | * A binding layer over {@link layerOtlp | Alchemy.Telemetry.layerOtlp}: |
| 72 | * building it binds each dataset's OTLP endpoint and the ingest token's |
| 73 | * `Authorization` header (as a secret) onto the host, and at runtime the |
| 74 | * built-in exporter ships each signal to its dataset, flushed per event. |
| 75 | * |
| 76 | * Compose it into the Function/Worker's single `Effect.provide`: |
| 77 | * |
| 78 | * ```ts |
| 79 | * import * as Axiom from "@alchemy.run/axiom"; |
| 80 | * import { Bucket } from "./bucket.ts"; |
| 81 | * import { Ingest, Logs, Traces } from "./observability.ts"; |
| 82 | * |
| 83 | * export default Cloudflare.Worker( |
| 84 | * "Worker", |
| 85 | * { main: import.meta.url }, |
| 86 | * Effect.gen(function* () { |
| 87 | * // ... |
| 88 | * }).pipe( |
| 89 | * Effect.provide( |
| 90 | * Layer.mergeAll( |
| 91 | * Cloudflare.R2.ReadWriteBucketBinding, |
| 92 | * Axiom.Telemetry({ token: Ingest, traces: Traces, logs: Logs }), |
| 93 | * ), |
| 94 | * ), |
| 95 | * ), |
| 96 | * ); |
| 97 | * ``` |
| 98 | */ |
| 99 | export const Telemetry = (props: AxiomTelemetryProps): Layer.Layer<never> => |
| 100 | Layer.unwrap( |
| 101 | Effect.gen(function* () { |
| 102 | // Declarations are yielded to instances (registering them on the |
| 103 | // Stack if the enclosing host is the first to reference them), so |
| 104 | // attribute accessors below produce real Outputs. |
| 105 | const token = yield* instance(props.token); |
| 106 | const traces = props.traces && (yield* instance(props.traces)); |
| 107 | const logs = props.logs && (yield* instance(props.logs)); |
| 108 | const metrics = props.metrics && (yield* instance(props.metrics)); |
| 109 | return layerOtlp({ |
| 110 | serviceName: props.serviceName, |
| 111 | traces: signal(token, traces, "otelTracesEndpoint"), |
| 112 | logs: signal(token, logs, "otelLogsEndpoint"), |
| 113 | metrics: signal(token, metrics, "otelMetricsEndpoint"), |
| 114 | }); |
| 115 | }), |
| 116 | ) as Layer.Layer<never>; |