|
1 |
| -import { Event, SdkInfo, SentryRequest, SentryRequestType, Session, SessionAggregates } from '@sentry/types'; |
2 |
| -import { dsnToString, normalize } from '@sentry/utils'; |
| 1 | +import { |
| 2 | + Event, |
| 3 | + EventEnvelope, |
| 4 | + EventItem, |
| 5 | + SdkInfo, |
| 6 | + SentryRequest, |
| 7 | + SentryRequestType, |
| 8 | + Session, |
| 9 | + SessionAggregates, |
| 10 | +} from '@sentry/types'; |
| 11 | +import { createEnvelope, dsnToString, normalize, serializeEnvelope } from '@sentry/utils'; |
3 | 12 |
|
4 | 13 | import { APIDetails, getEnvelopeEndpointWithUrlEncodedAuth, getStoreEndpointWithUrlEncodedAuth } from './api';
|
5 | 14 |
|
@@ -128,39 +137,21 @@ export function eventToSentryRequest(event: Event, api: APIDetails): SentryReque
|
128 | 137 | // deserialization. Instead, we only implement a minimal subset of the spec to
|
129 | 138 | // serialize events inline here.
|
130 | 139 | if (useEnvelope) {
|
131 |
| - const envelopeHeaders = JSON.stringify({ |
132 |
| - event_id: event.event_id, |
| 140 | + const envelopeHeaders = { |
| 141 | + event_id: event.event_id as string, |
133 | 142 | sent_at: new Date().toISOString(),
|
134 | 143 | ...(sdkInfo && { sdk: sdkInfo }),
|
135 | 144 | ...(!!api.tunnel && { dsn: dsnToString(api.dsn) }),
|
136 |
| - }); |
137 |
| - const itemHeaders = JSON.stringify({ |
138 |
| - type: eventType, |
139 |
| - |
140 |
| - // TODO: Right now, sampleRate may or may not be defined (it won't be in the cases of inheritance and |
141 |
| - // explicitly-set sampling decisions). Are we good with that? |
142 |
| - sample_rates: [{ id: samplingMethod, rate: sampleRate }], |
143 |
| - |
144 |
| - // The content-type is assumed to be 'application/json' and not part of |
145 |
| - // the current spec for transaction items, so we don't bloat the request |
146 |
| - // body with it. |
147 |
| - // |
148 |
| - // content_type: 'application/json', |
149 |
| - // |
150 |
| - // The length is optional. It must be the number of bytes in req.Body |
151 |
| - // encoded as UTF-8. Since the server can figure this out and would |
152 |
| - // otherwise refuse events that report the length incorrectly, we decided |
153 |
| - // not to send the length to avoid problems related to reporting the wrong |
154 |
| - // size and to reduce request body size. |
155 |
| - // |
156 |
| - // length: new TextEncoder().encode(req.body).length, |
157 |
| - }); |
158 |
| - // The trailing newline is optional. We intentionally don't send it to avoid |
159 |
| - // sending unnecessary bytes. |
160 |
| - // |
161 |
| - // const envelope = `${envelopeHeaders}\n${itemHeaders}\n${req.body}\n`; |
162 |
| - const envelope = `${envelopeHeaders}\n${itemHeaders}\n${req.body}`; |
163 |
| - req.body = envelope; |
| 145 | + }; |
| 146 | + const eventItem: EventItem = [ |
| 147 | + { |
| 148 | + type: eventType, |
| 149 | + sample_rates: [{ id: samplingMethod, rate: sampleRate }], |
| 150 | + }, |
| 151 | + req.body, |
| 152 | + ]; |
| 153 | + const envelope = createEnvelope<EventEnvelope>(envelopeHeaders, [eventItem]); |
| 154 | + req.body = serializeEnvelope(envelope); |
164 | 155 | }
|
165 | 156 |
|
166 | 157 | return req;
|
|
0 commit comments