Open History to see the last commit
1import { Duration, Effect, Exit, Metric } from "effect";
2import { dual } from "effect/Function";
3
4import {
5 compactMetricAttributes,
6 normalizeModelMetricLabel,
7 outcomeFromExit,
8} 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.",
20});
21
22export const orchestrationCommandDuration = Metric.timer("t3_orchestration_command_duration", {
23 description: "Orchestration command dispatch duration.",
24});
25
26export const orchestrationCommandAckDuration = Metric.timer(
27 "t3_orchestration_command_ack_duration",
28 {
29 description:
30 "Time from orchestration command dispatch to the first committed domain event emitted for that command.",
31 },
32);
33
34export const orchestrationEventsProcessedTotal = Metric.counter(
35 "t3_orchestration_events_processed_total",
36 {
37 description: "Total orchestration intent events processed by runtime reactors.",
38 },
39);
40
41export const providerSessionsTotal = Metric.counter("t3_provider_sessions_total", {
42 description: "Total provider session lifecycle operations.",
43});
44
45export const providerTurnsTotal = Metric.counter("t3_provider_turns_total", {
46 description: "Total provider turn lifecycle operations.",
47});
48
49export const providerTurnDuration = Metric.timer("t3_provider_turn_duration", {
50 description: "Provider turn request duration.",
51});
52
53export const 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>>;
92}
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 = Date.now();
100 const exit = yield* Effect.exit(effect);
101 const duration = Duration.millis(Math.max(0, Date.now() - startedAt));
102 const baseAttributes =
103 typeof options.attributes === "function" ? options.attributes() : (options.attributes ?? {});
104
105 if (options.timer) {
106 yield* Metric.update(
107 Metric.withAttributes(options.timer, metricAttributes(baseAttributes)),
108 duration,
109 );
110 }
111
112 if (options.counter) {
113 const outcome = outcomeFromExit(exit);
114 yield* Metric.update(
115 Metric.withAttributes(
116 options.counter,
117 metricAttributes({
118 ...baseAttributes,
119 outcome,
120 ...(options.outcomeAttributes ? options.outcomeAttributes(outcome) : {}),
121 }),
122 ),
123 1,
124 );
125 }
126
127 if (Exit.isSuccess(exit)) {
128 return exit.value;
129 }
130 return yield* Effect.failCause(exit.cause);
131 });
132
133export const withMetrics: {
134 <A, E, R>(
135 options: WithMetricsOptions,
136 ): (effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
137 <A, E, R>(effect: Effect.Effect<A, E, R>, options: WithMetricsOptions): Effect.Effect<A, E, R>;
138} = dual(2, withMetricsImpl);
139
140export const providerMetricAttributes = (
141 provider: string,
142 extra?: Readonly<Record<string, unknown>>,
143) =>
144 compactMetricAttributes({
145 provider,
146 ...extra,
147 });
148
149export const providerTurnMetricAttributes = (input: {
150 readonly provider: string;
151 readonly model: string | null | undefined;
152 readonly extra?: Readonly<Record<string, unknown>>;
153}) => {
154 const modelFamily = normalizeModelMetricLabel(input.model);
155 return compactMetricAttributes({
156 provider: input.provider,
157 ...(modelFamily ? { modelFamily } : {}),
158 ...input.extra,
159 });
160};