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

trace.test.ts

100 lines2.8 KB

1import { assert, it } from "@effect/vitest";
2​
3import { makeTraceSpanSummary } from "./trace.ts";
4​
5const MINUTE_MS = 60_000;
6​
7function span(name: string, durationMs: number, endMs: number, exitTag = "Success") {
8 return JSON.stringify({
9 type: "effect-span",
10 name,
11 traceId: "trace",
12 spanId: "span",
13 durationMs,
14 endTimeUnixNano: String(BigInt(endMs) * 1_000_000n),
15 exit: { _tag: exitTag, cause: "cause" },
16 });
18​
19function browserSpan(name: string, status: { code: string; message?: string }) {
20 return JSON.stringify({
21 type: "otlp-span",
22 name,
23 durationMs: 1,
24 endTimeUnixNano: "1000000",
25 status,
26 });
28​
29it("reports count, rate, percentiles, and exits per span name", () => {
30 // Ten `refresh` spans of 1..10 ms end over minutes 0..9, and one `probe`
31 // span ends at minute 10, so the recorded window is 10 minutes.
32 const refreshes = Array.from({ length: 10 }, (_, index) =>
33 span(
34 "refresh",
35 index + 1,
36 index * MINUTE_MS,
37 index === 0 ? "Interrupted" : index === 1 ? "Failure" : "Success",
38 ),
39 );
40 const summarizer = makeTraceSpanSummary();
41 [...refreshes, span("probe", 2_500, 10 * MINUTE_MS)].forEach(summarizer.addLine);
42 const summary = summarizer.finish();
43​
44 assert.strictEqual(summary.spanCount, 11);
45 assert.strictEqual(summary.minutes, 10);
46 assert.deepStrictEqual(summary.spans, [
47 {
48 name: "refresh",
49 count: 10,
50 perMinute: 1,
51 p50Ms: 5,
52 p90Ms: 9,
53 maxMs: 10,
54 interrupted: 1,
55 failures: 1,
56 },
57 {
58 name: "probe",
59 count: 1,
60 perMinute: 0.1,
61 p50Ms: 2_500,
62 p90Ms: 2_500,
63 maxMs: 2_500,
64 interrupted: 0,
65 failures: 0,
66 },
67 ]);
68});
69​
70it("drops spans that ended before the window and counts unreadable lines", () => {
71 const summarizer = makeTraceSpanSummary(MINUTE_MS);
72 [
73 span("old", 1, 0),
74 "",
75 "{not json",
76 JSON.stringify({ name: "no-duration" }),
77 // Ends past the largest Date, so the report could not print it.
78 JSON.stringify({ name: "far-future", durationMs: 1, endTimeUnixNano: "9".repeat(22) }),
79 span("recent", 4, 5 * MINUTE_MS),
80 ].forEach(summarizer.addLine);
81 const summary = summarizer.finish();
82​
83 assert.strictEqual(summary.skippedLineCount, 3);
84 assert.deepStrictEqual(
85 summary.spans.map((entry) => [entry.name, entry.count, entry.perMinute]),
86 [["recent", 1, undefined]],
87 );
88});
89​
90it("reads failures and interrupts of browser spans from their OTLP status", () => {
91 const summarizer = makeTraceSpanSummary();
92 [
93 browserSpan("render", { code: "2", message: "boom" }),
94 browserSpan("render", { code: "1", message: "Interrupted" }),
95 browserSpan("render", { code: "1" }),
96 ].forEach(summarizer.addLine);
97​
98 const [render] = summarizer.finish().spans;
99 assert.deepStrictEqual([render?.count, render?.interrupted, render?.failures], [3, 1, 1]);
100});