Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .changeset/37658-observability-loggers-cause-chain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'@objectstack/observability': patch
---

fix(observability): `ConsoleLogger` and `JsonLogger` record an error's `cause` chain

Clause-②: yes (widening)
Metadata-protocol: none

`error(message, err)` and `fatal(message, err)` on both loggers used to record the error's own message and stack only, so an error wrapped around an original on `cause` (`new Error(say, { cause })`) logged without its root cause. The record now carries `causes` beside `error` and `stack`: one entry per link, outermost first, each with the link's message and stack. The chain is bounded at five links and stops at the first repeat of a cycle, ending in a marker entry whenever it was cut. It is the same shape the 5xx fault line (`faultCauseChain` in `@objectstack/types`) and `ObjectLogger` in `@objectstack/core` record.

An error with no `cause` logs exactly as before: `causes` is omitted. No new exports, and no new runtime dependency.
20 changes: 20 additions & 0 deletions packages/core/src/logger-error-cause.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,26 @@ describe('a cause is redacted like the error', () => {
});
});

/**
* The glued redact qualifiers (`CONCATENATED_SECRET_QUALIFIERS`, one copy in
* `logger.ts` and one in `@objectstack/types`), each glued to a default word,
* so an edit to either copy's list fails the parity block below.
*/
const GLUED_QUALIFIERS = [
'access', 'account', 'admin', 'api', 'app', 'auth', 'bearer', 'client', 'csrf', 'db', 'database',
'encryption', 'id', 'jwt', 'master', 'oauth', 'private', 'public', 'refresh', 'root', 'secret',
'service', 'session', 'shared', 'sign', 'signing', 'ssh', 'token', 'user', 'webhook', 'xsrf',
];
const gluedQualifierCause = () => Object.fromEntries(GLUED_QUALIFIERS.map((qualifier) => [`${qualifier}key`, 'x']));

describe('parity — the logger and the fault log render one shape', () => {
it('CONTROL — the glued-qualifier fixture is withheld field by field, so it exercises each qualifier', () => {
const [line] = faultCauseChain(new Error('w', { cause: gluedQualifierCause() }));
const parsed = JSON.parse(line.message) as Record<string, string>;
expect(Object.keys(parsed)).toHaveLength(GLUED_QUALIFIERS.length);
expect(Object.values(parsed).every((value) => value === '***REDACTED***')).toBe(true);
});

const cycle = () => {
const a = new Error('a');
(a as { cause?: unknown }).cause = new Error('b', { cause: a });
Expand All @@ -138,6 +157,7 @@ describe('parity — the logger and the fault log render one shape', () => {
apiKey: 'x', apikey: 'x', accesstoken: 'x', refresh_tokens: ['x'], clientSecret: 'x', password: 'x', API_KEY: 'x',
},
})],
['every glued qualifier on a default word', () => new Error('w', { cause: gluedQualifierCause() })],
['an array inside an object cause', () => new Error('w', {
cause: { code: 'E3', attempts: [{ host: 'db-1', password: 'x' }, { host: 'db-2', authToken: 'y' }] },
})],
Expand Down
1 change: 1 addition & 0 deletions packages/observability/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@objectstack/spec": "workspace:*"
},
"devDependencies": {
"@objectstack/types": "workspace:*",
"@types/node": "^26.6.5",
"typescript": "^6.0.3",
"vitest": "^4.1.11"
Expand Down
164 changes: 164 additions & 0 deletions packages/observability/src/__tests__/loggers.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.

import { describe, it, expect } from 'vitest';
import { faultCauseChain, FAULT_CAUSE_DEPTH_LIMIT } from '@objectstack/types';
import type { Logger } from '../contracts.js';
import { NoopLogger, ConsoleLogger, JsonLogger } from '../loggers.js';

Expand Down Expand Up @@ -112,3 +113,166 @@ describe('JsonLogger', () => {
expect(error).toHaveLength(1);
});
});

