mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 07:07:31 +00:00
8632f93c37
A 5xx stack and its access-log line were two unrelated lines, the exception filter printed the stack with console.error and no request context, and the MCP, WebSocket, plugin RPC and cron entry points logged nothing per call. A self-hosted bug report could not be traced from a failure back to the call that caused it. Every HTTP request now runs under an id held in AsyncLocalStorage: a well-formed incoming X-Request-Id is kept, anything else is replaced by a UUID, and the response carries it in X-Request-Id. The logger tags every line written inside a unit of work with `[http <id>]`. The exception filter leaves a server-side failure on res.locals and the access log writes the request line and the stack as one entry under that id; without an access log watching the response it still prints the error itself. MCP tool, resource and prompt calls (through a new `around` seam on the registry), WebSocket messages, plugin RPC dispatches and cron ticks each run through traceEntry(), which gives the call its own id (remembering the outer one as its parent) and logs one line for it: debug on success, like the HTTP access log, and warn or error with the reason on failure. The call's result and timing are unchanged, a failed cron tick is logged once rather than twice, and a WebSocket handler rejection no longer reaches rxjs's asynchronous rethrow.
106 lines
4.2 KiB
TypeScript
106 lines
4.2 KiB
TypeScript
/**
|
|
* traceEntry (nest/audit/entry-trace.logger.ts): one correlation id and one
|
|
* log line per call of a non-HTTP entry point, without changing what the call
|
|
* returns or when it runs. TRACE-001 through TRACE-008.
|
|
*/
|
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
|
|
const log = vi.hoisted(() => ({
|
|
LOG_LEVEL: 'error',
|
|
logInfo: vi.fn(),
|
|
logDebug: vi.fn(),
|
|
logWarn: vi.fn(),
|
|
logError: vi.fn(),
|
|
}));
|
|
vi.mock('../../../src/nest/audit/audit-log.logger', () => log);
|
|
|
|
import { traceEntry, wasTraced } from '../../../src/nest/audit/entry-trace.logger';
|
|
import { currentCorrelation, runWithCorrelation } from '../../../src/nest/common/request-correlation';
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
describe('traceEntry', () => {
|
|
it('TRACE-001: a plain value comes back as it is, synchronously, with one debug line', () => {
|
|
const result = traceEntry('ws', 'join user=3', () => 42);
|
|
expect(result).toBe(42);
|
|
expect(log.logDebug).toHaveBeenCalledTimes(1);
|
|
expect(log.logDebug.mock.calls[0][0]).toMatch(/^ws join user=3 ok \d+ms$/);
|
|
});
|
|
|
|
it('TRACE-002: the call runs under its own correlation, nested under the current one', () => {
|
|
let inside: ReturnType<typeof currentCorrelation>;
|
|
runWithCorrelation({ id: 'req-1', kind: 'http' }, () => {
|
|
traceEntry('mcp', 'tool list_trips user=1', () => {
|
|
inside = currentCorrelation();
|
|
});
|
|
});
|
|
expect(inside!.kind).toBe('mcp');
|
|
expect(inside!.parentId).toBe('req-1');
|
|
expect(inside!.id).not.toBe('req-1');
|
|
expect(log.logDebug.mock.calls[0][0]).toContain(' parent=req-1');
|
|
});
|
|
|
|
it('TRACE-003: a promise comes back as that same promise, and the line waits for it to settle', async () => {
|
|
let resolve!: (value: string) => void;
|
|
const pending = new Promise<string>((r) => {
|
|
resolve = r;
|
|
});
|
|
const result = traceEntry('rpc', 'p1 trips.get', () => pending);
|
|
expect(result).toBe(pending);
|
|
expect(log.logDebug).not.toHaveBeenCalled();
|
|
resolve('done');
|
|
await expect(result).resolves.toBe('done');
|
|
await vi.waitFor(() => expect(log.logDebug).toHaveBeenCalledTimes(1));
|
|
});
|
|
|
|
it('TRACE-004: a synchronous throw is logged at warn and rethrown', () => {
|
|
expect(() =>
|
|
traceEntry('ws', 'join', () => {
|
|
throw new Error('nope');
|
|
}),
|
|
).toThrow('nope');
|
|
expect(log.logWarn.mock.calls[0][0]).toMatch(/^ws join failed \d+ms: nope$/);
|
|
});
|
|
|
|
it('TRACE-005: a rejection is logged with the configured level and wording, and marked as reported', async () => {
|
|
const boom = new Error('disk full');
|
|
const result = traceEntry('cron', 'auto-backup', () => Promise.reject(boom), {
|
|
failureLevel: 'error',
|
|
failureMessage: (error) => `Cron job "auto-backup" failed: ${(error as Error).message}`,
|
|
});
|
|
await expect(result).rejects.toBe(boom);
|
|
await vi.waitFor(() => expect(log.logError).toHaveBeenCalledWith('Cron job "auto-backup" failed: disk full'));
|
|
expect(wasTraced(boom)).toBe(true);
|
|
expect(wasTraced(new Error('other'))).toBe(false);
|
|
expect(wasTraced('a string')).toBe(false);
|
|
});
|
|
|
|
it('TRACE-006: a call that answers with a refusal is logged at warn with the reason', async () => {
|
|
const answer = { ok: false as const, error: 'FORBIDDEN' };
|
|
await traceEntry('rpc', 'p1 trips.update', () => Promise.resolve(answer), {
|
|
refusal: (value) => (value.ok ? null : value.error),
|
|
});
|
|
await vi.waitFor(() => expect(log.logWarn).toHaveBeenCalledTimes(1));
|
|
expect(log.logWarn.mock.calls[0][0]).toMatch(/^rpc p1 trips.update refused \d+ms: FORBIDDEN$/);
|
|
expect(log.logDebug).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('TRACE-007: every line the call writes carries the same correlation', async () => {
|
|
const seen: Array<string | undefined> = [];
|
|
await traceEntry('cron', 'docsync', async () => {
|
|
seen.push(currentCorrelation()?.id);
|
|
await new Promise((resolve) => setTimeout(resolve, 1));
|
|
seen.push(currentCorrelation()?.id);
|
|
});
|
|
expect(seen[0]).toBeDefined();
|
|
expect(seen[1]).toBe(seen[0]);
|
|
});
|
|
|
|
it('TRACE-008: two calls never share an id', () => {
|
|
const ids = [1, 2].map(() => traceEntry('ws', 'leave', () => currentCorrelation()?.id));
|
|
expect(ids[0]).not.toBe(ids[1]);
|
|
});
|
|
});
|