The best way to control Claude Code, Codex and more from any computer or phone

main
ts 93 lines 3.08 KB
Raw
1 import * as Axiom from "@alchemy.run/axiom";
2 import * as Cloudflare from "@alchemy.run/cloudflare";
3 import * as GitHub from "@alchemy.run/github";
4 import * as Alchemy from "alchemy";
5 import * as Effect from "effect/Effect";
6 import * as Layer from "effect/Layer";
7 import { CliOverviewDashboard } from "./otel/Dashboard.ts";
8 import { Logs, Metrics, Traces } from "./otel/Datasets.ts";
9 import Ingester from "./otel/Ingester.ts";
10 import {
11 ActiveUsersByCi,
12 ActiveUsersByVersion,
13 ActiveUsersHourly,
14 CliInvocations,
15 DeployDestroyLatency,
16 ResourceErrorRate,
17 ResourceLatency,
18 ResourcesUsed,
19 } from "./otel/Views.ts";
20
21 /**
22 * Provisions an Axiom OTEL ingestion pipeline:
23 *
24 * - Three datasets (`{stage}-traces`, `{stage}-logs`, `{stage}-metrics`),
25 * each with the matching `otel:*:v1` `kind`.
26 * - One ingest-only API token scoped to those three datasets.
27 * - Outputs the OTLP endpoints + `Authorization` header value so callers can
28 * wire them straight into a Worker / Lambda's env vars.
29 * - Optionally syncs the same values to the GitHub repo's Actions secrets
30 * (set `SYNC_GITHUB_SECRETS=1` when deploying).
31 */
32 export default Alchemy.Stack(
33 "AlchemyOtel",
34 {
35 providers: Layer.mergeAll(Axiom.providers(), Cloudflare.providers(), GitHub.providers()),
36 state: Cloudflare.state(),
37 },
38 Effect.gen(function* () {
39 const traces = yield* Traces;
40 const logs = yield* Logs;
41 const metrics = yield* Metrics;
42
43 // Public ingest relay. Bound to `otel.alchemy.run` and
44 // `analytics.alchemy.run` only in prod so dev stages exercise the same
45 // code path under a `*.workers.dev` URL. The same Worker also reverse-
46 // proxies PostHog Cloud for first-party browser analytics.
47 const relay = yield* Ingester;
48
49 // const posthogProjectKey = yield* Config.String("POSTHOG_PROJECT_KEY")
50 // .pipe(Config.option)
51 //
52 // .pipe(Effect.orDie);
53
54 // Saved APL queries — one per insight surfaced by the dashboard.
55 yield* ActiveUsersHourly;
56 yield* ActiveUsersByVersion;
57 yield* ActiveUsersByCi;
58 yield* ResourcesUsed;
59 yield* DeployDestroyLatency;
60 yield* ResourceLatency;
61 yield* CliInvocations;
62 yield* ResourceErrorRate;
63
64 // Composed dashboard combining the same signals on a 12-col grid.
65 yield* CliOverviewDashboard;
66
67 const env = {
68 OTEL_EXPORTER_OTLP_ENDPOINT: traces.otelEndpoint,
69 OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: traces.otelTracesEndpoint,
70 OTEL_EXPORTER_OTLP_LOGS_ENDPOINT: logs.otelLogsEndpoint,
71 OTEL_EXPORTER_OTLP_METRICS_ENDPOINT: metrics.otelMetricsEndpoint,
72 AXIOM_DATASET_TRACES: traces.name,
73 AXIOM_DATASET_LOGS: logs.name,
74 AXIOM_DATASET_METRICS: metrics.name,
75 RELAY_URL: relay.url,
76 };
77
78 if (process.env.SYNC_GITHUB_SECRETS === "1") {
79 yield* GitHub.Secrets({
80 owner: "alchemy-run",
81 repository: "alchemy",
82 secrets: {
83 AXIOM_DATASET_TRACES: traces.name,
84 AXIOM_DATASET_LOGS: logs.name,
85 AXIOM_DATASET_METRICS: metrics.name,
86 OTEL_EXPORTER_OTLP_ENDPOINT: traces.otelEndpoint,
87 },
88 });
89 }
90
91 return env;
92 }),
93 );