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

RpcInstrumentation.test.ts

313 lines9.5 KB

1import { assert, describe, it } from "@effect/vitest";
2import { WS_METHODS } from "@t3tools/contracts";
3import * as Duration from "effect/Duration";
4import * as Effect from "effect/Effect";
5import * as Exit from "effect/Exit";
6import * as Fiber from "effect/Fiber";
7import * as Metric from "effect/Metric";
8import * as Stream from "effect/Stream";
9import * as Tracer from "effect/Tracer";
10import * as TestClock from "effect/testing/TestClock";
11​
12import {
13 observeRpcEffect,
14 observeRpcStream,
15 observeRpcStreamEffect,
16} from "./RpcInstrumentation.ts";
17​
18const hasMetricSnapshot = (
19 snapshots: ReadonlyArray<Metric.Metric.Snapshot>,
20 id: string,
21 attributes: Readonly<Record<string, string>>,
22) =>
23 snapshots.some(
24 (snapshot) =>
25 snapshot.id === id &&
26 Object.entries(attributes).every(([key, value]) => snapshot.attributes?.[key] === value),
27 );
28​
29const findHistogramSnapshot = (
30 snapshots: ReadonlyArray<Metric.Metric.Snapshot>,
31 id: string,
32 attributes: Readonly<Record<string, string>>,
33) =>
34 snapshots.find(
35 (snapshot): snapshot is Extract<Metric.Metric.Snapshot, { readonly type: "Histogram" }> =>
36 snapshot.type === "Histogram" &&
37 snapshot.id === id &&
38 Object.entries(attributes).every(([key, value]) => snapshot.attributes?.[key] === value),
39 );
40​
41const collectSpanNames = <A, E, R>(
42 effect: Effect.Effect<A, E, R>,
43): Effect.Effect<ReadonlyArray<string>, E, R> =>
44 Effect.gen(function* () {
45 const spanNames: Array<string> = [];
46 const tracer = Tracer.make({
47 span: (options) => {
48 const span = new Tracer.NativeSpan(options);
49 const end = span.end.bind(span);
50​
51 span.end = (endTime, exit) => {
52 end(endTime, exit);
53 if (span.sampled) {
54 spanNames.push(span.name);
55 }
56 };
57​
58 return span;
59 },
60 });
61​
62 yield* effect.pipe(Effect.withTracer(tracer));
63​
64 return spanNames;
65 });
66​
67describe("RpcInstrumentation", () => {
68 it.effect("records success metrics for unary RPC handlers", () =>
69 Effect.gen(function* () {
70 yield* observeRpcEffect("rpc.instrumentation.success", Effect.succeed("ok"), {
71 "rpc.aggregate": "test",
72 }).pipe(Effect.withSpan("rpc.instrumentation.success.span"));
73​
74 const snapshots = yield* Metric.snapshot;
75​
76 assert.equal(
77 hasMetricSnapshot(snapshots, "t3_rpc_requests_total", {
78 method: "rpc.instrumentation.success",
79 outcome: "success",
80 }),
81 true,
82 );
83 assert.equal(
84 hasMetricSnapshot(snapshots, "t3_rpc_request_duration", {
85 method: "rpc.instrumentation.success",
86 }),
87 true,
88 );
89 }),
90 );
91​
92 it.effect("records failure outcomes for unary RPC handlers", () =>
93 Effect.gen(function* () {
94 yield* Effect.exit(
95 observeRpcEffect("rpc.instrumentation.failure", Effect.fail("boom"), {
96 "rpc.aggregate": "test",
97 }).pipe(Effect.withSpan("rpc.instrumentation.failure.span")),
98 );
99​
100 const snapshots = yield* Metric.snapshot;
101​
102 assert.equal(
103 hasMetricSnapshot(snapshots, "t3_rpc_requests_total", {
104 method: "rpc.instrumentation.failure",
105 outcome: "failure",
106 }),
107 true,
108 );
109 assert.equal(
110 hasMetricSnapshot(snapshots, "t3_rpc_request_duration", {
111 method: "rpc.instrumentation.failure",
112 }),
113 true,
114 );
115 }),
116 );
117​
118 it.effect("records subscription activation metrics for stream RPC handlers", () =>
119 Effect.gen(function* () {
120 const events = yield* Stream.runCollect(
121 observeRpcStreamEffect(
122 "rpc.instrumentation.stream",
123 Effect.succeed(Stream.make("a", "b")),
124 { "rpc.aggregate": "test" },
125 ).pipe(Stream.withSpan("rpc.instrumentation.stream.span")),
126 );
127​
128 assert.deepStrictEqual(Array.from(events), ["a", "b"]);
129​
130 const snapshots = yield* Metric.snapshot;
131​
132 assert.equal(
133 hasMetricSnapshot(snapshots, "t3_rpc_requests_total", {
134 method: "rpc.instrumentation.stream",
135 outcome: "success",
136 }),
137 true,
138 );
139 assert.equal(
140 hasMetricSnapshot(snapshots, "t3_rpc_request_duration", {
141 method: "rpc.instrumentation.stream",
142 }),
143 true,
144 );
145 }),
146 );
147​
148 it.effect("records failure outcomes for direct stream RPC handlers during consumption", () =>
149 Effect.gen(function* () {
150 const exit = yield* Stream.runCollect(
151 observeRpcStream(
152 "rpc.instrumentation.stream.failure",
153 Stream.make("a").pipe(Stream.concat(Stream.fail("boom"))),
154 { "rpc.aggregate": "test" },
155 ).pipe(Stream.withSpan("rpc.instrumentation.stream.failure.span")),
156 ).pipe(Effect.exit);
157​
158 assert.equal(Exit.isFailure(exit), true);
159​
160 const snapshots = yield* Metric.snapshot;
161​
162 assert.equal(
163 hasMetricSnapshot(snapshots, "t3_rpc_requests_total", {
164 method: "rpc.instrumentation.stream.failure",
165 outcome: "failure",
166 }),
167 true,
168 );
169 assert.equal(
170 hasMetricSnapshot(snapshots, "t3_rpc_request_duration", {
171 method: "rpc.instrumentation.stream.failure",
172 }),
173 true,
174 );
175 }),
176 );
177​
178 it.effect("records direct stream durations from nanosecond clock readings", () =>
179 Effect.gen(function* () {
180 const duration = Duration.nanos(1_500_000n);
181 const events = yield* Effect.gen(function* () {
182 const fiber = yield* Stream.runCollect(
183 observeRpcStream(
184 WS_METHODS.serverGetProcessDiagnostics,
185 Stream.fromEffect(Effect.sleep(duration).pipe(Effect.as("ok"))),
186 {
187 "rpc.aggregate": "test",
188 },
189 ),
190 ).pipe(Effect.forkChild);
191​
192 yield* Effect.yieldNow;
193 yield* TestClock.adjust(duration);
194 return yield* Fiber.join(fiber);
195 }).pipe(Effect.provide(TestClock.layer()));
196​
197 assert.deepStrictEqual(Array.from(events), ["ok"]);
198​
199 const snapshots = yield* Metric.snapshot;
200 const snapshot = findHistogramSnapshot(snapshots, "t3_rpc_request_duration", {
201 method: WS_METHODS.serverGetProcessDiagnostics,
202 });
203​
204 assert.equal(snapshot?.state.count, 1);
205 assert.equal(snapshot?.state.sum, 1.5);
206 }),
207 );
208​
209 it.effect("records failure outcomes when a stream RPC effect produces a failing stream", () =>
210 Effect.gen(function* () {
211 const exit = yield* Stream.runCollect(
212 observeRpcStreamEffect(
213 "rpc.instrumentation.stream.effect.failure",
214 Effect.succeed(Stream.fail("boom")),
215 { "rpc.aggregate": "test" },
216 ).pipe(Stream.withSpan("rpc.instrumentation.stream.effect.failure.span")),
217 ).pipe(Effect.exit);
218​
219 assert.equal(Exit.isFailure(exit), true);
220​
221 const snapshots = yield* Metric.snapshot;
222​
223 assert.equal(
224 hasMetricSnapshot(snapshots, "t3_rpc_requests_total", {
225 method: "rpc.instrumentation.stream.effect.failure",
226 outcome: "failure",
227 }),
228 true,
229 );
230 assert.equal(
231 hasMetricSnapshot(snapshots, "t3_rpc_request_duration", {
232 method: "rpc.instrumentation.stream.effect.failure",
233 }),
234 true,
235 );
236 }),
237 );
238​
239 it.effect("records spans for traced stream RPC handlers", () =>
240 Effect.gen(function* () {
241 const spanNames = yield* collectSpanNames(
242 Stream.runCollect(
243 observeRpcStream(
244 "rpc.instrumentation.traced.stream",
245 Stream.fromEffect(
246 Effect.succeed("ok").pipe(Effect.withSpan("rpc.instrumentation.traced.stream.child")),
247 ),
248 { "rpc.aggregate": "test" },
249 ),
250 ),
251 );
252​
253 assert.equal(spanNames.includes("ws.rpc.rpc.instrumentation.traced.stream"), true);
254 assert.equal(spanNames.includes("rpc.instrumentation.traced.stream.child"), true);
255 }),
256 );
257​
258 it.effect("does not create spans for disabled unary RPC handlers", () =>
259 Effect.gen(function* () {
260 const spanNames = yield* collectSpanNames(
261 observeRpcEffect(
262 WS_METHODS.serverGetTraceDiagnostics,
263 Effect.succeed("ok").pipe(Effect.withSpan("rpc.instrumentation.disabled.unary.child")),
264 { "rpc.aggregate": "test" },
265 ),
266 );
267​
268 assert.deepStrictEqual(spanNames, []);
269 }),
270 );
271​
272 it.effect("does not create spans for disabled direct stream RPC handlers", () =>
273 Effect.gen(function* () {
274 const spanNames = yield* collectSpanNames(
275 Stream.runCollect(
276 observeRpcStream(
277 WS_METHODS.serverGetTraceDiagnostics,
278 Stream.fromEffect(
279 Effect.succeed("ok").pipe(
280 Effect.withSpan("rpc.instrumentation.disabled.stream.child"),
281 ),
282 ),
283 { "rpc.aggregate": "test" },
284 ),
285 ),
286 );
287​
288 assert.deepStrictEqual(spanNames, []);
289 }),
290 );
291​
292 it.effect("does not create spans for disabled stream effect RPC handlers", () =>
293 Effect.gen(function* () {
294 const spanNames = yield* collectSpanNames(
295 Stream.runCollect(
296 observeRpcStreamEffect(
297 WS_METHODS.serverGetTraceDiagnostics,
298 Effect.succeed(
299 Stream.fromEffect(
300 Effect.succeed("ok").pipe(
301 Effect.withSpan("rpc.instrumentation.disabled.stream.effect.consume"),
302 ),
303 ),
304 ).pipe(Effect.withSpan("rpc.instrumentation.disabled.stream.effect.create")),
305 { "rpc.aggregate": "test" },
306 ),
307 ),
308 );
309​
310 assert.deepStrictEqual(spanNames, []);
311 }),
312 );
313});