Skip to content

Commit 22156f6

Browse files
committed
ref: Delete store endpoint code (#4969)
1 parent ea095e4 commit 22156f6

File tree

7 files changed

+130
-537
lines changed

7 files changed

+130
-537
lines changed

packages/core/src/api.ts

+3-44
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ function getBaseApiEndpoint(dsn: DsnComponents): string {
3535
}
3636

3737
/** Returns the ingest API endpoint for target. */
38-
function _getIngestEndpoint(dsn: DsnComponents, target: 'store' | 'envelope'): string {
39-
return `${getBaseApiEndpoint(dsn)}${dsn.projectId}/${target}/`;
38+
function _getIngestEndpoint(dsn: DsnComponents): string {
39+
return `${getBaseApiEndpoint(dsn)}${dsn.projectId}/envelope/`;
4040
}
4141

4242
/** Returns a URL-encoded string with auth config suitable for a query string. */
@@ -49,54 +49,13 @@ function _encodedAuth(dsn: DsnComponents): string {
4949
});
5050
}
5151

52-
/** Returns the store endpoint URL. */
53-
export function getStoreEndpoint(dsn: DsnComponents): string {
54-
return _getIngestEndpoint(dsn, 'store');
55-
}
56-
57-
/**
58-
* Returns the store endpoint URL with auth in the query string.
59-
*
60-
* Sending auth as part of the query string and not as custom HTTP headers avoids CORS preflight requests.
61-
*/
62-
export function getStoreEndpointWithUrlEncodedAuth(dsn: DsnComponents): string {
63-
return `${getStoreEndpoint(dsn)}?${_encodedAuth(dsn)}`;
64-
}
65-
66-
/** Returns the envelope endpoint URL. */
67-
function _getEnvelopeEndpoint(dsn: DsnComponents): string {
68-
return _getIngestEndpoint(dsn, 'envelope');
69-
}
70-
7152
/**
7253
* Returns the envelope endpoint URL with auth in the query string.
7354
*
7455
* Sending auth as part of the query string and not as custom HTTP headers avoids CORS preflight requests.
7556
*/
7657
export function getEnvelopeEndpointWithUrlEncodedAuth(dsn: DsnComponents, tunnel?: string): string {
77-
return tunnel ? tunnel : `${_getEnvelopeEndpoint(dsn)}?${_encodedAuth(dsn)}`;
78-
}
79-
80-
/**
81-
* Returns an object that can be used in request headers.
82-
* This is needed for node and the old /store endpoint in sentry
83-
*/
84-
export function getRequestHeaders(
85-
dsn: DsnComponents,
86-
clientName: string,
87-
clientVersion: string,
88-
): { [key: string]: string } {
89-
// CHANGE THIS to use metadata but keep clientName and clientVersion compatible
90-
const header = [`Sentry sentry_version=${SENTRY_API_VERSION}`];
91-
header.push(`sentry_client=${clientName}/${clientVersion}`);
92-
header.push(`sentry_key=${dsn.publicKey}`);
93-
if (dsn.pass) {
94-
header.push(`sentry_secret=${dsn.pass}`);
95-
}
96-
return {
97-
'Content-Type': 'application/json',
98-
'X-Sentry-Auth': header.join(', '),
99-
};
58+
return tunnel ? tunnel : `${_getIngestEndpoint(dsn)}?${_encodedAuth(dsn)}`;
10059
}
10160

10261
/** Returns the url to the report dialog endpoint. */

packages/core/src/baseclient.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ import {
3232
} from '@sentry/utils';
3333

3434
import { getEnvelopeEndpointWithUrlEncodedAuth } from './api';
35+
import { createEventEnvelope, createSessionEnvelope } from './envelope';
3536
import { IS_DEBUG_BUILD } from './flags';
3637
import { IntegrationIndex, setupIntegrations } from './integration';
37-
import { createEventEnvelope, createSessionEnvelope } from './request';
3838

3939
const ALREADY_SEEN_ERROR = "Not capturing exception because it's already been captured.";
4040

@@ -275,7 +275,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
275275
*/
276276
public sendSession(session: Session | SessionAggregates): void {
277277
if (this._dsn) {
278-
const [env] = createSessionEnvelope(session, this._dsn, this._options._metadata, this._options.tunnel);
278+
const env = createSessionEnvelope(session, this._dsn, this._options._metadata, this._options.tunnel);
279279
this.sendEnvelope(env);
280280
}
281281
}

