@@ -5,16 +5,13 @@ import {
55 EventItem ,
66 SdkInfo ,
77 SdkMetadata ,
8- SentryRequest ,
98 SentryRequestType ,
109 Session ,
1110 SessionAggregates ,
1211 SessionEnvelope ,
1312 SessionItem ,
1413} from '@sentry/types' ;
15- import { createEnvelope , dsnToString , normalize , serializeEnvelope } from '@sentry/utils' ;
16-
17- import { APIDetails , getEnvelopeEndpointWithUrlEncodedAuth , getStoreEndpointWithUrlEncodedAuth } from './api' ;
14+ import { createEnvelope , dsnToString } from '@sentry/utils' ;
1815
1916/** Extract sdk info from from the API metadata */
2017function getSdkMetadataForEnvelopeHeader ( metadata ?: SdkMetadata ) : SdkInfo | undefined {
@@ -47,7 +44,7 @@ export function createSessionEnvelope(
4744 dsn : DsnComponents ,
4845 metadata ?: SdkMetadata ,
4946 tunnel ?: string ,
50- ) : [ SessionEnvelope , SentryRequestType ] {
47+ ) : SessionEnvelope {
5148 const sdkInfo = getSdkMetadataForEnvelopeHeader ( metadata ) ;
5249 const envelopeHeaders = {
5350 sent_at : new Date ( ) . toISOString ( ) ,
@@ -62,22 +59,11 @@ export function createSessionEnvelope(
6259 const envelopeItem = [ { type } as { type : 'session' | 'sessions' } , session ] as SessionItem ;
6360 const envelope = createEnvelope < SessionEnvelope > ( envelopeHeaders , [ envelopeItem ] ) ;
6461
65- return [ envelope , type ] ;
66- }
67-
68- /** Creates a SentryRequest from a Session. */
69- export function sessionToSentryRequest ( session : Session | SessionAggregates , api : APIDetails ) : SentryRequest {
70- const [ envelope , type ] = createSessionEnvelope ( session , api . dsn , api . metadata , api . tunnel ) ;
71- return {
72- body : serializeEnvelope ( envelope ) ,
73- type,
74- url : getEnvelopeEndpointWithUrlEncodedAuth ( api . dsn , api . tunnel ) ,
75- } ;
62+ return envelope ;
7663}
7764
7865/**
79- * Create an Envelope from an event. Note that this is duplicated from below,
80- * but on purpose as this will be refactored in v7.
66+ * Create an Envelope from an event.
8167 */
8268export function createEventEnvelope (
8369 event : Event ,
@@ -135,102 +121,3 @@ export function createEventEnvelope(
135121 ] ;
136122 return createEnvelope < EventEnvelope > ( envelopeHeaders , [ eventItem ] ) ;
137123}
138-
139- /** Creates a SentryRequest from an event. */
140- export function eventToSentryRequest ( event : Event , api : APIDetails ) : SentryRequest {
141- const sdkInfo = getSdkMetadataForEnvelopeHeader ( api . metadata ) ;
142- const eventType = event . type || 'event' ;
143- const useEnvelope = eventType === 'transaction' || ! ! api . tunnel ;
144-
145- const { transactionSampling } = event . sdkProcessingMetadata || { } ;
146- const { method : samplingMethod , rate : sampleRate } = transactionSampling || { } ;
147-
148- // TODO: Below is a temporary hack in order to debug a serialization error - see
149- // https://github.com/getsentry/sentry-javascript/issues/2809,
150- // https://github.com/getsentry/sentry-javascript/pull/4425, and
151- // https://github.com/getsentry/sentry-javascript/pull/4574.
152- //
153- // TL; DR: even though we normalize all events (which should prevent this), something is causing `JSON.stringify` to
154- // throw a circular reference error.
155- //
156- // When it's time to remove it:
157- // 1. Delete everything between here and where the request object `req` is created, EXCEPT the line deleting
158- // `sdkProcessingMetadata`
159- // 2. Restore the original version of the request body, which is commented out
160- // 3. Search for either of the PR URLs above and pull out the companion hacks in the browser playwright tests and the
161- // baseClient tests in this package
162- enhanceEventWithSdkInfo ( event , api . metadata . sdk ) ;
163- event . tags = event . tags || { } ;
164- event . extra = event . extra || { } ;
165-
166- // In theory, all events should be marked as having gone through normalization and so
167- // we should never set this tag/extra data
168- if ( ! ( event . sdkProcessingMetadata && event . sdkProcessingMetadata . baseClientNormalized ) ) {
169- event . tags . skippedNormalization = true ;
170- event . extra . normalizeDepth = event . sdkProcessingMetadata ? event . sdkProcessingMetadata . normalizeDepth : 'unset' ;
171- }
172-
173- // prevent this data from being sent to sentry
174- // TODO: This is NOT part of the hack - DO NOT DELETE
175- delete event . sdkProcessingMetadata ;
176-
177- let body ;
178- try {
179- // 99.9% of events should get through just fine - no change in behavior for them
180- body = JSON . stringify ( event ) ;
181- } catch ( err ) {
182- // Record data about the error without replacing original event data, then force renormalization
183- event . tags . JSONStringifyError = true ;
184- event . extra . JSONStringifyError = err ;
185- try {
186- body = JSON . stringify ( normalize ( event ) ) ;
187- } catch ( newErr ) {
188- // At this point even renormalization hasn't worked, meaning something about the event data has gone very wrong.
189- // Time to cut our losses and record only the new error. With luck, even in the problematic cases we're trying to
190- // debug with this hack, we won't ever land here.
191- const innerErr = newErr as Error ;
192- body = JSON . stringify ( {
193- message : 'JSON.stringify error after renormalization' ,
194- // setting `extra: { innerErr }` here for some reason results in an empty object, so unpack manually
195- extra : { message : innerErr . message , stack : innerErr . stack } ,
196- } ) ;
197- }
198- }
199-
200- const req : SentryRequest = {
201- // this is the relevant line of code before the hack was added, to make it easy to undo said hack once we've solved
202- // the mystery
203- // body: JSON.stringify(sdkInfo ? enhanceEventWithSdkInfo(event, api.metadata.sdk) : event),
204- body,
205- type : eventType ,
206- url : useEnvelope
207- ? getEnvelopeEndpointWithUrlEncodedAuth ( api . dsn , api . tunnel )
208- : getStoreEndpointWithUrlEncodedAuth ( api . dsn ) ,
209- } ;
210-
211- // https://develop.sentry.dev/sdk/envelopes/
212-
213- // Since we don't need to manipulate envelopes nor store them, there is no
214- // exported concept of an Envelope with operations including serialization and
215- // deserialization. Instead, we only implement a minimal subset of the spec to
216- // serialize events inline here.
217- if ( useEnvelope ) {
218- const envelopeHeaders = {
219- event_id : event . event_id as string ,
220- sent_at : new Date ( ) . toISOString ( ) ,
221- ...( sdkInfo && { sdk : sdkInfo } ) ,
222- ...( ! ! api . tunnel && { dsn : dsnToString ( api . dsn ) } ) ,
223- } ;
224- const eventItem : EventItem = [
225- {
226- type : eventType ,
227- sample_rates : [ { id : samplingMethod , rate : sampleRate } ] ,
228- } ,
229- req . body ,
230- ] ;
231- const envelope = createEnvelope < EventEnvelope > ( envelopeHeaders , [ eventItem ] ) ;
232- req . body = serializeEnvelope ( envelope ) ;
233- }
234-
235- return req ;
236- }
0 commit comments