Skip to content

Commit

Permalink
perf(node): Truncate breadcrumb messages created by console integrati…
Browse files Browse the repository at this point in the history
…on (#14006)
  • Loading branch information
lforst authored Oct 18, 2024
1 parent 77b3355 commit d3847b4
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 10 deletions.
17 changes: 7 additions & 10 deletions packages/node/src/integrations/console.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import * as util from 'node:util';
import { addBreadcrumb, defineIntegration, getClient } from '@sentry/core';
import type { IntegrationFn } from '@sentry/types';
import { addConsoleInstrumentationHandler, severityLevelFromString } from '@sentry/utils';
import { addConsoleInstrumentationHandler, severityLevelFromString, truncate } from '@sentry/utils';

const INTEGRATION_NAME = 'Console';

const _consoleIntegration = (() => {
/**
* Capture console logs as breadcrumbs.
*/
export const consoleIntegration = defineIntegration(() => {
return {
name: INTEGRATION_NAME,
setup(client) {
Expand All @@ -18,7 +20,7 @@ const _consoleIntegration = (() => {
{
category: 'console',
level: severityLevelFromString(level),
message: util.format.apply(undefined, args),
message: truncate(util.format.apply(undefined, args), 2048), // 2KB
},
{
input: [...args],
Expand All @@ -28,9 +30,4 @@ const _consoleIntegration = (() => {
});
},
};
}) satisfies IntegrationFn;

/**
* Capture console logs as breadcrumbs.
*/
export const consoleIntegration = defineIntegration(_consoleIntegration);
});
60 changes: 60 additions & 0 deletions packages/node/test/integration/console.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import * as SentryCore from '@sentry/core';
import { resetInstrumentationHandlers } from '@sentry/utils';
import { getClient } from '../../src';
import type { NodeClient } from '../../src';
import { consoleIntegration } from '../../src/integrations/console';

const addBreadcrumbSpy = jest.spyOn(SentryCore, 'addBreadcrumb');

jest.spyOn(console, 'log').mockImplementation(() => {
// noop so that we don't spam the logs
});

afterEach(() => {
jest.clearAllMocks();
resetInstrumentationHandlers();
});

describe('Console integration', () => {
it('should add a breadcrumb on console.log', () => {
consoleIntegration().setup?.(getClient() as NodeClient);

// eslint-disable-next-line no-console
console.log('test');

expect(addBreadcrumbSpy).toHaveBeenCalledTimes(1);
expect(addBreadcrumbSpy).toHaveBeenCalledWith(
{
category: 'console',
level: 'log',
message: 'test',
},
{
input: ['test'],
level: 'log',
},
);
});

it('should truncate breadcrumbs with more than 2 KB message size', () => {
consoleIntegration().setup?.(getClient() as NodeClient);

const longMsg = 'A'.repeat(10_000);

// eslint-disable-next-line no-console
console.log(longMsg);

expect(addBreadcrumbSpy).toHaveBeenCalledTimes(1);
expect(addBreadcrumbSpy).toHaveBeenCalledWith(
{
category: 'console',
level: 'log',
message: `${'A'.repeat(2048)}...`,
},
{
input: [longMsg],
level: 'log',
},
);
});
});

0 comments on commit d3847b4

Please sign in to comment.