Skip to content

Commit db373c9

Browse files
committed
renormalize only if stringifying goes wrong
1 parent fcd97c4 commit db373c9

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

packages/core/src/baseclient.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -428,10 +428,6 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
428428
normalized.contexts.trace = event.contexts.trace;
429429
}
430430

431-
const { _experiments = {} } = this.getOptions();
432-
if (_experiments.ensureNoCircularStructures) {
433-
return normalize(normalized);
434-
}
435431

436432
return normalized;
437433
}

packages/core/src/request.ts

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Event, SdkInfo, SentryRequest, SentryRequestType, Session, SessionAggregates } from '@sentry/types';
2-
import { dsnToString } from '@sentry/utils';
2+
import { dsnToString, normalize } from '@sentry/utils';
33

44
import { APIDetails, getEnvelopeEndpointWithUrlEncodedAuth, getStoreEndpointWithUrlEncodedAuth } from './api';
55

@@ -58,11 +58,52 @@ export function eventToSentryRequest(event: Event, api: APIDetails): SentryReque
5858
const { transactionSampling } = event.sdkProcessingMetadata || {};
5959
const { method: samplingMethod, rate: sampleRate } = transactionSampling || {};
6060

61+
// TODO: Below is a temporary hack in order to debug a serialization error - see
62+
// https://github.com/getsentry/sentry-javascript/issues/2809 and
63+
// https://github.com/getsentry/sentry-javascript/pull/4425. TL;DR: even though we normalize all events (which should
64+
// prevent this), something is causing `JSON.stringify` to throw a circular reference error.
65+
//
66+
// When it's time to remove it:
67+
// 1. Delete everything between here and where the request object `req` is created, EXCEPT the line deleting
68+
// `sdkProcessingMetadata`
69+
// 2. Restore the original version of the request body, which is commented out
70+
// 3. Search for `skippedNormalization` and pull out the companion hack in the browser playwright tests
71+
enhanceEventWithSdkInfo(event, api.metadata.sdk);
72+
event.tags = event.tags || {};
73+
event.extra = event.extra || {};
74+
6175
// prevent this data from being sent to sentry
76+
// TODO: This is NOT part of the hack - DO NOT DELETE
6277
delete event.sdkProcessingMetadata;
6378

79+
let body;
80+
try {
81+
// 99.9% of events should get through just fine - no change in behavior for them
82+
body = JSON.stringify(event);
83+
} catch (err) {
84+
// Record data about the error without replacing original event data, then force renormalization
85+
event.tags.JSONStringifyError = true;
86+
event.extra.JSONStringifyError = err;
87+
try {
88+
body = JSON.stringify(normalize(event));
89+
} catch (newErr) {
90+
// At this point even renormalization hasn't worked, meaning something about the event data has gone very wrong.
91+
// Time to cut our losses and record only the new error. With luck, even in the problematic cases we're trying to
92+
// debug with this hack, we won't ever land here.
93+
const innerErr = newErr as Error;
94+
body = JSON.stringify({
95+
message: 'JSON.stringify error after renormalization',
96+
// setting `extra: { innerErr }` here for some reason results in an empty object, so unpack manually
97+
extra: { message: innerErr.message, stack: innerErr.stack },
98+
});
99+
}
100+
}
101+
64102
const req: SentryRequest = {
65-
body: JSON.stringify(sdkInfo ? enhanceEventWithSdkInfo(event, api.metadata.sdk) : event),
103+
// this is the relevant line of code before the hack was added, to make it easy to undo said hack once we've solved
104+
// the mystery
105+
// body: JSON.stringify(sdkInfo ? enhanceEventWithSdkInfo(event, api.metadata.sdk) : event),
106+
body,
66107
type: eventType,
67108
url: useEnvelope
68109
? getEnvelopeEndpointWithUrlEncodedAuth(api.dsn, api.tunnel)

0 commit comments

Comments
 (0)