Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

Metrics.ts

207 lines7.5 KB

1import * as Clock from "effect/Clock";
2import * as Duration from "effect/Duration";
3import * as Effect from "effect/Effect";
4import * as Exit from "effect/Exit";
5import * as Metric from "effect/Metric";
6import { dual } from "effect/Function";
7​
8import { compactMetricAttributes, outcomeFromExit } from "./Attributes.ts";
9​
10export const rpcRequestsTotal = Metric.counter("t3_rpc_requests_total", {
11 description: "Total RPC requests handled by the websocket RPC server.",
12});
13​
14export const rpcRequestDuration = Metric.timer("t3_rpc_request_duration", {
15 description: "RPC request handling duration.",
16});
17​
18export const orchestrationCommandsTotal = Metric.counter("t3_orchestration_commands_total", {
19 description: "Total orchestration commands dispatched, by outcome.",
20});
21​
22export const orchestrationCommandDuration = Metric.timer("t3_orchestration_command_duration", {
23 description: "Orchestration command handling duration, excluding thread dispatch lock wait.",
24});
25​
26export const orchestrationEffectClaimsTotal = Metric.counter(
27 "t3_orchestration_effect_claims_total",
28 {
29 description: "Total completed orchestration effect outbox claim attempts by result.",
30 },
31);
32​
33export const orchestrationEffectQueueWait = Metric.timer("t3_orchestration_effect_queue_wait", {
34 description:
35 "Time from an orchestration effect's temporal availability until claim, including same-thread blocking.",
36});
37​
38export const providerSessionsTotal = Metric.counter("t3_provider_sessions_total", {
39 description: "Total provider session lifecycle operations.",
40});
41​
42export const providerTurnsTotal = Metric.counter("t3_provider_turns_total", {
43 description: "Total provider turn lifecycle operations.",
44});
45​
46export const providerTurnDuration = Metric.timer("t3_provider_turn_duration", {
47 description: "Time for the provider adapter to start a turn, not how long the turn runs.",
48});
49​
50// OTLP carries the unit, and Prometheus names the series by it: a unitless gauge gains `_ratio`.
51export const serverResidentBytes = Metric.gauge("t3_server_resident_bytes", {
52 description: "Resident memory of the server process.",
53 attributes: { unit: "By" },
54});
55​
56export const serverHeapUsedBytes = Metric.gauge("t3_server_heap_used_bytes", {
57 description: "JavaScript heap in use by the server process.",
58 attributes: { unit: "By" },
59});
60​
61export const serverCpuUtilization = Metric.gauge("t3_server_cpu_utilization", {
62 description: "CPU time the server used per second over the last sample; 1 is one full core.",
63});
64​
65export const serverEventLoopUtilization = Metric.gauge("t3_server_event_loop_utilization", {
66 description: "Share of the last sample the server's event loop spent busy.",
67});
68​
69export const serverEventLoopDelayMax = Metric.gauge("t3_server_event_loop_delay_max", {
70 description: "Longest event loop delay over the last sample.",
71 attributes: { unit: "ms" },
72});
73​
74export const runsTotal = Metric.counter("t3_runs_total", {
75 description: "Runs that finished, by outcome. A later rollback does not count again.",
76});
77​
78export const runDuration = Metric.timer("t3_run_duration", {
79 description: "Wall time of a run, from when it started to its terminal status.",
80});
81​
82export const runQueueWait = Metric.timer("t3_run_queue_wait", {
83 description: "Time a run waited between being requested and starting.",
84});
85​
86export const gitCommandsTotal = Metric.counter("t3_git_commands_total", {
87 description: "Total git commands executed by the server runtime.",
88});
89​
90export const gitCommandDuration = Metric.timer("t3_git_command_duration", {
91 description: "Git command execution duration.",
92});
93​
94export const terminalSessionsTotal = Metric.counter("t3_terminal_sessions_total", {
95 description: "Total terminal sessions started.",
96});
97​
98export const terminalRestartsTotal = Metric.counter("t3_terminal_restarts_total", {
99 description: "Total terminal restart requests handled.",
100});
101​
102export const webhookDeliveriesTotal = Metric.counter("t3_webhook_deliveries_total", {
103 description: "Webhook requests handled, by outcome and source.",
104});
105​
106export const webhookDeliveryDuration = Metric.timer("t3_webhook_delivery_duration", {
107 description: "Time to verify, log, and enqueue one webhook request.",
108});
109​
110export const webhookHeldDelay = Metric.timer("t3_webhook_held_delay", {
111 description:
112 "Time between the relay receiving a webhook request and the environment handling it.",
113});
114​
115export const webhookRunsTotal = Metric.counter("t3_webhook_runs_total", {
116 description: "Runs started from webhook deliveries, by outcome.",
117});
118​
119export const secretRequestsTotal = Metric.counter("t3_secret_requests_total", {
120 description: "Secrets agents asked users for, by how each request ended.",
121});
122​
123export const secretRefsConsumedTotal = Metric.counter("t3_secret_refs_consumed_total", {
124 description: "Secret refs tools tried to use, by result.",
125});
126​
127export const metricAttributes = (
128 attributes: Readonly<Record<string, unknown>>,
129): ReadonlyArray<[string, string]> => Object.entries(compactMetricAttributes(attributes));
130​
131export const increment = (
132 metric: Metric.Metric<number, unknown>,
133 attributes: Readonly<Record<string, unknown>>,
134 amount = 1,
135) => Metric.update(Metric.withAttributes(metric, metricAttributes(attributes)), amount);
136​
137export interface WithMetricsOptions {
138 readonly counter?: Metric.Metric<number, unknown>;
139 readonly timer?: Metric.Metric<Duration.Duration, unknown>;
140 readonly attributes?:
141 | Readonly<Record<string, unknown>>
142 | (() => Readonly<Record<string, unknown>>);
143 readonly outcomeAttributes?: (
144 outcome: ReturnType<typeof outcomeFromExit>,
145 ) => Readonly<Record<string, unknown>>;
147​
148const recordMetrics =
149 /* Instrumentation must not create its own span for every recorded operation. */
150 Effect.fnUntraced(function* (
151 options: WithMetricsOptions,
152 startedAt: bigint,
153 exit: Exit.Exit<unknown, unknown>,
154 ) {
155 const duration = Duration.nanos((yield* Clock.monotonicTimeNanos) - startedAt);
156 const baseAttributes =
157 typeof options.attributes === "function" ? options.attributes() : (options.attributes ?? {});
158​
159 if (options.timer) {
160 yield* Metric.update(
161 Metric.withAttributes(options.timer, metricAttributes(baseAttributes)),
162 duration,
163 );
164 }
165​
166 if (options.counter) {
167 const outcome = outcomeFromExit(exit);
168 yield* Metric.update(
169 Metric.withAttributes(
170 options.counter,
171 metricAttributes({
172 ...baseAttributes,
173 outcome,
174 ...(options.outcomeAttributes ? options.outcomeAttributes(outcome) : {}),
175 }),
176 ),
177 1,
178 );
179 }
180 });
181​
182// Monotonic timing ignores clock corrections; exit finalizers count interruptions.
183const withMetricsImpl = <A, E, R>(
184 effect: Effect.Effect<A, E, R>,
185 options: WithMetricsOptions,
186): Effect.Effect<A, E, R> =>
187 Effect.flatMap(Clock.monotonicTimeNanos, (startedAt) =>
188 Effect.onExit(effect, (exit) => recordMetrics(options, startedAt, exit)),
189 );
190​
191export const withMetrics: {
192 (
193 options: WithMetricsOptions,
194 ): <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
195 <A, E, R>(effect: Effect.Effect<A, E, R>, options: WithMetricsOptions): Effect.Effect<A, E, R>;
196} = dual(2, withMetricsImpl);
197​
198// Shaped as an `Effect.fn` combinator: the command is the instrumented function's argument.
199export const withCommandMetrics = <A, E, R>(
200 effect: Effect.Effect<A, E, R>,
201 command: { readonly type: string },
202): Effect.Effect<A, E, R> =>
203 withMetrics(effect, {
204 counter: orchestrationCommandsTotal,
205 timer: orchestrationCommandDuration,
206 attributes: { commandType: command.type },
207 });