@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 172 lines 5.7 KB
Raw
1 import * as Duration from "effect/Duration";
2 import * as Effect from "effect/Effect";
3 import * as Exit from "effect/Exit";
4 import * as Metric from "effect/Metric";
5
6 /**
7 * Counter for resource lifecycle operations. Tagged per call with
8 * `resource_type`, `op` (`precreate`/`create`/`update`/`delete`/`read`),
9 * and `status` (`success`/`error`).
10 */
11 export const resourceCounter = Metric.counter("alchemy.resource.operations", {
12 description: "Number of resource lifecycle operations dispatched by alchemy apply.",
13 incremental: true,
14 });
15
16 /**
17 * Histogram of how long each lifecycle operation takes.
18 */
19 export const resourceDuration = Metric.timer("alchemy.resource.duration", {
20 description: "Wall-clock duration of resource lifecycle operations dispatched by alchemy apply.",
21 });
22
23 /**
24 * Counter for CLI command invocations.
25 */
26 export const cliCounter = Metric.counter("alchemy.cli.invocations", {
27 description: "Number of alchemy CLI command invocations.",
28 incremental: true,
29 });
30
31 /**
32 * Counter for Cloudflare State Store bootstrap/deploy operations.
33 * Tagged per call with `op` (e.g. `deploy`) and `status`
34 * (`success`/`error`) so that the deploy success rate of the
35 * state store itself can be tracked separately from regular
36 * resource lifecycle ops.
37 */
38 export const stateStoreCounter = Metric.counter("alchemy.state_store.operations", {
39 description:
40 "Number of Cloudflare State Store deploy/bootstrap operations dispatched by alchemy.",
41 incremental: true,
42 });
43
44 export type StateStoreOp = "deploy";
45
46 /**
47 * Counter for State store layer construction. Tagged with `id` (the
48 * `StateService.id` slug) so we can answer "how many distinct projects
49 * are using each backend" from the corresponding `state_store.init`
50 * spans. Open-ended on purpose: third-party state stores get counted
51 * automatically by setting their `StateService.id`.
52 */
53 export const stateStoreInitCounter = Metric.counter("alchemy.state_store.inits", {
54 description: "Number of times a State store layer is constructed.",
55 incremental: true,
56 });
57
58 /**
59 * Wraps a resource lifecycle Effect to record a counter + timer entry,
60 * tagged with `resource_type`, `op`, and `status` (`success`/`error`).
61 *
62 * Usage:
63 * ```ts
64 * provider.reconcile(input).pipe(recordResourceOp(node.resource.Type, "create"))
65 * ```
66 */
67 export const recordResourceOp =
68 (resourceType: string, op: ResourceOp) =>
69 <A, E, R>(self: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> =>
70 Effect.suspend(() => {
71 const startNs = process.hrtime.bigint();
72 const baseAttrs = { resource_type: resourceType, op } as const;
73 return self.pipe(
74 Effect.onExit((exit) =>
75 recordOutcome(baseAttrs, Exit.isSuccess(exit) ? "success" : "error", elapsed(startNs)),
76 ),
77 );
78 });
79
80 /**
81 * Wraps a CLI command Effect to bump {@link cliCounter} with the
82 * outcome (`success`/`error`).
83 */
84 export const recordCli =
85 (command: string) =>
86 <A, E, R>(self: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> =>
87 self.pipe(
88 Effect.onExit((exit) =>
89 Metric.update(
90 Metric.withAttributes(cliCounter, {
91 command,
92 status: Exit.isSuccess(exit) ? "success" : "error",
93 }),
94 1,
95 ),
96 ),
97 );
98
99 /**
100 * Wraps a Cloudflare State Store deploy/bootstrap Effect to bump
101 * {@link stateStoreCounter} with the outcome (`success`/`error`),
102 * tagged with the given `op`.
103 */
104 export const recordStateStoreOp =
105 (op: StateStoreOp) =>
106 <A, E, R>(self: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> =>
107 self.pipe(
108 Effect.onExit((exit) =>
109 Metric.update(
110 Metric.withAttributes(stateStoreCounter, {
111 op,
112 status: Exit.isSuccess(exit) ? "success" : "error",
113 }),
114 1,
115 ),
116 ),
117 );
118
119 /**
120 * Wraps a State store construction Effect to:
121 *
122 * 1. Bump {@link stateStoreInitCounter} tagged with `id`.
123 * 2. Open a `state_store.init` span carrying
124 * `alchemy.state_store.id` so Axiom (which can't query metric
125 * datasets via APL) can group projects by backend.
126 *
127 * The `id` is read off the constructed `StateService.id` field, so any
128 * third-party state-store implementation gets tracked just by setting
129 * a stable slug there. Apply at every `Layer.effect(State, …)` site
130 * exactly once.
131 */
132 export const recordStateStoreInit = <A extends { readonly id: string }, E, R>(
133 self: Effect.Effect<A, E, R>,
134 ): Effect.Effect<A, E, R> =>
135 // The debug lines bracket the first phase of a deploy that can block on
136 // credential resolution (see `Auth/Lock.ts`). Without them a stalled
137 // deploy produces no output at all, at any log level, and there is
138 // nothing to tell "hung" apart from "slow".
139 Effect.logDebug("state store: initializing").pipe(
140 Effect.andThen(self),
141 Effect.tap((service) =>
142 Effect.all(
143 [
144 Effect.logDebug(`state store: ready (${service.id})`),
145 Metric.update(Metric.withAttributes(stateStoreInitCounter, { id: service.id }), 1),
146 Effect.annotateCurrentSpan("alchemy.state_store.id", service.id),
147 ],
148 { discard: true },
149 ),
150 ),
151 Effect.withSpan("state_store.init"),
152 );
153
154 export type ResourceOp = "observe" | "precreate" | "create" | "update" | "delete" | "read";
155
156 const elapsed = (startNs: bigint): Duration.Duration =>
157 Duration.nanos(process.hrtime.bigint() - startNs);
158
159 const recordOutcome = (
160 baseAttrs: { resource_type: string; op: ResourceOp },
161 status: "success" | "error",
162 duration: Duration.Duration,
163 ): Effect.Effect<void> => {
164 const attrs = { ...baseAttrs, status };
165 return Effect.all(
166 [
167 Metric.update(Metric.withAttributes(resourceCounter, attrs), 1),
168 Metric.update(Metric.withAttributes(resourceDuration, attrs), duration),
169 ],
170 { discard: true },
171 );
172 };