Open History to see the last commit
1import fs from "node:fs";
2import os from "node:os";
3import path from "node:path";
4
5import { assert, describe, it } from "@effect/vitest";
6import { Effect } from "effect";
7
8import type { TraceRecord } from "./TraceRecord.ts";
9import { makeTraceSink } from "./TraceSink.ts";
10
11const makeRecord = (name: string, suffix = ""): TraceRecord => ({
12 type: "effect-span",
13 name,
14 traceId: `trace-${name}-${suffix}`,
15 spanId: `span-${name}-${suffix}`,
16 sampled: true,
17 kind: "internal",
18 startTimeUnixNano: "1",
19 endTimeUnixNano: "2",
20 durationMs: 1,
21 attributes: {
22 payload: suffix,
23 },
24 events: [],
25 links: [],
26 exit: {
27 _tag: "Success",
28 },
29});
30
31describe("TraceSink", () => {
32 it.effect("flushes buffered records on close", () =>
33 Effect.scoped(
34 Effect.gen(function* () {
35 const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "t3-trace-sink-"));
36 const tracePath = path.join(tempDir, "server.trace.ndjson");
37
38 try {
39 const sink = yield* makeTraceSink({
40 filePath: tracePath,
41 maxBytes: 1024,
42 maxFiles: 2,
43 batchWindowMs: 10_000,
44 });
45
46 sink.push(makeRecord("alpha"));
47 sink.push(makeRecord("beta"));
48 yield* sink.close();
49
50 const lines = fs
51 .readFileSync(tracePath, "utf8")
52 .trim()
53 .split("\n")
54 .map((line) => JSON.parse(line) as TraceRecord);
55
56 assert.equal(lines.length, 2);
57 assert.equal(lines[0]?.name, "alpha");
58 assert.equal(lines[1]?.name, "beta");
59 } finally {
60 fs.rmSync(tempDir, { recursive: true, force: true });
61 }
62 }),
63 ),
64 );
65
66 it.effect("rotates the trace file when the configured max size is exceeded", () =>
67 Effect.scoped(
68 Effect.gen(function* () {
69 const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "t3-trace-sink-"));
70 const tracePath = path.join(tempDir, "server.trace.ndjson");
71
72 try {
73 const sink = yield* makeTraceSink({
74 filePath: tracePath,
75 maxBytes: 180,
76 maxFiles: 2,
77 batchWindowMs: 10_000,
78 });
79
80 for (let index = 0; index < 8; index += 1) {
81 sink.push(makeRecord("rotate", `${index}-${"x".repeat(48)}`));
82 yield* sink.flush;
83 }
84 yield* sink.close();
85
86 const matchingFiles = fs
87 .readdirSync(tempDir)
88 .filter(
89 (entry) =>
90 entry === "server.trace.ndjson" || entry.startsWith("server.trace.ndjson."),
91 )
92 .toSorted();
93
94 assert.equal(
95 matchingFiles.some((entry) => entry === "server.trace.ndjson.1"),
96 true,
97 );
98 assert.equal(
99 matchingFiles.some((entry) => entry === "server.trace.ndjson.3"),
100 false,
101 );
102 } finally {
103 fs.rmSync(tempDir, { recursive: true, force: true });
104 }
105 }),
106 ),
107 );
108
109 it.effect("drops only the invalid record when serialization fails", () =>
110 Effect.scoped(
111 Effect.gen(function* () {
112 const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "t3-trace-sink-"));
113 const tracePath = path.join(tempDir, "server.trace.ndjson");
114
115 try {
116 const sink = yield* makeTraceSink({
117 filePath: tracePath,
118 maxBytes: 1024,
119 maxFiles: 2,
120 batchWindowMs: 10_000,
121 });
122
123 const circular: Array<unknown> = [];
124 circular.push(circular);
125
126 sink.push(makeRecord("alpha"));
127 sink.push({
128 ...makeRecord("invalid"),
129 attributes: {
130 circular,
131 },
132 } as TraceRecord);
133 sink.push(makeRecord("beta"));
134 yield* sink.close();
135
136 const lines = fs
137 .readFileSync(tracePath, "utf8")
138 .trim()
139 .split("\n")
140 .map((line) => JSON.parse(line) as TraceRecord);
141
142 assert.deepStrictEqual(
143 lines.map((line) => line.name),
144 ["alpha", "beta"],
145 );
146 } finally {
147 fs.rmSync(tempDir, { recursive: true, force: true });
148 }
149 }),
150 ),
151 );
152});