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

Metrics.ts

152 lines5.3 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 orchestrationEffectClaimsTotal = Metric.counter(
19 "t3_orchestration_effect_claims_total",
20 {
21 description: "Total completed orchestration effect outbox claim attempts by result.",
22 },
23);
24​
25export const orchestrationEffectQueueWait = Metric.timer("t3_orchestration_effect_queue_wait", {
26 description:
27 "Time from an orchestration effect's temporal availability until claim, including same-thread blocking.",
28});
29​
30export const providerSessionsTotal = Metric.counter("t3_provider_sessions_total", {
31 description: "Total provider session lifecycle operations.",
32});
33​
34export const providerTurnsTotal = Metric.counter("t3_provider_turns_total", {
35 description: "Total provider turn lifecycle operations.",
36});
37​
38export const providerTurnDuration = Metric.timer("t3_provider_turn_duration", {
39 description: "Time for the provider adapter to start a turn, not how long the turn runs.",
40});
41​
42export const gitCommandsTotal = Metric.counter("t3_git_commands_total", {
43 description: "Total git commands executed by the server runtime.",
44});
45​
46export const gitCommandDuration = Metric.timer("t3_git_command_duration", {
47 description: "Git command execution duration.",
48});
49​
50export const terminalSessionsTotal = Metric.counter("t3_terminal_sessions_total", {
51 description: "Total terminal sessions started.",
52});
53​
54export const terminalRestartsTotal = Metric.counter("t3_terminal_restarts_total", {
55 description: "Total terminal restart requests handled.",
56});
57​
58export const webhookDeliveriesTotal = Metric.counter("t3_webhook_deliveries_total", {
59 description: "Webhook requests handled, by outcome and source.",
60});
61​
62export const webhookDeliveryDuration = Metric.timer("t3_webhook_delivery_duration", {
63 description: "Time to verify, log, and enqueue one webhook request.",
64});
65​
66export const webhookHeldDelay = Metric.timer("t3_webhook_held_delay", {
67 description:
68 "Time between the relay receiving a webhook request and the environment handling it.",
69});
70​
71export const webhookRunsTotal = Metric.counter("t3_webhook_runs_total", {
72 description: "Runs started from webhook deliveries, by outcome.",
73});
74​
75export const secretRequestsTotal = Metric.counter("t3_secret_requests_total", {
76 description: "Secrets agents asked users for, by how each request ended.",
77});
78​
79export const secretRefsConsumedTotal = Metric.counter("t3_secret_refs_consumed_total", {
80 description: "Secret refs tools tried to use, by result.",
81});
82​
83export const metricAttributes = (
84 attributes: Readonly<Record<string, unknown>>,
85): ReadonlyArray<[string, string]> => Object.entries(compactMetricAttributes(attributes));
86​
87export const increment = (
88 metric: Metric.Metric<number, unknown>,
89 attributes: Readonly<Record<string, unknown>>,
90 amount = 1,
91) => Metric.update(Metric.withAttributes(metric, metricAttributes(attributes)), amount);
92​
93export interface WithMetricsOptions {
94 readonly counter?: Metric.Metric<number, unknown>;
95 readonly timer?: Metric.Metric<Duration.Duration, unknown>;
96 readonly attributes?:
97 | Readonly<Record<string, unknown>>
98 | (() => Readonly<Record<string, unknown>>);
99 readonly outcomeAttributes?: (
100 outcome: ReturnType<typeof outcomeFromExit>,
101 ) => Readonly<Record<string, unknown>>;
103​
104const recordMetrics = (
105 options: WithMetricsOptions,
106 startedAt: bigint,
107 exit: Exit.Exit<unknown, unknown>,
108) =>
109 Effect.gen(function* () {
110 const duration = Duration.nanos((yield* Clock.monotonicTimeNanos) - startedAt);
111 const baseAttributes =
112 typeof options.attributes === "function" ? options.attributes() : (options.attributes ?? {});
113​
114 if (options.timer) {
115 yield* Metric.update(
116 Metric.withAttributes(options.timer, metricAttributes(baseAttributes)),
117 duration,
118 );
119 }
120​
121 if (options.counter) {
122 const outcome = outcomeFromExit(exit);
123 yield* Metric.update(
124 Metric.withAttributes(
125 options.counter,
126 metricAttributes({
127 ...baseAttributes,
128 outcome,
129 ...(options.outcomeAttributes ? options.outcomeAttributes(outcome) : {}),
130 }),
131 ),
132 1,
133 );
134 }
135 });
136​
137// Durations come from the monotonic clock, so wall-clock corrections cannot skew them, and
138// metrics are recorded in an exit finalizer, so interrupted work is counted as "interrupt".
139const withMetricsImpl = <A, E, R>(
140 effect: Effect.Effect<A, E, R>,
141 options: WithMetricsOptions,
142): Effect.Effect<A, E, R> =>
143 Effect.flatMap(Clock.monotonicTimeNanos, (startedAt) =>
144 Effect.onExit(effect, (exit) => recordMetrics(options, startedAt, exit)),
145 );
146​
147export const withMetrics: {
148 (
149 options: WithMetricsOptions,
150 ): <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
151 <A, E, R>(effect: Effect.Effect<A, E, R>, options: WithMetricsOptions): Effect.Effect<A, E, R>;
152} = dual(2, withMetricsImpl);