Repository navigation
Expand file tree
/
Copy pathtrace.test.ts
More file actions
74 lines (64 loc) · 2.3 KB
/
Copy pathtrace.test.ts
File metadata and controls
74 lines (64 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { assert, it } from "@effect/vitest";
import { formatTraceSummary, makeTraceSpanSummary } from "./trace.ts";
const MINUTE_MS = 60_000;
function span(name: string, durationMs: number, endMs: number, exitTag = "Success") {
return JSON.stringify({
type: "effect-span",
name,
traceId: "trace",
spanId: "span",
durationMs,
endTimeUnixNano: String(BigInt(endMs) * 1_000_000n),
exit: { _tag: exitTag, cause: "cause" },
});
}
function browserSpan(
name: string,
status: { code: string; message?: string },
attributes: Record<string, unknown> = {},
) {
return JSON.stringify({
type: "otlp-span",
name,
durationMs: 1,
endTimeUnixNano: "1000000",
status,
attributes,
});
}
it("drops spans that ended before the window and counts unreadable lines", () => {
const summarizer = makeTraceSpanSummary(MINUTE_MS);
[
span("old", 1, 0),
"",
"{not json",
JSON.stringify({ name: "no-duration" }),
JSON.stringify({ name: "far-future", durationMs: 1, endTimeUnixNano: "9".repeat(22) }),
span("recent", 4, 5 * MINUTE_MS),
].forEach(summarizer.addLine);
const summary = summarizer.finish();
assert.strictEqual(summary.skippedLineCount, 3);
assert.deepStrictEqual(
summary.spans.map((entry) => [entry.name, entry.count, entry.perMinute]),
[["recent", 1, undefined]],
);
});
it("reads failures and interrupts of browser spans from their OTLP status", () => {
const summarizer = makeTraceSpanSummary();
[
browserSpan("render", { code: "2", message: "boom" }),
browserSpan("render", { code: "0" }, { "effect.fiber.interrupted": true }),
browserSpan("render", { code: "1", message: "Interrupted" }),
browserSpan("render", { code: "1" }),
].forEach(summarizer.addLine);
const [render] = summarizer.finish().spans;
assert.deepStrictEqual([render?.count, render?.interrupted, render?.failures], [4, 2, 1]);
});
it("prints durations that round up to a second in seconds", () => {
const summarizer = makeTraceSpanSummary();
[span("rpc", 999.4, MINUTE_MS), span("rpc", 999.6, 2 * MINUTE_MS)].forEach(summarizer.addLine);
const row = formatTraceSummary(summarizer.finish(), 25)
.split("\n")
.find((line) => line.startsWith("rpc"));
assert.deepStrictEqual(row?.trim().split(/\s+/).slice(3, 6), ["999ms", "1s", "1s"]);
});