Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Always truncate stored breadcrumb messages to 2kb #15819

Merged
merged 2 commits into from
Mar 26, 2025
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
5 changes: 4 additions & 1 deletion packages/core/src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { isPlainObject } from './utils-hoist/is';
import { logger } from './utils-hoist/logger';
import { uuid4 } from './utils-hoist/misc';
import { generateTraceId } from './utils-hoist/propagationContext';
import { truncate } from './utils-hoist/string';
import { dateTimestampInSeconds } from './utils-hoist/time';
import { merge } from './utils/merge';
import { _getSpanForScope, _setSpanForScope } from './utils/spanOnScope';
Expand Down Expand Up @@ -474,9 +475,11 @@ export class Scope {
return this;
}

const mergedBreadcrumb = {
const mergedBreadcrumb: Breadcrumb = {
timestamp: dateTimestampInSeconds(),
...breadcrumb,
// Breadcrumb messages can theoretically be infinitely large and they're held in memory so we truncate them not to leak (too much) memory
message: breadcrumb.message ? truncate(breadcrumb.message, 2048) : breadcrumb.message,
Copy link
Member

@s1gr1d s1gr1d Mar 26, 2025

Choose a reason for hiding this comment

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

Should we maybe provide an option for people to overwrite this value of 2kb?

Like maxMessageLength as option in the breadcrumb integration.

Copy link
Member Author

Choose a reason for hiding this comment

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

We didn't have this option before and nobody asked for it so I would say no.

};

this._breadcrumbs.push(mergedBreadcrumb);
Expand Down
6 changes: 6 additions & 0 deletions packages/core/test/lib/scope.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ describe('Scope', () => {
expect(scope['_breadcrumbs']).toHaveLength(111);
});

test('addBreadcrumb will truncate the stored messages', () => {
const scope = new Scope();
scope.addBreadcrumb({ message: 'A'.repeat(10_000) });
expect(scope['_breadcrumbs'][0]?.message).toBe(`${'A'.repeat(2048)}...`);
});

test('setLevel', () => {
const scope = new Scope();
scope.setLevel('fatal');
Expand Down
3 changes: 1 addition & 2 deletions packages/node/src/integrations/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
defineIntegration,
getClient,
severityLevelFromString,
truncate,
} from '@sentry/core';

const INTEGRATION_NAME = 'Console';
Expand All @@ -26,7 +25,7 @@ export const consoleIntegration = defineIntegration(() => {
{
category: 'console',
level: severityLevelFromString(level),
message: truncate(util.format.apply(undefined, args), 2048), // 2KB
message: util.format.apply(undefined, args),
},
{
input: [...args],
Expand Down
22 changes: 0 additions & 22 deletions packages/node/test/integration/console.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,4 @@ describe('Console integration', () => {
},
);
});

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',
},
);
});
});
Loading