Open History to see the last commit
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 });
17}
18
19function browserSpan(
20 name: string,
21 status: { code: string; message?: string },
22 attributes: Record<string, unknown> = {},
23) {
24 return JSON.stringify({
25 type: "otlp-span",
26 name,
27 durationMs: 1,
28 endTimeUnixNano: "1000000",
29 status,
30 attributes,
31 });
32}
33
34it("reports count, rate, percentiles, and exits per span name", () => {
35 const refreshes = Array.from({ length: 10 }, (_, index) =>
36 span(
37 "refresh",
38 index + 1,
39 index * MINUTE_MS,
40 index === 0 ? "Interrupted" : index === 1 ? "Failure" : "Success",
41 ),
42 );
43 const summarizer = makeTraceSpanSummary();
44 [...refreshes, span("probe", 2_500, 10 * MINUTE_MS)].forEach(summarizer.addLine);
45 const summary = summarizer.finish();
46
47 assert.strictEqual(summary.spanCount, 11);
48 assert.strictEqual(summary.minutes, 10);
49 assert.deepStrictEqual(summary.spans, [
50 {
51 name: "refresh",
52 count: 10,
53 perMinute: 1,
54 p50Ms: 5,
55 p90Ms: 9,
56 maxMs: 10,
57 interrupted: 1,
58 failures: 1,
59 },
60 {
61 name: "probe",
62 count: 1,
63 perMinute: 0.1,
64 p50Ms: 2_500,
65 p90Ms: 2_500,
66 maxMs: 2_500,
67 interrupted: 0,
68 failures: 0,
69 },
70 ]);
71});
72
73it("drops spans that ended before the window and counts unreadable lines", () => {
74 const summarizer = makeTraceSpanSummary(MINUTE_MS);
75 [
76 span("old", 1, 0),
77 "",
78 "{not json",
79 JSON.stringify({ name: "no-duration" }),
80 JSON.stringify({ name: "far-future", durationMs: 1, endTimeUnixNano: "9".repeat(22) }),
81 span("recent", 4, 5 * MINUTE_MS),
82 ].forEach(summarizer.addLine);
83 const summary = summarizer.finish();
84
85 assert.strictEqual(summary.skippedLineCount, 3);
86 assert.deepStrictEqual(
87 summary.spans.map((entry) => [entry.name, entry.count, entry.perMinute]),
88 [["recent", 1, undefined]],
89 );
90});
91
92it("reads failures and interrupts of browser spans from their OTLP status", () => {
93 const summarizer = makeTraceSpanSummary();
94 [
95 browserSpan("render", { code: "2", message: "boom" }),
96 browserSpan("render", { code: "0" }, { "effect.fiber.interrupted": true }),
97 browserSpan("render", { code: "1", message: "Interrupted" }),
98 browserSpan("render", { code: "1" }),
99 ].forEach(summarizer.addLine);
100
101 const [render] = summarizer.finish().spans;
102 assert.deepStrictEqual([render?.count, render?.interrupted, render?.failures], [4, 2, 1]);
103});