Skip to content

Commit

Permalink
Truncate stack when application_error stack trace exceeds 8kb (close #…
Browse files Browse the repository at this point in the history
…1327)

PR #1328 
* add truncate function to application error event
  • Loading branch information
Jack-Keene authored and matus-tomlein committed Jul 24, 2024
1 parent 9fe9ee9 commit 97aa82b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@snowplow/browser-plugin-error-tracking",
"comment": "Truncate error stack trace if it's size is greater than 8kb",
"type": "none"
}
],
"packageName": "@snowplow/browser-plugin-error-tracking"
}
6 changes: 4 additions & 2 deletions plugins/browser-plugin-error-tracking/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
dispatchToTrackersInCollection,
} from '@snowplow/browser-tracker-core';
import { buildSelfDescribingEvent, CommonEventProperties, SelfDescribingJson } from '@snowplow/tracker-core';
import { truncateString } from './util';

let _trackers: Record<string, BrowserTracker> = {};

Expand Down Expand Up @@ -74,7 +75,8 @@ export function trackError(
trackers: Array<string> = Object.keys(_trackers)
) {
const { message, filename, lineno, colno, error, context, timestamp } = event,
stack = error && error.stack ? error.stack : null;
stack = error && truncateString(error.stack, 8192),
truncatedMessage = message && truncateString(message, 2048);

dispatchToTrackersInCollection(trackers, _trackers, (t) => {
t.core.track(
Expand All @@ -83,7 +85,7 @@ export function trackError(
schema: 'iglu:com.snowplowanalytics.snowplow/application_error/jsonschema/1-0-1',
data: {
programmingLanguage: 'JAVASCRIPT',
message: message ?? "JS Exception. Browser doesn't support ErrorEvent API",
message: truncatedMessage ?? "JS Exception. Browser doesn't support ErrorEvent API",
stackTrace: stack,
lineNumber: lineno,
lineColumn: colno,
Expand Down
15 changes: 15 additions & 0 deletions plugins/browser-plugin-error-tracking/src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Truncate string if it's longer than 8192 chars
*
* @param string - The stack trace to truncate
* @param maxLength - The maximum length of the truncated string. Default = 8192
* @returns The truncated string
*/
export function truncateString(string?: string, maxLength: number = 8192): string | undefined {
if (string && string.length > maxLength) {
const truncatedString = string.substring(0, maxLength);
return truncatedString;
}

return string;
}
28 changes: 28 additions & 0 deletions plugins/browser-plugin-error-tracking/test/events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,18 @@ describe('AdTrackingPlugin', () => {
encodeBase64: false,
plugins: [ErrorTrackingPlugin()],
});
addTracker('sp4', 'sp4', 'js-3.0.0', '', state, {
encodeBase64: false,
plugins: [ErrorTrackingPlugin()],
});

const error = new Error('this is an error');
error.stack = 'stacktrace-1';

const oversizedError = new Error('this is a longer error');
oversizedError.stack = 'x'.repeat(10000);
const oversizedMessage = 'y'.repeat(10000);

trackError(
{
message: 'message-1',
Expand All @@ -89,6 +97,14 @@ describe('AdTrackingPlugin', () => {
['sp3']
);

trackError(
{
message: oversizedMessage,
error: oversizedError,
},
['sp4']
);

it('trackError adds the expected application error event to the queue', () => {
expect(
extractUeEvent('iglu:com.snowplowanalytics.snowplow/application_error/jsonschema/1-0-1').from(state.outQueues[0])
Expand Down Expand Up @@ -126,4 +142,16 @@ describe('AdTrackingPlugin', () => {
},
});
});

it('trackError replaces undefined messages with placeholder', () => {
expect(
extractUeEvent('iglu:com.snowplowanalytics.snowplow/application_error/jsonschema/1-0-1').from(state.outQueues[3])
).toMatchObject({
schema: 'iglu:com.snowplowanalytics.snowplow/application_error/jsonschema/1-0-1',
data: {
message: 'y'.repeat(2048),
stackTrace: 'x'.repeat(8192),
},
});
});
});

0 comments on commit 97aa82b

Please sign in to comment.