@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 62 lines 2.26 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as FetchHttpClient from "effect/http/FetchHttpClient";
3 import * as Layer from "effect/Layer";
4 import * as OtlpMetrics from "effect/observability/OtlpMetrics";
5 import * as OtlpSerialization from "effect/observability/OtlpSerialization";
6 import * as OtlpTracer from "effect/observability/OtlpTracer";
7 import packageJson from "../../package.json" with { type: "json" };
8 import { collectAttributes, isTelemetryDisabled } from "./Attributes.ts";
9
10 const TRACES_URL = "https://otel.alchemy.run/v1/traces";
11 const METRICS_URL = "https://otel.alchemy.run/v1/metrics";
12
13 const SERVICE_NAME = "alchemy-cli";
14
15 const buildOtlpLayer = (attrs: Record<string, unknown>): Layer.Layer<never, never, never> => {
16 const resource = {
17 serviceName: SERVICE_NAME,
18 serviceVersion: packageJson.version,
19 attributes: attrs,
20 };
21
22 // Short export intervals so even sub-second CLI invocations flush at
23 // least one batch before the process exits.
24 const tracer = OtlpTracer.layer({
25 url: TRACES_URL,
26 resource,
27 exportInterval: "1 second",
28 });
29 const metrics = OtlpMetrics.layer({
30 url: METRICS_URL,
31 resource,
32 exportInterval: "1 second",
33 });
34 return Layer.mergeAll(tracer, metrics).pipe(
35 Layer.provide(OtlpSerialization.layerJson),
36 Layer.provide(FetchHttpClient.layer),
37 );
38 };
39
40 /**
41 * The CLI's telemetry layer. Builds an OTLP HTTP exporter that ships spans
42 * to {@link TRACES_URL} and metrics to {@link METRICS_URL}, attaching
43 * {@link collectAttributes} as resource-level attributes so every signal
44 * carries user/project/runtime context.
45 *
46 * Logs are never exported: they stay in the run log on disk, which users
47 * can attach to a support request.
48 *
49 * If the user has opted out (via `DO_NOT_TRACK`, `NO_TRACK`,
50 * `ALCHEMY_TELEMETRY_DISABLED`, or `~/.alchemy/telemetry-disabled`), this
51 * resolves to {@link Layer.empty}. Effect's default `Tracer` is a no-op,
52 * so all `withSpan`/`Effect.fn` instrumentation in core stays free.
53 */
54 export const TelemetryLive: Layer.Layer<never, never, never> = Layer.unwrap(
55 Effect.gen(function* () {
56 if (yield* isTelemetryDisabled) {
57 return Layer.empty;
58 }
59 const attrs = yield* collectAttributes;
60 return buildOtlpLayer(attrs as unknown as Record<string, unknown>);
61 }),
62 );