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

Metrics.ts

175 lines5.7 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 {
9 compactMetricAttributes,
10 normalizeModelMetricLabel,
11 outcomeFromExit,
12} from "./Attributes.ts";
13​
14export const rpcRequestsTotal = Metric.counter("t3_rpc_requests_total", {
15 description: "Total RPC requests handled by the websocket RPC server.",
16});
17​
18export const rpcRequestDuration = Metric.timer("t3_rpc_request_duration", {
19 description: "RPC request handling duration.",
20});
21​
22export const orchestrationCommandsTotal = Metric.counter("t3_orchestration_commands_total", {
23 description: "Total orchestration commands dispatched.",
24});
25​
26export const orchestrationCommandDuration = Metric.timer("t3_orchestration_command_duration", {
27 description: "Orchestration command dispatch duration.",
28});
29​
30export const orchestrationCommandAckDuration = Metric.timer(
31 "t3_orchestration_command_ack_duration",
32 {
33 description:
34 "Time from orchestration command dispatch to the first committed domain event emitted for that command.",
35 },
36);
37​
38const orchestrationEventsProcessedTotal = Metric.counter(
39 "t3_orchestration_events_processed_total",
40 {
41 description: "Total orchestration intent events processed by runtime reactors.",
42 },
43);
44​
45export const orchestrationEffectClaimsTotal = Metric.counter(
46 "t3_orchestration_effect_claims_total",
47 {
48 description: "Total completed orchestration effect outbox claim attempts by result.",
49 },
50);
51​
52export const orchestrationEffectQueueWait = Metric.timer("t3_orchestration_effect_queue_wait", {
53 description:
54 "Time from an orchestration effect's temporal availability until claim, including same-thread blocking.",
55});
56​
57const providerSessionsTotal = Metric.counter("t3_provider_sessions_total", {
58 description: "Total provider session lifecycle operations.",
59});
60​
61const providerTurnsTotal = Metric.counter("t3_provider_turns_total", {
62 description: "Total provider turn lifecycle operations.",
63});
64​
65const providerTurnDuration = Metric.timer("t3_provider_turn_duration", {
66 description: "Provider turn request duration.",
67});
68​
69const providerRuntimeEventsTotal = Metric.counter("t3_provider_runtime_events_total", {
70 description: "Total canonical provider runtime events processed.",
71});
72​
73export const gitCommandsTotal = Metric.counter("t3_git_commands_total", {
74 description: "Total git commands executed by the server runtime.",
75});
76​
77export const gitCommandDuration = Metric.timer("t3_git_command_duration", {
78 description: "Git command execution duration.",
79});
80​
81export const terminalSessionsTotal = Metric.counter("t3_terminal_sessions_total", {
82 description: "Total terminal sessions started.",
83});
84​
85export const terminalRestartsTotal = Metric.counter("t3_terminal_restarts_total", {
86 description: "Total terminal restart requests handled.",
87});
88​
89export const metricAttributes = (
90 attributes: Readonly<Record<string, unknown>>,
91): ReadonlyArray<[string, string]> => Object.entries(compactMetricAttributes(attributes));
92​
93export const increment = (
94 metric: Metric.Metric<number, unknown>,
95 attributes: Readonly<Record<string, unknown>>,
96 amount = 1,
97) => Metric.update(Metric.withAttributes(metric, metricAttributes(attributes)), amount);
98​
99export interface WithMetricsOptions {
100 readonly counter?: Metric.Metric<number, unknown>;
101 readonly timer?: Metric.Metric<Duration.Duration, unknown>;
102 readonly attributes?:
103 | Readonly<Record<string, unknown>>
104 | (() => Readonly<Record<string, unknown>>);
105 readonly outcomeAttributes?: (
106 outcome: ReturnType<typeof outcomeFromExit>,
107 ) => Readonly<Record<string, unknown>>;
109​
110const withMetricsImpl = <A, E, R>(
111 effect: Effect.Effect<A, E, R>,
112 options: WithMetricsOptions,
113): Effect.Effect<A, E, R> =>
114 Effect.gen(function* () {
115 const startedAt = yield* Clock.currentTimeNanos;
116 const exit = yield* Effect.exit(effect);
117 const endedAt = yield* Clock.currentTimeNanos;
118 const elapsedNanos = endedAt > startedAt ? endedAt - startedAt : 0n;
119 const duration = Duration.nanos(elapsedNanos);
120 const baseAttributes =
121 typeof options.attributes === "function" ? options.attributes() : (options.attributes ?? {});
122​
123 if (options.timer) {
124 yield* Metric.update(
125 Metric.withAttributes(options.timer, metricAttributes(baseAttributes)),
126 duration,
127 );
128 }
129​
130 if (options.counter) {
131 const outcome = outcomeFromExit(exit);
132 yield* Metric.update(
133 Metric.withAttributes(
134 options.counter,
135 metricAttributes({
136 ...baseAttributes,
137 outcome,
138 ...(options.outcomeAttributes ? options.outcomeAttributes(outcome) : {}),
139 }),
140 ),
141 1,
142 );
143 }
144​
145 if (Exit.isSuccess(exit)) {
146 return exit.value;
147 }
148 return yield* Effect.failCause(exit.cause);
149 });
150​
151export const withMetrics: {
152 <A, E, R>(
153 options: WithMetricsOptions,
154 ): (effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
155 <A, E, R>(effect: Effect.Effect<A, E, R>, options: WithMetricsOptions): Effect.Effect<A, E, R>;
156} = dual(2, withMetricsImpl);
157​
158const providerMetricAttributes = (provider: string, extra?: Readonly<Record<string, unknown>>) =>
159 compactMetricAttributes({
160 provider,
161 ...extra,
162 });
163​
164const providerTurnMetricAttributes = (input: {
165 readonly provider: string;
166 readonly model: string | null | undefined;
167 readonly extra?: Readonly<Record<string, unknown>>;
168}) => {
169 const modelFamily = normalizeModelMetricLabel(input.model);
170 return compactMetricAttributes({
171 provider: input.provider,
172 ...(modelFamily ? { modelFamily } : {}),
173 ...input.extra,
174 });
175};