|
1 | 1 | import { Event, SdkInfo, SentryRequest, SentryRequestType, Session, SessionAggregates } from '@sentry/types';
|
2 |
| -import { dsnToString } from '@sentry/utils'; |
| 2 | +import { dsnToString, normalize } from '@sentry/utils'; |
3 | 3 |
|
4 | 4 | import { APIDetails, getEnvelopeEndpointWithUrlEncodedAuth, getStoreEndpointWithUrlEncodedAuth } from './api';
|
5 | 5 |
|
@@ -58,11 +58,58 @@ export function eventToSentryRequest(event: Event, api: APIDetails): SentryReque
|
58 | 58 | const { transactionSampling } = event.sdkProcessingMetadata || {};
|
59 | 59 | const { method: samplingMethod, rate: sampleRate } = transactionSampling || {};
|
60 | 60 |
|
| 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 | + |
| 75 | + // In theory, all events should be marked as having gone through normalization and so |
| 76 | + // we should never set this tag |
| 77 | + if (!(event.sdkProcessingMetadata && event.sdkProcessingMetadata.baseClientNormalized)) { |
| 78 | + event.tags.skippedNormalization = true; |
| 79 | + } |
| 80 | + |
61 | 81 | // prevent this data from being sent to sentry
|
| 82 | + // TODO: This is NOT part of the hack - DO NOT DELETE |
62 | 83 | delete event.sdkProcessingMetadata;
|
63 | 84 |
|
| 85 | + let body; |
| 86 | + try { |
| 87 | + // 99.9% of events should get through just fine - no change in behavior for them |
| 88 | + body = JSON.stringify(event); |
| 89 | + } catch (err) { |
| 90 | + // Record data about the error without replacing original event data, then force renormalization |
| 91 | + event.tags.JSONStringifyError = true; |
| 92 | + event.extra.JSONStringifyError = err; |
| 93 | + try { |
| 94 | + body = JSON.stringify(normalize(event)); |
| 95 | + } catch (newErr) { |
| 96 | + // At this point even renormalization hasn't worked, meaning something about the event data has gone very wrong. |
| 97 | + // Time to cut our losses and record only the new error. With luck, even in the problematic cases we're trying to |
| 98 | + // debug with this hack, we won't ever land here. |
| 99 | + const innerErr = newErr as Error; |
| 100 | + body = JSON.stringify({ |
| 101 | + message: 'JSON.stringify error after renormalization', |
| 102 | + // setting `extra: { innerErr }` here for some reason results in an empty object, so unpack manually |
| 103 | + extra: { message: innerErr.message, stack: innerErr.stack }, |
| 104 | + }); |
| 105 | + } |
| 106 | + } |
| 107 | + |
64 | 108 | const req: SentryRequest = {
|
65 |
| - body: JSON.stringify(sdkInfo ? enhanceEventWithSdkInfo(event, api.metadata.sdk) : event), |
| 109 | + // this is the relevant line of code before the hack was added, to make it easy to undo said hack once we've solved |
| 110 | + // the mystery |
| 111 | + // body: JSON.stringify(sdkInfo ? enhanceEventWithSdkInfo(event, api.metadata.sdk) : event), |
| 112 | + body, |
66 | 113 | type: eventType,
|
67 | 114 | url: useEnvelope
|
68 | 115 | ? getEnvelopeEndpointWithUrlEncodedAuth(api.dsn, api.tunnel)
|
|
0 commit comments