Skip to content

Commit fcdb458

Browse files
authored
ref(core): Use helpers with session envelopes (#4655)
This patch converts the sessions logic in `packages/core/src/request.ts` to use the recently introduced envelope helpers. ref: #4587
1 parent fb5d4e6 commit fcdb458

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

packages/core/src/request.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {
77
SentryRequestType,
88
Session,
99
SessionAggregates,
10+
SessionEnvelope,
11+
SessionItem,
1012
} from '@sentry/types';
1113
import { createEnvelope, dsnToString, normalize, serializeEnvelope } from '@sentry/utils';
1214

@@ -40,19 +42,20 @@ function enhanceEventWithSdkInfo(event: Event, sdkInfo?: SdkInfo): Event {
4042
/** Creates a SentryRequest from a Session. */
4143
export function sessionToSentryRequest(session: Session | SessionAggregates, api: APIDetails): SentryRequest {
4244
const sdkInfo = getSdkMetadataForEnvelopeHeader(api);
43-
const envelopeHeaders = JSON.stringify({
45+
const envelopeHeaders = {
4446
sent_at: new Date().toISOString(),
4547
...(sdkInfo && { sdk: sdkInfo }),
4648
...(!!api.tunnel && { dsn: dsnToString(api.dsn) }),
47-
});
48-
// I know this is hacky but we don't want to add `session` to request type since it's never rate limited
49-
const type: SentryRequestType = 'aggregates' in session ? ('sessions' as SentryRequestType) : 'session';
50-
const itemHeaders = JSON.stringify({
51-
type,
52-
});
49+
};
50+
51+
// I know this is hacky but we don't want to add `sessions` to request type since it's never rate limited
52+
const type = 'aggregates' in session ? ('sessions' as SentryRequestType) : 'session';
5353

54+
// TODO (v7) Have to cast type because envelope items do not accept a `SentryRequestType`
55+
const envelopeItem = [{ type } as { type: 'session' | 'sessions' }, session] as SessionItem;
56+
const envelope = createEnvelope<SessionEnvelope>(envelopeHeaders, [envelopeItem]);
5457
return {
55-
body: `${envelopeHeaders}\n${itemHeaders}\n${JSON.stringify(session)}`,
58+
body: serializeEnvelope(envelope),
5659
type,
5760
url: getEnvelopeEndpointWithUrlEncodedAuth(api.dsn, api.tunnel),
5861
};

0 commit comments

Comments
 (0)