Skip to content

Commit 00d359d

Browse files
authored
ref(core): Log normalizeDepth when normalization is skipped (#4574)
Continuing debugging of #2809. In the most recent example event there[1], it's clear from the stringifying error's stacktrace that the event is hitting `BaseClient._processEvent`, and we can tell from the line number in that stack trace that it's getting past `_prepareEvent`, which is what calls `normalize`. In `_prepareEvent`, the only way for normalization not to run is if the `normalizeDepth` option either isn't a number or is set to a value <= 0.[2] (It's got a default value[3], so it not being set directly by the user isn't the issue.) In order to better diagnose what the problem might be, this PR adds logging of the `normalizeDepth` value in cases where an event fails to be normalized when it should. [1] #2809 (comment) [2] https://github.com/getsentry/sentry-javascript/blob/b46674c6382be48bd6cafafa44b6e1a6b4661610/packages/core/src/baseclient.ts#L377-L379 [3] https://github.com/getsentry/sentry-javascript/blob/b46674c6382be48bd6cafafa44b6e1a6b4661610/packages/core/src/baseclient.ts#L349
1 parent b46674c commit 00d359d

File tree

4 files changed

+42
-6
lines changed

4 files changed

+42
-6
lines changed

packages/core/src/baseclient.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,11 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
374374
}
375375

376376
return result.then(evt => {
377+
if (evt) {
378+
// TODO this is more of the hack trying to solve https://github.com/getsentry/sentry-javascript/issues/2809
379+
// it is only attached as extra data to the event if the event somehow skips being normalized
380+
evt.sdkProcessingMetadata = { ...evt.sdkProcessingMetadata, normalizeDepth: normalize(normalizeDepth) };
381+
}
377382
if (typeof normalizeDepth === 'number' && normalizeDepth > 0) {
378383
return this._normalizeEvent(evt, normalizeDepth);
379384
}

packages/core/src/request.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,28 @@ export function eventToSentryRequest(event: Event, api: APIDetails): SentryReque
5959
const { method: samplingMethod, rate: sampleRate } = transactionSampling || {};
6060

6161
// 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.
62+
// https://github.com/getsentry/sentry-javascript/issues/2809,
63+
// https://github.com/getsentry/sentry-javascript/pull/4425, and
64+
// https://github.com/getsentry/sentry-javascript/pull/4574.
65+
//
66+
// TL; DR: even though we normalize all events (which should prevent this), something is causing `JSON.stringify` to
67+
// throw a circular reference error.
6568
//
6669
// When it's time to remove it:
6770
// 1. Delete everything between here and where the request object `req` is created, EXCEPT the line deleting
6871
// `sdkProcessingMetadata`
6972
// 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
73+
// 3. Search for either of the PR URLs above and pull out the companion hacks in the browser playwright tests and the
74+
// baseClient tests in this package
7175
enhanceEventWithSdkInfo(event, api.metadata.sdk);
7276
event.tags = event.tags || {};
7377
event.extra = event.extra || {};
7478

7579
// In theory, all events should be marked as having gone through normalization and so
76-
// we should never set this tag
80+
// we should never set this tag/extra data
7781
if (!(event.sdkProcessingMetadata && event.sdkProcessingMetadata.baseClientNormalized)) {
7882
event.tags.skippedNormalization = true;
83+
event.extra.normalizeDepth = event.sdkProcessingMetadata ? event.sdkProcessingMetadata.normalizeDepth : 'unset';
7984
}
8085

8186
// prevent this data from being sent to sentry

packages/core/test/lib/base.test.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,21 @@ describe('BaseClient', () => {
867867

868868
client.captureEvent(transaction);
869869

870-
expect(TestBackend.instance!.event!).toEqual(normalizedTransaction);
870+
// TODO: This is to compensate for a temporary debugging hack which adds data the tests aren't anticipating to the
871+
// event. The code can be restored to its original form (the commented-out line below) once that hack is
872+
// removed. See https://github.com/getsentry/sentry-javascript/pull/4425 and
873+
// https://github.com/getsentry/sentry-javascript/pull/4574
874+
const capturedEvent = TestBackend.instance!.event!;
875+
if (capturedEvent.sdkProcessingMetadata?.normalizeDepth) {
876+
if (Object.keys(capturedEvent.sdkProcessingMetadata).length === 1) {
877+
delete capturedEvent.sdkProcessingMetadata;
878+
} else {
879+
delete capturedEvent.sdkProcessingMetadata.normalizeDepth;
880+
}
881+
}
882+
883+
expect(capturedEvent).toEqual(normalizedTransaction);
884+
// expect(TestBackend.instance!.event!).toEqual(normalizedTransaction);
871885
});
872886

873887
test('calls beforeSend and uses original event without any changes', () => {

packages/integration-tests/suites/public-api/configureScope/clear_scope/test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ sentryTest('should clear previously set properties of a scope', async ({ getLoca
88

99
const eventData = await getSentryRequest(page, url);
1010

11+
// TODO: This is to compensate for a temporary debugging hack which adds data the tests aren't anticipating to the
12+
// event. The code can be restored to its original form (the commented-out line below) once that hack is
13+
// removed. See https://github.com/getsentry/sentry-javascript/pull/4425 and
14+
// https://github.com/getsentry/sentry-javascript/pull/4574
15+
if (eventData.extra) {
16+
if (Object.keys(eventData.extra).length === 1) {
17+
delete eventData.extra;
18+
} else {
19+
delete eventData.extra.normalizeDepth;
20+
}
21+
}
22+
1123
expect(eventData.message).toBe('cleared_scope');
1224
expect(eventData.user).toBeUndefined();
1325
expect(eventData.tags).toBeUndefined();

0 commit comments

Comments
 (0)