-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrail.ts
More file actions
315 lines (271 loc) · 7.91 KB
/
trail.ts
File metadata and controls
315 lines (271 loc) · 7.91 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import { randomUUID } from "node:crypto";
import { readFileSync } from "node:fs";
import type {
Crumb,
CrumbOptions,
CrumbType,
Session,
Sink,
TrailFunction,
} from "./types.js";
import { NOOP } from "./noop.js";
import { isNamespaceEnabled, getCollectorUrl, getFormat } from "./env.js";
import { getContext, runWithContext, type DebugContext } from "./context.js";
import { ConsoleSink } from "./sinks/console.js";
import { HttpSink } from "./sinks/socket.js";
const SESSION_FILE = "/tmp/agentcrumbs.session";
const globalSinks: Sink[] = [];
let sinksInitialized = false;
function ensureSinks(): void {
if (sinksInitialized) return;
sinksInitialized = true;
// HTTP sink — fire-and-forget POST to collector. If collector isn't
// running, fetch() silently fails. No connection state to manage.
const url = getCollectorUrl();
globalSinks.push(new HttpSink(url));
// Console sink — always on as fallback. Crumbs show in stderr so
// they're visible even without the collector running.
const format = getFormat();
if (format === "json") {
globalSinks.push({
write(crumb: Crumb) {
process.stderr.write(JSON.stringify(crumb) + "\n");
},
});
} else {
globalSinks.push(new ConsoleSink());
}
}
export function addSink(sink: Sink): void {
globalSinks.push(sink);
sinksInitialized = true;
}
export function removeSink(sink: Sink): void {
const idx = globalSinks.indexOf(sink);
if (idx !== -1) globalSinks.splice(idx, 1);
}
/** Reset sinks — for testing */
export function resetSinks(): void {
globalSinks.length = 0;
sinksInitialized = false;
}
function emit(crumb: Crumb): void {
ensureSinks();
for (const sink of globalSinks) {
try {
sink.write(crumb);
} catch {
// Never let a sink error affect the application
}
}
}
function getCliSessionId(): string | undefined {
try {
const content = readFileSync(SESSION_FILE, "utf-8").trim();
return content || undefined;
} catch {
return undefined;
}
}
function createTrailFunction(
namespace: string,
parentCtx?: Record<string, unknown>
): TrailFunction {
let lastTime = performance.now();
const timers = new Map<string, number>();
function makeCrumb(
msg: string,
type: CrumbType,
data?: unknown,
options?: CrumbOptions,
overrides?: Partial<Crumb>
): Crumb {
const now = performance.now();
const dt = now - lastTime;
lastTime = now;
const asyncCtx = getContext();
const cliSession = getCliSessionId();
const crumb: Crumb = {
ts: new Date().toISOString(),
ns: namespace,
msg,
type,
dt: Math.round(dt * 100) / 100,
pid: process.pid,
...overrides,
};
if (data !== undefined) crumb.data = data;
// Merge context: parent -> async -> explicit
const mergedCtx = {
...parentCtx,
...asyncCtx?.contextData,
};
if (Object.keys(mergedCtx).length > 0) crumb.ctx = mergedCtx;
if (!crumb.traceId && asyncCtx?.traceId) crumb.traceId = asyncCtx.traceId;
if (!crumb.depth && asyncCtx?.depth) crumb.depth = asyncCtx.depth;
// Session: prefer async context session, then CLI session
const sid = asyncCtx?.sessionId ?? cliSession;
if (sid) crumb.sid = sid;
if (options?.tags && options.tags.length > 0) crumb.tags = options.tags;
return crumb;
}
const fn = function trailFn(
msg: string,
data?: unknown,
options?: CrumbOptions
): void {
emit(makeCrumb(msg, "crumb", data, options));
};
fn.enabled = true;
fn.namespace = namespace;
fn.child = (ctx: Record<string, unknown>): TrailFunction => {
return createTrailFunction(namespace, { ...parentCtx, ...ctx });
};
fn.scope = <T>(
name: string,
scopeFn: (ctx: { crumb: TrailFunction; traceId: string }) => T | Promise<T>
): T | Promise<T> => {
const traceId = randomUUID().slice(0, 8);
const asyncCtx = getContext();
const depth = (asyncCtx?.depth ?? 0) + 1;
const newCtx: DebugContext = {
namespace,
contextData: { ...parentCtx, ...asyncCtx?.contextData },
traceId,
depth,
sessionId: asyncCtx?.sessionId,
};
emit(makeCrumb(name, "scope:enter", undefined, undefined, { traceId, depth }));
const startTime = performance.now();
const childTrail = createTrailFunction(namespace, newCtx.contextData);
const finish = (error?: unknown) => {
const duration = Math.round((performance.now() - startTime) * 100) / 100;
if (error) {
const errorData =
error instanceof Error
? { message: error.message, stack: error.stack, name: error.name }
: { value: error };
emit(
makeCrumb(name, "scope:error", { ...errorData, duration }, undefined, {
traceId,
depth,
})
);
} else {
emit(
makeCrumb(name, "scope:exit", { duration }, undefined, {
traceId,
depth,
})
);
}
};
try {
const result = runWithContext(newCtx, () =>
scopeFn({ crumb: childTrail, traceId })
);
if (result instanceof Promise) {
return result.then(
(val) => {
finish();
return val;
},
(err) => {
finish(err);
throw err;
}
) as T | Promise<T>;
}
finish();
return result;
} catch (err) {
finish(err);
throw err;
}
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
fn.wrap = <T extends (...args: any[]) => any>(
name: string,
wrappedFn: T
): T => {
return ((...args: unknown[]) => {
return fn.scope(name, () => wrappedFn(...args));
}) as T;
};
fn.time = (label: string): void => {
timers.set(label, performance.now());
};
fn.timeEnd = (label: string, data?: unknown): void => {
const start = timers.get(label);
if (start === undefined) return;
timers.delete(label);
const duration = Math.round((performance.now() - start) * 100) / 100;
emit(makeCrumb(label, "time", { ...((data as object) ?? {}), duration }));
};
fn.snapshot = (label: string, obj: unknown): void => {
let cloned: unknown;
try {
cloned = structuredClone(obj);
} catch {
cloned = obj;
}
emit(makeCrumb(label, "snapshot", cloned));
};
fn.assert = (condition: unknown, msg: string): void => {
if (!condition) {
emit(makeCrumb(msg, "assert", { passed: false }));
}
};
fn.session = ((
name: string,
sessionFn?: (session: Session) => unknown
): unknown => {
const id = randomUUID().slice(0, 8);
const asyncCtx = getContext();
const sessionCtx: DebugContext = {
namespace,
contextData: { ...parentCtx, ...asyncCtx?.contextData },
traceId: asyncCtx?.traceId ?? "",
depth: asyncCtx?.depth ?? 0,
sessionId: id,
};
emit(makeCrumb(name, "session:start", undefined, undefined, { sid: id }));
const session: Session = {
id,
name,
crumb: (msg: string, data?: unknown, options?: CrumbOptions) => {
runWithContext(sessionCtx, () => {
emit(makeCrumb(msg, "crumb", data, options, { sid: id }));
});
},
end: () => {
emit(makeCrumb(name, "session:end", undefined, undefined, { sid: id }));
},
};
if (typeof sessionFn === "function") {
const result = runWithContext(sessionCtx, () => sessionFn(session));
if (result instanceof Promise) {
return result.then(
(val) => {
session.end();
return val;
},
(err) => {
session.end();
throw err;
}
);
}
session.end();
return result;
}
return session;
}) as TrailFunction["session"];
return fn as TrailFunction;
}
export function trail(namespace: string): TrailFunction {
if (!isNamespaceEnabled(namespace)) {
return NOOP;
}
return createTrailFunction(namespace);
}