// ---------------------------------------------------------------------------
// An error record carries the error's `cause` chain, in the shape the 5xx fault
// line (`faultCauseChain`, `@objectstack/types`) and `ObjectLogger` record. The
// loggers keep their own copy (this package depends on `@objectstack/spec`
// alone); the parity block pins it to `faultCauseChain`'s output.
// ---------------------------------------------------------------------------

/** `new Error(message, { cause })`, spelled for the ES2020 `lib` this package compiles under. */
const withCause = (message: string, cause: unknown): Error => Object.assign(new Error(message), { cause });

type CauseLine = { message: string; stack?: string };
type ErrorRecord = { error?: string; stack?: string; causes?: CauseLine[] };

const ROOT = 'SQLITE_IOERR: disk I/O error reading policy_revision.version';
const MID = 'driver read failed for policy_revision';
const OUTER = 'The related records of policy could not be read.';

const wrapped = () => withCause(OUTER, withCause(MID, new Error(ROOT)));

/** The JSON tail of a `ConsoleLogger` line: `[error] msg {...}`. */
function consoleRecord(line: string, msg: string): ErrorRecord {
const prefix = `[error] ${msg} `;
expect(line.startsWith(prefix)).toBe(true);
return JSON.parse(line.slice(prefix.length)) as ErrorRecord;
}

/** One error record from each logger, for the same call. */
const LOGGERS: Array<[string, (error: Error) => ErrorRecord]> = [
['ConsoleLogger', (error) => {
const { sink, error: lines } = makeSink();
new ConsoleLogger({ level: 'debug', sink }).error('boom', error);
expect(lines).toHaveLength(1);
return consoleRecord(lines[0], 'boom');
}],
['JsonLogger', (error) => {
const { sink, error: lines } = makeSink();
new JsonLogger({ level: 'debug', sink }).error('boom', error);
expect(lines).toHaveLength(1);
return JSON.parse(lines[0]) as ErrorRecord;
}],
];

describe.each(LOGGERS)('%s — an error record carries the cause chain', (_name, recordOf) => {
it('logs each cause\'s message and stack, outermost first', () => {
const record = recordOf(wrapped());
expect(record.error).toBe(OUTER);
expect(record.stack).toContain(OUTER);
const causes = record.causes ?? [];
expect(causes).toHaveLength(2);
expect(causes[0].message).toBe(`Error: ${MID}`);
expect(causes[0].stack).toContain(MID);
expect(causes[1].message).toBe(`Error: ${ROOT}`);
expect(causes[1].stack).toContain(ROOT);
});

it('CONTROL — an error with no cause carries no causes field', () => {
const record = recordOf(new Error(OUTER));
expect(record.error).toBe(OUTER);
expect(record).not.toHaveProperty('causes');
});

it('stops a cycle at its first repeat, and says so', () => {
const a = new Error('a');
const b = withCause('b', a);
(a as { cause?: unknown }).cause = b;
expect(recordOf(a).causes?.map((c) => c.message)).toEqual([
'Error: b',
'[cause chain cycle: this link repeats an earlier one]',
]);
});

it(`stops a deep chain after ${FAULT_CAUSE_DEPTH_LIMIT} links, and says so`, () => {
let error = new Error('link 0');
for (let i = 1; i <= 20; i += 1) error = withCause(`link ${i}`, error);
const causes = recordOf(error).causes ?? [];
expect(causes).toHaveLength(FAULT_CAUSE_DEPTH_LIMIT + 1);
expect(causes[FAULT_CAUSE_DEPTH_LIMIT]).toEqual({
message: `[cause chain truncated after ${FAULT_CAUSE_DEPTH_LIMIT} links]`,
});
});
});

