Skip to content

Commit e003307

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

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

packages/core/src/request.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1-
import { Event, SdkInfo, SentryRequest, SentryRequestType, Session, SessionAggregates } from '@sentry/types';
2-
import { dsnToString, normalize } from '@sentry/utils';
1+
import {
2+
Event,
3+
SdkInfo,
4+
SentryRequest,
5+
SentryRequestType,
6+
Session,
7+
SessionAggregates,
8+
SessionEnvelope,
9+
SessionItem,
10+
} from '@sentry/types';
11+
import { createEnvelope, dsnToString, normalize, serializeEnvelope } from '@sentry/utils';
312

413
import { APIDetails, getEnvelopeEndpointWithUrlEncodedAuth, getStoreEndpointWithUrlEncodedAuth } from './api';
514

@@ -31,19 +40,20 @@ function enhanceEventWithSdkInfo(event: Event, sdkInfo?: SdkInfo): Event {
3140
/** Creates a SentryRequest from a Session. */
3241
export function sessionToSentryRequest(session: Session | SessionAggregates, api: APIDetails): SentryRequest {
3342
const sdkInfo = getSdkMetadataForEnvelopeHeader(api);
34-
const envelopeHeaders = JSON.stringify({
43+
const envelopeHeaders = {
3544
sent_at: new Date().toISOString(),
3645
...(sdkInfo && { sdk: sdkInfo }),
3746
...(!!api.tunnel && { dsn: dsnToString(api.dsn) }),
38-
});
39-
// I know this is hacky but we don't want to add `session` to request type since it's never rate limited
40-
const type: SentryRequestType = 'aggregates' in session ? ('sessions' as SentryRequestType) : 'session';
41-
const itemHeaders = JSON.stringify({
42-
type,
43-
});
47+
};
48+
49+
// I know this is hacky but we don't want to add `sessions` to request type since it's never rate limited
50+
const type = 'aggregates' in session ? ('sessions' as SentryRequestType) : 'session';
4451

52+
// Have to cast type because envelope items do not accept a `SentryRequestType`
53+
const envelopeItem = [{ type } as { type: 'session' | 'sessions' }, session] as SessionItem;
54+
const envelope = createEnvelope<SessionEnvelope>(envelopeHeaders, [envelopeItem]);
4555
return {
46-
body: `${envelopeHeaders}\n${itemHeaders}\n${JSON.stringify(session)}`,
56+
body: serializeEnvelope(envelope),
4757
type,
4858
url: getEnvelopeEndpointWithUrlEncodedAuth(api.dsn, api.tunnel),
4959
};

0 commit comments

Comments
 (0)