Skip to content

Commit 3553eae

Browse files
committed
feat(replay): Stop fixing truncated JSONs in SDK
This is potentially performance intensive, so we should just do this in the UI instead.
1 parent 10fb09a commit 3553eae

File tree

12 files changed

+54
-569
lines changed

12 files changed

+54
-569
lines changed

packages/browser-integration-tests/suites/replay/captureConsoleLog/test.ts

+1-17
Original file line numberDiff line numberDiff line change
@@ -99,23 +99,7 @@ sentryTest('should capture very large console logs', async ({ getLocalTestPath,
9999
type: 'default',
100100
category: 'console',
101101
data: {
102-
arguments: [
103-
expect.objectContaining({
104-
'item-0': {
105-
aa: expect.objectContaining({
106-
'item-0': {
107-
aa: expect.any(Object),
108-
bb: expect.any(String),
109-
cc: expect.any(String),
110-
dd: expect.any(String),
111-
},
112-
}),
113-
bb: expect.any(String),
114-
cc: expect.any(String),
115-
dd: expect.any(String),
116-
},
117-
}),
118-
],
102+
arguments: [expect.any(String)],
119103
logger: 'console',
120104
_meta: {
121105
warnings: ['CONSOLE_ARG_TRUNCATED'],

packages/replay/src/coreHandlers/handleScope.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { CONSOLE_ARG_MAX_SIZE } from '../constants';
55
import type { ReplayContainer } from '../types';
66
import type { ReplayFrame } from '../types/replayFrame';
77
import { createBreadcrumb } from '../util/createBreadcrumb';
8-
import { fixJson } from '../util/truncateJson/fixJson';
98
import { addBreadcrumbEvent } from './util/addBreadcrumbEvent';
109

1110
let _LAST_BREADCRUMB: null | Breadcrumb = null;
@@ -95,11 +94,9 @@ export function normalizeConsoleBreadcrumb(
9594
const normalizedArg = normalize(arg, 7);
9695
const stringified = JSON.stringify(normalizedArg);
9796
if (stringified.length > CONSOLE_ARG_MAX_SIZE) {
98-
const fixedJson = fixJson(stringified.slice(0, CONSOLE_ARG_MAX_SIZE));
99-
const json = JSON.parse(fixedJson);
100-
// We only set this after JSON.parse() was successfull, so we know we didn't run into `catch`
10197
isTruncated = true;
102-
return json;
98+
// We use the pretty printed JSON string here as a base
99+
return `${JSON.stringify(normalizedArg, null, 2).slice(0, CONSOLE_ARG_MAX_SIZE)}…`;
103100
}
104101
return normalizedArg;
105102
} catch {

packages/replay/src/coreHandlers/util/networkUtils.ts

+25-15
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import type {
1010
ReplayNetworkRequestOrResponse,
1111
ReplayPerformanceEntry,
1212
} from '../../types';
13-
import { fixJson } from '../../util/truncateJson/fixJson';
1413

1514
/** Get the size of a body. */
1615
export function getBodySize(
@@ -161,7 +160,7 @@ export function buildNetworkRequestOrResponse(
161160

162161
const { body: normalizedBody, warnings } = normalizeNetworkBody(body);
163162
info.body = normalizedBody;
164-
if (warnings.length > 0) {
163+
if (warnings && warnings.length > 0) {
165164
info._meta = {
166165
warnings,
167166
};
@@ -191,36 +190,47 @@ function _serializeFormData(formData: FormData): string {
191190

192191
function normalizeNetworkBody(body: string | undefined): {
193192
body: NetworkBody | undefined;
194-
warnings: NetworkMetaWarning[];
193+
warnings?: NetworkMetaWarning[];
195194
} {
196195
if (!body || typeof body !== 'string') {
197196
return {
198197
body,
199-
warnings: [],
200198
};
201199
}
202200

203201
const exceedsSizeLimit = body.length > NETWORK_BODY_MAX_SIZE;
204202

205-
if (_strIsProbablyJson(body)) {
206-
try {
207-
const json = exceedsSizeLimit ? fixJson(body.slice(0, NETWORK_BODY_MAX_SIZE)) : body;
208-
const normalizedBody = JSON.parse(json);
203+
const isProbablyJson = _strIsProbablyJson(body);
204+
205+
if (exceedsSizeLimit) {
206+
const truncatedBody = body.slice(0, NETWORK_BODY_MAX_SIZE);
207+
208+
if (isProbablyJson) {
209209
return {
210-
body: normalizedBody,
211-
warnings: exceedsSizeLimit ? ['JSON_TRUNCATED'] : [],
210+
body: truncatedBody,
211+
warnings: ['MAYBE_JSON_TRUNCATED'],
212212
};
213-
} catch {
213+
}
214+
215+
return {
216+
body: `${truncatedBody}…`,
217+
warnings: ['TEXT_TRUNCATED'],
218+
};
219+
}
220+
221+
if (isProbablyJson) {
222+
try {
223+
const jsonBody = JSON.parse(body);
214224
return {
215-
body: exceedsSizeLimit ? `${body.slice(0, NETWORK_BODY_MAX_SIZE)}…` : body,
216-
warnings: exceedsSizeLimit ? ['INVALID_JSON', 'TEXT_TRUNCATED'] : ['INVALID_JSON'],
225+
body: jsonBody,
217226
};
227+
} catch {
228+
// fall back to just send the body as string
218229
}
219230
}
220231

221232
return {
222-
body: exceedsSizeLimit ? `${body.slice(0, NETWORK_BODY_MAX_SIZE)}…` : body,
223-
warnings: exceedsSizeLimit ? ['TEXT_TRUNCATED'] : [],
233+
body,
224234
};
225235
}
226236

packages/replay/src/types/request.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ type JsonArray = unknown[];
33

44
export type NetworkBody = JsonObject | JsonArray | string;
55

6-
export type NetworkMetaWarning = 'JSON_TRUNCATED' | 'TEXT_TRUNCATED' | 'INVALID_JSON' | 'URL_SKIPPED';
6+
export type NetworkMetaWarning = 'MAYBE_JSON_TRUNCATED' | 'TEXT_TRUNCATED' | 'URL_SKIPPED';
77

88
interface NetworkMeta {
99
warnings?: NetworkMetaWarning[];

packages/replay/src/util/truncateJson/completeJson.ts

-125
This file was deleted.

packages/replay/src/util/truncateJson/constants.ts

-23
This file was deleted.

0 commit comments

Comments
 (0)