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

Metrics.ts

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