Skip to content

Commit 525d08c

Browse files
committed
normalize depth
1 parent a090220 commit 525d08c

4 files changed

Lines changed: 69 additions & 3 deletions

File tree

dev-packages/node-integration-tests/suites/consola/subject-object-context.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,24 @@ async function run(): Promise<void> {
2525
// Mixed primitives and objects
2626
consola.error('Error occurred', 'in payment module', { errorCode: 'E001', retryable: true });
2727

28-
// Aarrays (should be stored as context attributes)
28+
// Arrays (should be stored as context attributes)
2929
consola.debug('Processing items', [1, 2, 3, 4, 5]);
3030

3131
// Nested objects
3232
consola.info('Complex data', { user: { id: 789, name: 'Jane' }, metadata: { source: 'api' } });
3333

34+
// Deep nesting to test normalizeDepth (should be normalized at depth 3 - default)
35+
consola.info('Deep object', {
36+
level1: {
37+
level2: {
38+
level3: {
39+
level4: { level5: 'should be normalized' },
40+
},
41+
},
42+
},
43+
simpleKey: 'simple value',
44+
});
45+
3446
await Sentry.flush();
3547
}
3648

dev-packages/node-integration-tests/suites/consola/test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,26 @@ describe('consola integration', () => {
593593
metadata: { value: '{"source":"api"}', type: 'string' },
594594
},
595595
},
596+
{
597+
timestamp: expect.any(Number),
598+
level: 'info',
599+
body: 'Deep object',
600+
severity_number: expect.any(Number),
601+
trace_id: expect.any(String),
602+
attributes: {
603+
'sentry.origin': { value: 'auto.log.consola', type: 'string' },
604+
'sentry.release': { value: '1.0.0', type: 'string' },
605+
'sentry.environment': { value: 'test', type: 'string' },
606+
'sentry.sdk.name': { value: 'sentry.javascript.node', type: 'string' },
607+
'sentry.sdk.version': { value: expect.any(String), type: 'string' },
608+
'server.address': { value: expect.any(String), type: 'string' },
609+
'consola.type': { value: 'info', type: 'string' },
610+
'consola.level': { value: 3, type: 'integer' },
611+
// Nested objects are extracted and normalized respecting normalizeDepth setting
612+
level1: { value: '{"level2":{"level3":{"level4":"[Object]"}}}', type: 'string' },
613+
simpleKey: { value: 'simple value', type: 'string' },
614+
},
615+
},
596616
],
597617
},
598618
})

packages/core/src/integrations/consola.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { _INTERNAL_captureLog } from '../logs/internal';
44
import { formatConsoleArgs } from '../logs/utils';
55
import type { LogSeverityLevel } from '../types-hoist/log';
66
import { isPrimitive } from '../utils/is';
7+
import { normalize } from '../utils/normalize';
78

89
/**
910
* Options for the Sentry Consola reporter.
@@ -239,7 +240,12 @@ export function createConsolaReporter(options: ConsolaReporterOptions = {}): Con
239240
for (const key in arg) {
240241
// Only add if not conflicting with existing or consola-prefixed attributes
241242
if (!(key in attributes) && !(`consola.${key}` in attributes)) {
242-
attributes[key] = (arg as Record<string, unknown>)[key];
243+
// Normalize the value to respect normalizeDepth
244+
attributes[key] = normalize(
245+
(arg as Record<string, unknown>)[key],
246+
normalizeDepth,
247+
normalizeMaxBreadth,
248+
);
243249
}
244250
}
245251
} catch {

packages/core/test/lib/integrations/consola.test.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ describe('createConsolaReporter', () => {
213213
attributes: {
214214
'sentry.origin': 'auto.log.consola',
215215
'consola.type': 'info',
216-
self: circular,
216+
self: { self: '[Circular ~]' },
217217
},
218218
});
219219
});
@@ -318,6 +318,34 @@ describe('createConsolaReporter', () => {
318318
expect(captureCall.attributes.timestamp).toEqual(expect.any(Number));
319319
});
320320

321+
it('should respect normalizeDepth when extracting object properties', () => {
322+
const logObj = {
323+
type: 'info',
324+
args: [
325+
'Deep object',
326+
{
327+
level1: {
328+
level2: {
329+
level3: {
330+
level4: { level5: 'should be normalized' }, // beause of normalizeDepth=3
331+
},
332+
},
333+
},
334+
simpleKey: 'simple value',
335+
},
336+
],
337+
};
338+
339+
sentryReporter.log(logObj);
340+
341+
const captureCall = vi.mocked(_INTERNAL_captureLog).mock.calls[0][0];
342+
expect(captureCall.level).toBe('info');
343+
expect(captureCall.message).toBe('Deep object');
344+
expect(captureCall.attributes['sentry.origin']).toBe('auto.log.consola');
345+
expect(captureCall.attributes.level1).toEqual({ level2: { level3: { level4: '[Object]' } } });
346+
expect(captureCall.attributes.simpleKey).toBe('simple value');
347+
});
348+
321349
it('should map consola levels to sentry levels when type is not provided', () => {
322350
const logObj = {
323351
level: 0, // Fatal level

0 commit comments

Comments
 (0)