diff --git a/dev-packages/node-integration-tests/suites/pino/scenario-track.mjs b/dev-packages/node-integration-tests/suites/pino/scenario-track.mjs index 2e968444a74f..e55f11f9c00c 100644 --- a/dev-packages/node-integration-tests/suites/pino/scenario-track.mjs +++ b/dev-packages/node-integration-tests/suites/pino/scenario-track.mjs @@ -17,7 +17,18 @@ Sentry.withIsolationScope(() => { setTimeout(() => { Sentry.withIsolationScope(() => { Sentry.startSpan({ name: 'later' }, () => { - logger.error(new Error('oh no')); + // This child should be captured as we marked the parent logger to be tracked + const child = logger.child({ module: 'authentication' }); + child.error(new Error('oh no')); + + // This child should be ignored + const child2 = logger.child({ module: 'authentication.v2' }); + Sentry.pinoIntegration.untrackLogger(child2); + child2.error(new Error('oh no v2')); + + // This should also be ignored as the parent is ignored + const child3 = child2.child({ module: 'authentication.v3' }); + child3.error(new Error('oh no v3')); }); }); }, 1000); diff --git a/dev-packages/node-integration-tests/suites/pino/scenario.mjs b/dev-packages/node-integration-tests/suites/pino/scenario.mjs index beb080ac3c42..55966552a07f 100644 --- a/dev-packages/node-integration-tests/suites/pino/scenario.mjs +++ b/dev-packages/node-integration-tests/suites/pino/scenario.mjs @@ -17,7 +17,8 @@ Sentry.withIsolationScope(() => { setTimeout(() => { Sentry.withIsolationScope(() => { Sentry.startSpan({ name: 'later' }, () => { - logger.error(new Error('oh no')); + const child = logger.child({ module: 'authentication' }); + child.error(new Error('oh no')); }); }); }, 1000); diff --git a/dev-packages/node-integration-tests/suites/pino/test.ts b/dev-packages/node-integration-tests/suites/pino/test.ts index 1982c8d686fc..f9cdb143ddff 100644 --- a/dev-packages/node-integration-tests/suites/pino/test.ts +++ b/dev-packages/node-integration-tests/suites/pino/test.ts @@ -45,7 +45,6 @@ conditionalTest({ min: 20 })('Pino integration', () => { function: '?', in_app: true, module: 'scenario', - context_line: " logger.error(new Error('oh no'));", }), ]), }, @@ -63,8 +62,8 @@ conditionalTest({ min: 20 })('Pino integration', () => { body: 'hello world', trace_id: expect.any(String), severity_number: 9, - attributes: expect.objectContaining({ - 'pino.logger.name': { value: 'myapp', type: 'string' }, + attributes: { + name: { value: 'myapp', type: 'string' }, 'pino.logger.level': { value: 30, type: 'integer' }, user: { value: 'user-id', type: 'string' }, something: { @@ -74,7 +73,7 @@ conditionalTest({ min: 20 })('Pino integration', () => { 'sentry.origin': { value: 'auto.logging.pino', type: 'string' }, 'sentry.release': { value: '1.0', type: 'string' }, 'sentry.sdk.name': { value: 'sentry.javascript.node', type: 'string' }, - }), + }, }, { timestamp: expect.any(Number), @@ -82,14 +81,14 @@ conditionalTest({ min: 20 })('Pino integration', () => { body: 'oh no', trace_id: expect.any(String), severity_number: 17, - attributes: expect.objectContaining({ - 'pino.logger.name': { value: 'myapp', type: 'string' }, + attributes: { + name: { value: 'myapp', type: 'string' }, + module: { value: 'authentication', type: 'string' }, 'pino.logger.level': { value: 50, type: 'integer' }, - err: { value: '{}', type: 'string' }, 'sentry.origin': { value: 'auto.logging.pino', type: 'string' }, 'sentry.release': { value: '1.0', type: 'string' }, 'sentry.sdk.name': { value: 'sentry.javascript.node', type: 'string' }, - }), + }, }, ], }, @@ -139,8 +138,8 @@ conditionalTest({ min: 20 })('Pino integration', () => { body: 'hello world', trace_id: expect.any(String), severity_number: 9, - attributes: expect.objectContaining({ - 'pino.logger.name': { value: 'myapp', type: 'string' }, + attributes: { + name: { value: 'myapp', type: 'string' }, 'pino.logger.level': { value: 30, type: 'integer' }, user: { value: 'user-id', type: 'string' }, something: { @@ -150,7 +149,7 @@ conditionalTest({ min: 20 })('Pino integration', () => { 'sentry.origin': { value: 'auto.logging.pino', type: 'string' }, 'sentry.release': { value: '1.0', type: 'string' }, 'sentry.sdk.name': { value: 'sentry.javascript.node', type: 'string' }, - }), + }, }, { timestamp: expect.any(Number), @@ -158,14 +157,13 @@ conditionalTest({ min: 20 })('Pino integration', () => { body: 'oh no', trace_id: expect.any(String), severity_number: 17, - attributes: expect.objectContaining({ - 'pino.logger.name': { value: 'myapp', type: 'string' }, + attributes: { + name: { value: 'myapp', type: 'string' }, 'pino.logger.level': { value: 50, type: 'integer' }, - err: { value: '{}', type: 'string' }, 'sentry.origin': { value: 'auto.logging.pino', type: 'string' }, 'sentry.release': { value: '1.0', type: 'string' }, 'sentry.sdk.name': { value: 'sentry.javascript.node', type: 'string' }, - }), + }, }, ], }, @@ -189,18 +187,19 @@ conditionalTest({ min: 20 })('Pino integration', () => { body: 'hello world', trace_id: expect.any(String), severity_number: 9, - attributes: expect.objectContaining({ - 'pino.logger.name': { value: 'myapp', type: 'string' }, + attributes: { + name: { value: 'myapp', type: 'string' }, 'pino.logger.level': { value: 30, type: 'integer' }, user: { value: 'user-id', type: 'string' }, something: { type: 'string', value: '{"more":3,"complex":"nope"}', }, + msg: { value: 'hello world', type: 'string' }, 'sentry.origin': { value: 'auto.logging.pino', type: 'string' }, 'sentry.release': { value: '1.0', type: 'string' }, 'sentry.sdk.name': { value: 'sentry.javascript.node', type: 'string' }, - }), + }, }, { timestamp: expect.any(Number), @@ -208,14 +207,15 @@ conditionalTest({ min: 20 })('Pino integration', () => { body: 'oh no', trace_id: expect.any(String), severity_number: 17, - attributes: expect.objectContaining({ - 'pino.logger.name': { value: 'myapp', type: 'string' }, + attributes: { + name: { value: 'myapp', type: 'string' }, + module: { value: 'authentication', type: 'string' }, + msg: { value: 'oh no', type: 'string' }, 'pino.logger.level': { value: 50, type: 'integer' }, - err: { value: '{}', type: 'string' }, 'sentry.origin': { value: 'auto.logging.pino', type: 'string' }, 'sentry.release': { value: '1.0', type: 'string' }, 'sentry.sdk.name': { value: 'sentry.javascript.node', type: 'string' }, - }), + }, }, ], }, diff --git a/packages/node-core/src/integrations/pino.ts b/packages/node-core/src/integrations/pino.ts index dfc51d5022ff..7c3e0c2c813f 100644 --- a/packages/node-core/src/integrations/pino.ts +++ b/packages/node-core/src/integrations/pino.ts @@ -81,9 +81,23 @@ type DeepPartial = { [P in keyof T]?: T[P] extends object ? Partial : T[P]; }; +type PinoResult = { + level?: string; + time?: string; + pid?: number; + hostname?: string; + err?: Error; +} & Record; + +function stripIgnoredFields(result: PinoResult): PinoResult { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const { level, time, pid, hostname, err, ...rest } = result; + return rest; +} + const _pinoIntegration = defineIntegration((userOptions: DeepPartial = {}) => { const options: PinoOptions = { - autoInstrument: userOptions.autoInstrument === false ? userOptions.autoInstrument : DEFAULT_OPTIONS.autoInstrument, + autoInstrument: userOptions.autoInstrument !== false, error: { ...DEFAULT_OPTIONS.error, ...userOptions.error }, log: { ...DEFAULT_OPTIONS.log, ...userOptions.log }, }; @@ -112,27 +126,23 @@ const _pinoIntegration = defineIntegration((userOptions: DeepPartial = { - ...obj, + ...resultObj, 'sentry.origin': 'auto.logging.pino', 'pino.logger.level': levelNumber, }; - const parsedResult = JSON.parse(result) as { name?: string }; - - if (parsedResult.name) { - attributes['pino.logger.name'] = parsedResult.name; - } - _INTERNAL_captureLog({ level, message, attributes }); } @@ -153,8 +163,8 @@ const _pinoIntegration = defineIntegration((userOptions: DeepPartial { const { self, arguments: args, result } = data as { self: Pino; arguments: PinoHookArgs; result: string }; - onPinoStart(self, args, result); + onPinoStart(self, args, JSON.parse(result)); }); integratedChannel.end.subscribe(data => { @@ -174,7 +184,7 @@ const _pinoIntegration = defineIntegration((userOptions: DeepPartial