Skip to content

Commit 97aa82b

Browse files
Jack-Keenematus-tomlein
authored andcommitted
Truncate stack when application_error stack trace exceeds 8kb (close #1327)
PR #1328 * add truncate function to application error event
1 parent 9fe9ee9 commit 97aa82b

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@snowplow/browser-plugin-error-tracking",
5+
"comment": "Truncate error stack trace if it's size is greater than 8kb",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@snowplow/browser-plugin-error-tracking"
10+
}

plugins/browser-plugin-error-tracking/src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
dispatchToTrackersInCollection,
3737
} from '@snowplow/browser-tracker-core';
3838
import { buildSelfDescribingEvent, CommonEventProperties, SelfDescribingJson } from '@snowplow/tracker-core';
39+
import { truncateString } from './util';
3940

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

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

7981
dispatchToTrackersInCollection(trackers, _trackers, (t) => {
8082
t.core.track(
@@ -83,7 +85,7 @@ export function trackError(
8385
schema: 'iglu:com.snowplowanalytics.snowplow/application_error/jsonschema/1-0-1',
8486
data: {
8587
programmingLanguage: 'JAVASCRIPT',
86-
message: message ?? "JS Exception. Browser doesn't support ErrorEvent API",
88+
message: truncatedMessage ?? "JS Exception. Browser doesn't support ErrorEvent API",
8789
stackTrace: stack,
8890
lineNumber: lineno,
8991
lineColumn: colno,
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Truncate string if it's longer than 8192 chars
3+
*
4+
* @param string - The stack trace to truncate
5+
* @param maxLength - The maximum length of the truncated string. Default = 8192
6+
* @returns The truncated string
7+
*/
8+
export function truncateString(string?: string, maxLength: number = 8192): string | undefined {
9+
if (string && string.length > maxLength) {
10+
const truncatedString = string.substring(0, maxLength);
11+
return truncatedString;
12+
}
13+
14+
return string;
15+
}

plugins/browser-plugin-error-tracking/test/events.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,18 @@ describe('AdTrackingPlugin', () => {
6060
encodeBase64: false,
6161
plugins: [ErrorTrackingPlugin()],
6262
});
63+
addTracker('sp4', 'sp4', 'js-3.0.0', '', state, {
64+
encodeBase64: false,
65+
plugins: [ErrorTrackingPlugin()],
66+
});
6367

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

71+
const oversizedError = new Error('this is a longer error');
72+
oversizedError.stack = 'x'.repeat(10000);
73+
const oversizedMessage = 'y'.repeat(10000);
74+
6775
trackError(
6876
{
6977
message: 'message-1',
@@ -89,6 +97,14 @@ describe('AdTrackingPlugin', () => {
8997
['sp3']
9098
);
9199

100+
trackError(
101+
{
102+
message: oversizedMessage,
103+
error: oversizedError,
104+
},
105+
['sp4']
106+
);
107+
92108
it('trackError adds the expected application error event to the queue', () => {
93109
expect(
94110
extractUeEvent('iglu:com.snowplowanalytics.snowplow/application_error/jsonschema/1-0-1').from(state.outQueues[0])
@@ -126,4 +142,16 @@ describe('AdTrackingPlugin', () => {
126142
},
127143
});
128144
});
145+
146+
it('trackError replaces undefined messages with placeholder', () => {
147+
expect(
148+
extractUeEvent('iglu:com.snowplowanalytics.snowplow/application_error/jsonschema/1-0-1').from(state.outQueues[3])
149+
).toMatchObject({
150+
schema: 'iglu:com.snowplowanalytics.snowplow/application_error/jsonschema/1-0-1',
151+
data: {
152+
message: 'y'.repeat(2048),
153+
stackTrace: 'x'.repeat(8192),
154+
},
155+
});
156+
});
129157
});

0 commit comments

Comments
 (0)