packages/core/src/envelope.ts

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import {
2+
DsnComponents,
3+
Event,
4+
EventEnvelope,
5+
EventItem,
6+
SdkInfo,
7+
SdkMetadata,
8+
SentryRequestType,
9+
Session,
10+
SessionAggregates,
11+
SessionEnvelope,
12+
SessionItem,
13+
} from '@sentry/types';
14+
import { createEnvelope, dsnToString } from '@sentry/utils';
15+
16+
/** Extract sdk info from from the API metadata */
17+
function getSdkMetadataForEnvelopeHeader(metadata?: SdkMetadata): SdkInfo | undefined {
18+
if (!metadata || !metadata.sdk) {
19+
return;
20+
}
21+
const { name, version } = metadata.sdk;
22+
return { name, version };
23+
}
24+
25+
/**
26+
* Apply SdkInfo (name, version, packages, integrations) to the corresponding event key.
27+
* Merge with existing data if any.
28+
**/
29+
function enhanceEventWithSdkInfo(event: Event, sdkInfo?: SdkInfo): Event {
30+
if (!sdkInfo) {
31+
return event;
32+
}
33+
event.sdk = event.sdk || {};
34+
event.sdk.name = event.sdk.name || sdkInfo.name;
35+
event.sdk.version = event.sdk.version || sdkInfo.version;
36+
event.sdk.integrations = [...(event.sdk.integrations || []), ...(sdkInfo.integrations || [])];
37+
event.sdk.packages = [...(event.sdk.packages || []), ...(sdkInfo.packages || [])];
38+
return event;
39+
}
40+
41+
/** Creates an envelope from a Session */
42+
export function createSessionEnvelope(
43+
session: Session | SessionAggregates,
44+
dsn: DsnComponents,
45+
metadata?: SdkMetadata,
46+
tunnel?: string,
47+
): SessionEnvelope {
48+
const sdkInfo = getSdkMetadataForEnvelopeHeader(metadata);
49+
const envelopeHeaders = {
50+
sent_at: new Date().toISOString(),
51+
...(sdkInfo && { sdk: sdkInfo }),
52+
...(!!tunnel && { dsn: dsnToString(dsn) }),
53+
};
54+
55+
// I know this is hacky but we don't want to add `sessions` to request type since it's never rate limited
56+
const type = 'aggregates' in session ? ('sessions' as SentryRequestType) : 'session';
57+
58+
// TODO (v7) Have to cast type because envelope items do not accept a `SentryRequestType`
59+
const envelopeItem = [{ type } as { type: 'session' | 'sessions' }, session] as SessionItem;
60+
const envelope = createEnvelope<SessionEnvelope>(envelopeHeaders, [envelopeItem]);
61+
62+
return envelope;
63+
}
64+
65+
/**
66+
* Create an Envelope from an event.
67+
*/
68+
export function createEventEnvelope(
69+
event: Event,
70+
dsn: DsnComponents,
71+
metadata?: SdkMetadata,
72+
tunnel?: string,
73+
): EventEnvelope {
74+
const sdkInfo = getSdkMetadataForEnvelopeHeader(metadata);
75+
const eventType = event.type || 'event';
76+
77+
const { transactionSampling } = event.sdkProcessingMetadata || {};
78+
const { method: samplingMethod, rate: sampleRate } = transactionSampling || {};
79+
80+
// TODO: Below is a temporary hack in order to debug a serialization error - see
81+
// https://github.com/getsentry/sentry-javascript/issues/2809,
82+
// https://github.com/getsentry/sentry-javascript/pull/4425, and
83+
// https://github.com/getsentry/sentry-javascript/pull/4574.
84+
//
85+
// TL; DR: even though we normalize all events (which should prevent this), something is causing `JSON.stringify` to
86+
// throw a circular reference error.
87+
//
88+
// When it's time to remove it:
89+
// 1. Delete everything between here and where the request object `req` is created, EXCEPT the line deleting
90+
// `sdkProcessingMetadata`
91+
// 2. Restore the original version of the request body, which is commented out
92+
// 3. Search for either of the PR URLs above and pull out the companion hacks in the browser playwright tests and the
93+
// baseClient tests in this package
94+
enhanceEventWithSdkInfo(event, metadata && metadata.sdk);
95+
event.tags = event.tags || {};
96+
event.extra = event.extra || {};
97+
98+
// In theory, all events should be marked as having gone through normalization and so
99+
// we should never set this tag/extra data
100+
if (!(event.sdkProcessingMetadata && event.sdkProcessingMetadata.baseClientNormalized)) {
101+
event.tags.skippedNormalization = true;
102+
event.extra.normalizeDepth = event.sdkProcessingMetadata ? event.sdkProcessingMetadata.normalizeDepth : 'unset';
103+
}
104+
105+
// prevent this data from being sent to sentry
106+
// TODO: This is NOT part of the hack - DO NOT DELETE
107+
delete event.sdkProcessingMetadata;
108+
109+
const envelopeHeaders = {
110+
event_id: event.event_id as string,
111+
sent_at: new Date().toISOString(),
112+
...(sdkInfo && { sdk: sdkInfo }),
113+
...(!!tunnel && { dsn: dsnToString(dsn) }),
114+
};
115+
const eventItem: EventItem = [
116+
{
117+
type: eventType,
118+
sample_rates: [{ id: samplingMethod, rate: sampleRate }],
119+
},
120+
event,
121+
];
122+
return createEnvelope<EventEnvelope>(envelopeHeaders, [eventItem]);
123+
}

packages/core/src/index.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,8 @@ export {
2323
Scope,
2424
Session,
2525
} from '@sentry/hub';
26-
export {
27-
getEnvelopeEndpointWithUrlEncodedAuth,
28-
getStoreEndpointWithUrlEncodedAuth,
29-
getRequestHeaders,
30-
initAPIDetails,
31-
getReportDialogEndpoint,
32-
} from './api';
26+
export { getEnvelopeEndpointWithUrlEncodedAuth, initAPIDetails, getReportDialogEndpoint } from './api';
3327
export { BaseClient } from './baseclient';
34-
export { eventToSentryRequest, sessionToSentryRequest } from './request';
3528
export { initAndBind } from './sdk';
3629
export { createTransport } from './transports/base';
3730
export { SDK_VERSION } from './version';

0 commit comments

Comments
 (0)