describe('fatal carries the cause chain too', () => {
it('ConsoleLogger', () => {
const { sink, error } = makeSink();
new ConsoleLogger({ level: 'debug', sink }).fatal('down', wrapped());
const record = JSON.parse(error[0].slice('[fatal] down '.length)) as ErrorRecord;
expect(record.causes?.[1].message).toBe(`Error: ${ROOT}`);
});

it('JsonLogger', () => {
const { sink, error } = makeSink();
new JsonLogger({ level: 'debug', sink }).fatal('down', wrapped());
expect((JSON.parse(error[0]) as ErrorRecord).causes?.[1].message).toBe(`Error: ${ROOT}`);
});
});

/** The redact qualifiers a glued field name may start with, each glued to a default word. */
const GLUED_QUALIFIERS = [
'access', 'account', 'admin', 'api', 'app', 'auth', 'bearer', 'client', 'csrf', 'db', 'database',
'encryption', 'id', 'jwt', 'master', 'oauth', 'private', 'public', 'refresh', 'root', 'secret',
'service', 'session', 'shared', 'sign', 'signing', 'ssh', 'token', 'user', 'webhook', 'xsrf',
];
const gluedQualifierCause = () => Object.fromEntries(GLUED_QUALIFIERS.map((qualifier) => [`${qualifier}key`, 'x']));

describe('the glued-qualifier fixture', () => {
it('CONTROL — faultCauseChain withholds it field by field, so it exercises each qualifier', () => {
const [line] = faultCauseChain(withCause('w', gluedQualifierCause()));
const parsed = JSON.parse(line.message) as Record<string, string>;
expect(Object.keys(parsed)).toHaveLength(GLUED_QUALIFIERS.length);
expect(Object.values(parsed).every((value) => value === '***REDACTED***')).toBe(true);
});
});

describe.each(LOGGERS)('parity — %s and faultCauseChain render one shape', (_name, recordOf) => {
const cycle = () => {
const a = new Error('a');
(a as { cause?: unknown }).cause = withCause('b', a);
return a;
};
const deep = () => {
let error = new Error('link 0');
for (let i = 1; i <= 9; i += 1) error = withCause(`link ${i}`, error);
return error;
};
const fixtures: Array<[string, () => Error]> = [
['a nested Error chain', wrapped],
['a cycle', cycle],
['a deep chain', deep],
['a string cause', () => withCause('w', 'socket hang up')],
['an object cause', () => withCause('w', { code: 'ECONNRESET' })],
['an object cause with secret-named fields', () => withCause('w', { code: 'E1', password: 'p', apiKey: 'k' })],
['field names under the default redact words, whole-word matched', () => withCause('w', {
keys: ['a'], tokens: 3, monkey: 'm', keyword: 'k', secretary: 's', tokenizer: 't',
apiKey: 'x', apikey: 'x', accesstoken: 'x', refresh_tokens: ['x'], clientSecret: 'x', password: 'x', API_KEY: 'x',
})],
['every glued qualifier on a default word', () => withCause('w', gluedQualifierCause())],
['an array inside an object cause', () => withCause('w', {
code: 'E3', attempts: [{ host: 'db-1', password: 'x' }, { host: 'db-2', authToken: 'y' }],
})],
['an object cause that cannot be serialized', () => {
const loop: Record<string, unknown> = { code: 'E2' };
loop.self = loop;
return withCause('w', loop);
}],
['a link whose message read throws', () => {
const unreadable = new Error('unreadable');
Object.defineProperty(unreadable, 'message', { get() { throw new Error('getter'); } });
return withCause('w', withCause('mid', unreadable));
}],
['a throwing cause getter', () => {
const hostile = new Error('w');
Object.defineProperty(hostile, 'cause', { get() { throw new Error('getter'); } });
return hostile;
}],
];
it.each(fixtures)('%s', (_fixture, make) => {
const error = make();
// The record omits `causes` when the chain is empty; the fault log's helper answers `[]`.
expect(recordOf(error).causes ?? []).toEqual(faultCauseChain(error));
});
});
Loading
Loading