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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Sentry from '@sentry/node';
import pino from 'pino-next';

const logger = pino({});
const logger = pino({ name: 'myapp' });

Sentry.withIsolationScope(() => {
Sentry.startSpan({ name: 'startup' }, () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Sentry from '@sentry/node';
import pino from 'pino';

const logger = pino({});
const logger = pino({ name: 'myapp' });

Sentry.withIsolationScope(() => {
Sentry.startSpan({ name: 'startup' }, () => {
Expand Down
20 changes: 12 additions & 8 deletions dev-packages/node-integration-tests/suites/pino/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,14 @@ conditionalTest({ min: 20 })('Pino integration', () => {
trace_id: expect.any(String),
severity_number: 9,
attributes: expect.objectContaining({
'sentry.origin': { value: 'auto.logging.pino', type: 'string' },
'sentry.pino.level': { value: 30, type: 'integer' },
'pino.logger.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"}',
},
'sentry.origin': { value: 'auto.logging.pino', type: 'string' },
'sentry.release': { value: '1.0', type: 'string' },
'sentry.sdk.name': { value: 'sentry.javascript.node', type: 'string' },
}),
Expand All @@ -82,9 +83,10 @@ conditionalTest({ min: 20 })('Pino integration', () => {
trace_id: expect.any(String),
severity_number: 17,
attributes: expect.objectContaining({
'sentry.origin': { value: 'auto.logging.pino', type: 'string' },
'sentry.pino.level': { value: 50, type: 'integer' },
'pino.logger.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' },
}),
Expand Down Expand Up @@ -138,13 +140,14 @@ conditionalTest({ min: 20 })('Pino integration', () => {
trace_id: expect.any(String),
severity_number: 9,
attributes: expect.objectContaining({
'sentry.origin': { value: 'auto.logging.pino', type: 'string' },
'sentry.pino.level': { value: 30, type: 'integer' },
'pino.logger.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"}',
},
'sentry.origin': { value: 'auto.logging.pino', type: 'string' },
'sentry.release': { value: '1.0', type: 'string' },
'sentry.sdk.name': { value: 'sentry.javascript.node', type: 'string' },
}),
Expand All @@ -156,9 +159,10 @@ conditionalTest({ min: 20 })('Pino integration', () => {
trace_id: expect.any(String),
severity_number: 17,
attributes: expect.objectContaining({
'sentry.origin': { value: 'auto.logging.pino', type: 'string' },
'sentry.pino.level': { value: 50, type: 'integer' },
'pino.logger.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' },
}),
Expand Down
36 changes: 23 additions & 13 deletions packages/node-core/src/integrations/pino.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,23 @@ export const pinoIntegration = defineIntegration((userOptions: DeepPartial<PinoO
const injectedChannel = tracingChannel('orchestrion:pino:pino-log');
const integratedChannel = tracingChannel('pino_asJson');

function onPinoStart(self: Pino, args: PinoHookArgs): void {
function onPinoStart(self: Pino, args: PinoHookArgs, result: string): void {
const [obj, message, levelNumber] = args;
const level = self?.levels?.labels?.[levelNumber] || 'info';

const attributes = {
...obj,
'sentry.origin': 'auto.logging.pino',
'sentry.pino.level': levelNumber,
};

if (enableLogs && options.log.levels.includes(level)) {
const attributes: Record<string, unknown> = {
...obj,
'sentry.origin': 'auto.logging.pino',
'pino.logger.level': levelNumber,
};

const parsedResult = JSON.parse(result) as { name?: string };
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Pino JSON Parsing Fails Without Error Handling

The JSON.parse(result) call lacks error handling. If the result string from Pino's asJson function is malformed, it throws an uncaught exception. This can crash the integration and prevent log capture.

Fix in Cursor Fix in Web

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the string doesn't parse as json we've got more fundamental issues!


if (parsedResult.name) {
attributes['pino.logger.name'] = parsedResult.name;
}

_INTERNAL_captureLog({ level, message, attributes });
}

Expand Down Expand Up @@ -135,14 +141,18 @@ export const pinoIntegration = defineIntegration((userOptions: DeepPartial<PinoO
}
}

injectedChannel.start.subscribe(data => {
const { self, arguments: args } = data as { self: Pino; arguments: PinoHookArgs };
onPinoStart(self, args);
injectedChannel.end.subscribe(data => {
const { self, arguments: args, result } = data as { self: Pino; arguments: PinoHookArgs; result: string };
onPinoStart(self, args, result);
});

integratedChannel.start.subscribe(data => {
const { instance, arguments: args } = data as { instance: Pino; arguments: PinoHookArgs };
onPinoStart(instance, args);
integratedChannel.end.subscribe(data => {
const {
instance,
arguments: args,
result,
} = data as { instance: Pino; arguments: PinoHookArgs; result: string };
onPinoStart(instance, args, result);
});
},
};
Expand Down