Open History to see the last commit
1import { assert, it } from "@effect/vitest";
2
3import { formatTraceSummary, 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("drops spans that ended before the window and counts unreadable lines", () => {
35 const summarizer = makeTraceSpanSummary(MINUTE_MS);
36 [
37 span("old", 1, 0),
38 "",
39 "{not json",
40 JSON.stringify({ name: "no-duration" }),
41 JSON.stringify({ name: "far-future", durationMs: 1, endTimeUnixNano: "9".repeat(22) }),
42 span("recent", 4, 5 * MINUTE_MS),
43 ].forEach(summarizer.addLine);
44 const summary = summarizer.finish();
45
46 assert.strictEqual(summary.skippedLineCount, 3);
47 assert.deepStrictEqual(
48 summary.spans.map((entry) => [entry.name, entry.count, entry.perMinute]),
49 [["recent", 1, undefined]],
50 );
51});
52
53it("reads failures and interrupts of browser spans from their OTLP status", () => {
54 const summarizer = makeTraceSpanSummary();
55 [
56 browserSpan("render", { code: "2", message: "boom" }),
57 browserSpan("render", { code: "0" }, { "effect.fiber.interrupted": true }),
58 browserSpan("render", { code: "1", message: "Interrupted" }),
59 browserSpan("render", { code: "1" }),
60 ].forEach(summarizer.addLine);
61
62 const [render] = summarizer.finish().spans;
63 assert.deepStrictEqual([render?.count, render?.interrupted, render?.failures], [4, 2, 1]);
64});
65
66it("prints durations that round up to a second in seconds", () => {
67 const summarizer = makeTraceSpanSummary();
68 [span("rpc", 999.4, MINUTE_MS), span("rpc", 999.6, 2 * MINUTE_MS)].forEach(summarizer.addLine);
69
70 const row = formatTraceSummary(summarizer.finish(), 25)
71 .split("\n")
72 .find((line) => line.startsWith("rpc"));
73 assert.deepStrictEqual(row?.trim().split(/\s+/).slice(3, 6), ["999ms", "1s", "1s"]);
74});