Skip to content

Commit d901a62

Browse files
committed
refactor api usage in client
1 parent e2cd850 commit d901a62

File tree

8 files changed

+64
-95
lines changed

8 files changed

+64
-95
lines changed

packages/browser/src/transports/new-fetch.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { createTransport } from '@sentry/core';
22
import { BaseTransportOptions, NewTransport, TransportMakeRequestResponse, TransportRequest } from '@sentry/types';
3-
import { resolvedSyncPromise } from '@sentry/utils';
43

54
import { FetchImpl, getNativeFetchImplementation } from './utils';
65

@@ -16,10 +15,6 @@ export function makeNewFetchTransport(
1615
nativeFetch: FetchImpl = getNativeFetchImplementation(),
1716
): NewTransport {
1817
function makeRequest(request: TransportRequest): PromiseLike<TransportMakeRequestResponse> {
19-
if (!options.url) {
20-
return resolvedSyncPromise({ statusCode: 400 });
21-
}
22-
2318
const requestOptions: RequestInit = {
2419
body: request.body,
2520
method: 'POST',

packages/browser/src/transports/new-xhr.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createTransport } from '@sentry/core';
22
import { BaseTransportOptions, NewTransport, TransportMakeRequestResponse, TransportRequest } from '@sentry/types';
3-
import { resolvedSyncPromise, SyncPromise } from '@sentry/utils';
3+
import { SyncPromise } from '@sentry/utils';
44

55
/**
66
* The DONE ready state for XmlHttpRequest
@@ -21,10 +21,6 @@ export interface XHRTransportOptions extends BaseTransportOptions {
2121
*/
2222
export function makeNewXHRTransport(options: XHRTransportOptions): NewTransport {
2323
function makeRequest(request: TransportRequest): PromiseLike<TransportMakeRequestResponse> {
24-
if (!options.url) {
25-
return resolvedSyncPromise({ statusCode: 400 });
26-
}
27-
2824
return new SyncPromise<TransportMakeRequestResponse>((resolve, _reject) => {
2925
const xhr = new XMLHttpRequest();
3026

packages/browser/test/unit/sdk.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { NoopTransport, Scope } from '@sentry/core';
1+
import { Scope } from '@sentry/core';
22
import { MockIntegration } from '@sentry/core/test/lib/sdk.test';
33
import { Client, Integration } from '@sentry/types';
44

@@ -12,7 +12,6 @@ const PUBLIC_DSN = 'https://username@domain/123';
1212
function getDefaultBrowserOptions(options: Partial<BrowserOptions> = {}): BrowserOptions {
1313
return {
1414
integrations: [],
15-
transport: NoopTransport,
1615
stackParser: () => [],
1716
...options,
1817
};

packages/core/src/baseclient.ts

Lines changed: 36 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
Client,
55
ClientOptions,
66
DsnComponents,
7+
Envelope,
78
Event,
89
EventHint,
910
Integration,
@@ -30,7 +31,7 @@ import {
3031
uuid4,
3132
} from '@sentry/utils';
3233

33-
import { getEnvelopeEndpointWithUrlEncodedAuth, initAPIDetails } from './api';
34+
import { getEnvelopeEndpointWithUrlEncodedAuth } from './api';
3435
import { IS_DEBUG_BUILD } from './flags';
3536
import { IntegrationIndex, setupIntegrations } from './integration';
3637
import { createEventEnvelope, createSessionEnvelope } from './request';
@@ -75,18 +76,14 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
7576
/** The client Dsn, if specified in options. Without this Dsn, the SDK will be disabled. */
7677
protected readonly _dsn?: DsnComponents;
7778

79+
protected readonly _transport?: NewTransport | undefined;
80+
7881
/** Array of used integrations. */
7982
protected _integrations: IntegrationIndex = {};
8083

8184
/** Number of calls being processed */
8285
protected _numProcessing: number = 0;
8386

84-
/** Cached transport used internally. */
85-
protected _transport: NewTransport;
86-
87-
/** New v7 Transport that is initialized alongside the old one */
88-
protected _newTransport?: NewTransport;
89-
9087
/**
9188
* Initializes this client instance.
9289
*
@@ -96,18 +93,13 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
9693
*/
9794
protected constructor(options: O) {
9895
this._options = options;
99-
100-
let url;
10196
if (options.dsn) {
102-
this._dsn = makeDsn(options.dsn);
103-
// TODO(v7): Figure out what to do with transport when dsn is not defined
104-
const api = initAPIDetails(this._dsn, options._metadata, options.tunnel);
105-
url = getEnvelopeEndpointWithUrlEncodedAuth(api.dsn, api.tunnel);
97+
const dsn = makeDsn(options.dsn);
98+
const url = getEnvelopeEndpointWithUrlEncodedAuth(dsn, options.tunnel);
99+
this._transport = options.transport({ ...options.transportOptions, url });
106100
} else {
107101
IS_DEBUG_BUILD && logger.warn('No DSN provided, client will not do anything.');
108102
}
109-
110-
this._transport = options.transport({ ...options.transportOptions, url });
111103
}
112104

113105
/**
@@ -217,19 +209,22 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
217209
/**
218210
* @inheritDoc
219211
*/
220-
public getTransport(): Transport {
212+
public getTransport(): NewTransport | undefined {
221213
return this._transport;
222214
}
223215

224216
/**
225217
* @inheritDoc
226218
*/
227219
public flush(timeout?: number): PromiseLike<boolean> {
228-
return this._isClientDoneProcessing(timeout).then(clientFinished => {
229-
return this.getTransport()
230-
.close(timeout)
231-
.then(transportFlushed => clientFinished && transportFlushed);
232-
});
220+
const transport = this._transport;
221+
if (transport) {
222+
return this._isClientDoneProcessing(timeout).then(clientFinished => {
223+
return transport.flush(timeout).then(transportFlushed => clientFinished && transportFlushed);
224+
});
225+
} else {
226+
return resolvedSyncPromise(true);
227+
}
233228
}
234229

235230
/**
@@ -267,50 +262,32 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
267262
* @inheritDoc
268263
*/
269264
public sendEvent(event: Event): void {
270-
// TODO(v7): Remove the if-else
271-
if (
272-
this._newTransport &&
273-
this._options.dsn &&
274-
this._options._experiments &&
275-
this._options._experiments.newTransport
276-
) {
277-
const api = initAPIDetails(this._options.dsn, this._options._metadata, this._options.tunnel);
278-
const env = createEventEnvelope(event, api);
279-
void this._newTransport.send(env).then(null, reason => {
280-
IS_DEBUG_BUILD && logger.error('Error while sending event:', reason);
281-
});
282-
} else {
283-
void this._transport.sendEvent(event).then(null, reason => {
284-
IS_DEBUG_BUILD && logger.error('Error while sending event:', reason);
285-
});
265+
if (this._dsn) {
266+
const env = createEventEnvelope(event, this._dsn, this._options._metadata, this._options.tunnel);
267+
this.sendEnvelope(env);
286268
}
287269
}
288270

289271
/**
290272
* @inheritDoc
291273
*/
292274
public sendSession(session: Session): void {
293-
if (!this._transport.sendSession) {
294-
IS_DEBUG_BUILD && logger.warn("Dropping session because custom transport doesn't implement sendSession");
295-
return;
275+
if (this._dsn) {
276+
const [env] = createSessionEnvelope(session, this._dsn, this._options._metadata, this._options.tunnel);
277+
this.sendEnvelope(env);
296278
}
279+
}
297280

298-
// TODO(v7): Remove the if-else
299-
if (
300-
this._newTransport &&
301-
this._options.dsn &&
302-
this._options._experiments &&
303-
this._options._experiments.newTransport
304-
) {
305-
const api = initAPIDetails(this._options.dsn, this._options._metadata, this._options.tunnel);
306-
const [env] = createSessionEnvelope(session, api);
307-
void this._newTransport.send(env).then(null, reason => {
308-
IS_DEBUG_BUILD && logger.error('Error while sending session:', reason);
281+
/**
282+
* @inheritdoc
283+
*/
284+
public sendEnvelope(env: Envelope): void {
285+
if (this._transport && this._dsn) {
286+
this._transport.send(env).then(null, reason => {
287+
IS_DEBUG_BUILD && logger.error('Error while sending event:', reason);
309288
});
310289
} else {
311-
void this._transport.sendSession(session).then(null, reason => {
312-
IS_DEBUG_BUILD && logger.error('Error while sending session:', reason);
313-
});
290+
IS_DEBUG_BUILD && logger.error('Transport disabled');
314291
}
315292
}
316293

@@ -590,15 +567,15 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
590567
protected _processEvent(event: Event, hint?: EventHint, scope?: Scope): PromiseLike<Event> {
591568
// eslint-disable-next-line @typescript-eslint/unbound-method
592569
const { beforeSend, sampleRate } = this.getOptions();
593-
const transport = this.getTransport();
594570

595571
type RecordLostEvent = NonNullable<Transport['recordLostEvent']>;
596572
type RecordLostEventParams = Parameters<RecordLostEvent>;
597573

598-
function recordLostEvent(outcome: RecordLostEventParams[0], category: RecordLostEventParams[1]): void {
599-
if (transport.recordLostEvent) {
600-
transport.recordLostEvent(outcome, category);
601-
}
574+
function recordLostEvent(_outcome: RecordLostEventParams[0], _category: RecordLostEventParams[1]): void {
575+
// no-op as new transports don't have client outcomes
576+
// if (transport.recordLostEvent) {
577+
// transport.recordLostEvent(outcome, category);
578+
// }
602579
}
603580

604581
if (!this._isEnabled()) {

packages/core/src/request.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import {
2+
DsnComponents,
23
Event,
34
EventEnvelope,
45
EventItem,
56
SdkInfo,
7+
SdkMetadata,
68
SentryRequest,
79
SentryRequestType,
810
Session,
@@ -15,11 +17,11 @@ import { createEnvelope, dsnToString, normalize, serializeEnvelope } from '@sent
1517
import { APIDetails, getEnvelopeEndpointWithUrlEncodedAuth, getStoreEndpointWithUrlEncodedAuth } from './api';
1618

1719
/** Extract sdk info from from the API metadata */
18-
function getSdkMetadataForEnvelopeHeader(api: APIDetails): SdkInfo | undefined {
19-
if (!api.metadata || !api.metadata.sdk) {
20+
function getSdkMetadataForEnvelopeHeader(metadata?: SdkMetadata): SdkInfo | undefined {
21+
if (!metadata || !metadata.sdk) {
2022
return;
2123
}
22-
const { name, version } = api.metadata.sdk;
24+
const { name, version } = metadata.sdk;
2325
return { name, version };
2426
}
2527

@@ -42,13 +44,15 @@ function enhanceEventWithSdkInfo(event: Event, sdkInfo?: SdkInfo): Event {
4244
/** Creates an envelope from a Session */
4345
export function createSessionEnvelope(
4446
session: Session | SessionAggregates,
45-
api: APIDetails,
47+
dsn: DsnComponents,
48+
metadata?: SdkMetadata,
49+
tunnel?: string,
4650
): [SessionEnvelope, SentryRequestType] {
47-
const sdkInfo = getSdkMetadataForEnvelopeHeader(api);
51+
const sdkInfo = getSdkMetadataForEnvelopeHeader(metadata);
4852
const envelopeHeaders = {
4953
sent_at: new Date().toISOString(),
5054
...(sdkInfo && { sdk: sdkInfo }),
51-
...(!!api.tunnel && { dsn: dsnToString(api.dsn) }),
55+
...(!!tunnel && { dsn: dsnToString(dsn) }),
5256
};
5357

5458
// I know this is hacky but we don't want to add `sessions` to request type since it's never rate limited
@@ -63,7 +67,7 @@ export function createSessionEnvelope(
6367

6468
/** Creates a SentryRequest from a Session. */
6569
export function sessionToSentryRequest(session: Session | SessionAggregates, api: APIDetails): SentryRequest {
66-
const [envelope, type] = createSessionEnvelope(session, api);
70+
const [envelope, type] = createSessionEnvelope(session, api.dsn, api.metadata, api.tunnel);
6771
return {
6872
body: serializeEnvelope(envelope),
6973
type,
@@ -75,8 +79,13 @@ export function sessionToSentryRequest(session: Session | SessionAggregates, api
7579
* Create an Envelope from an event. Note that this is duplicated from below,
7680
* but on purpose as this will be refactored in v7.
7781
*/
78-
export function createEventEnvelope(event: Event, api: APIDetails): EventEnvelope {
79-
const sdkInfo = getSdkMetadataForEnvelopeHeader(api);
82+
export function createEventEnvelope(
83+
event: Event,
84+
dsn: DsnComponents,
85+
metadata?: SdkMetadata,
86+
tunnel?: string,
87+
): EventEnvelope {
88+
const sdkInfo = getSdkMetadataForEnvelopeHeader(metadata);
8089
const eventType = event.type || 'event';
8190

8291
const { transactionSampling } = event.sdkProcessingMetadata || {};
@@ -96,7 +105,7 @@ export function createEventEnvelope(event: Event, api: APIDetails): EventEnvelop
96105
// 2. Restore the original version of the request body, which is commented out
97106
// 3. Search for either of the PR URLs above and pull out the companion hacks in the browser playwright tests and the
98107
// baseClient tests in this package
99-
enhanceEventWithSdkInfo(event, api.metadata.sdk);
108+
enhanceEventWithSdkInfo(event, metadata && metadata.sdk);
100109
event.tags = event.tags || {};
101110
event.extra = event.extra || {};
102111

@@ -115,7 +124,7 @@ export function createEventEnvelope(event: Event, api: APIDetails): EventEnvelop
115124
event_id: event.event_id as string,
116125
sent_at: new Date().toISOString(),
117126
...(sdkInfo && { sdk: sdkInfo }),
118-
...(!!api.tunnel && { dsn: dsnToString(api.dsn) }),
127+
...(!!tunnel && { dsn: dsnToString(dsn) }),
119128
};
120129
const eventItem: EventItem = [
121130
{
@@ -129,7 +138,7 @@ export function createEventEnvelope(event: Event, api: APIDetails): EventEnvelop
129138

130139
/** Creates a SentryRequest from an event. */
131140
export function eventToSentryRequest(event: Event, api: APIDetails): SentryRequest {
132-
const sdkInfo = getSdkMetadataForEnvelopeHeader(api);
141+
const sdkInfo = getSdkMetadataForEnvelopeHeader(api.metadata);
133142
const eventType = event.type || 'event';
134143
const useEnvelope = eventType === 'transaction' || !!api.tunnel;
135144

packages/node/src/transports/new.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,6 @@ export interface NodeTransportOptions extends BaseTransportOptions {
3535
* Creates a Transport that uses native the native 'http' and 'https' modules to send events to Sentry.
3636
*/
3737
export function makeNodeTransport(options: NodeTransportOptions): NewTransport {
38-
if (!options.url) {
39-
return {
40-
send: () => new Promise(() => undefined),
41-
flush: () => new Promise(() => true),
42-
};
43-
}
4438
const urlSegments = new URL(options.url);
4539
const isHttps = urlSegments.protocol === 'https:';
4640

@@ -60,8 +54,7 @@ export function makeNodeTransport(options: NodeTransportOptions): NewTransport {
6054
: new nativeHttpModule.Agent({ keepAlive: false, maxSockets: 30, timeout: 2000 });
6155

6256
const requestExecutor = createRequestExecutor(
63-
// have to explicitly cast because we check url above
64-
options as NodeTransportOptions & { url: string },
57+
options as NodeTransportOptions,
6558
options.httpModule ?? nativeHttpModule,
6659
agent,
6760
);
@@ -97,7 +90,7 @@ function applyNoProxyOption(transportUrlSegments: URL, proxy: string | undefined
9790
* Creates a RequestExecutor to be used with `createTransport`.
9891
*/
9992
function createRequestExecutor(
100-
options: NodeTransportOptions & { url: string },
93+
options: NodeTransportOptions,
10194
httpModule: HTTPModule,
10295
agent: http.Agent,
10396
): TransportRequestExecutor {

packages/types/src/client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { ClientOptions } from './options';
55
import { Scope } from './scope';
66
import { Session } from './session';
77
import { Severity, SeverityLevel } from './severity';
8-
import { Transport } from './transport';
8+
import { Transport, NewTransport } from './transport';
99

1010
/**
1111
* User-Facing Sentry SDK Client.
@@ -72,7 +72,7 @@ export interface Client<O extends ClientOptions = ClientOptions> {
7272
*
7373
* @returns The transport.
7474
*/
75-
getTransport?(): Transport;
75+
getTransport(): NewTransport | undefined;
7676

7777
/**
7878
* Flush the event queue and set the client to `enabled = false`. See {@link Client.flush}.

packages/types/src/transport.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export interface BaseTransportOptions extends InternalBaseTransportOptions {
4646
// transport does not care about dsn specific - client should take care of
4747
// parsing and figuring that out
4848
// If no DSN is configured, pass in an undefined url.
49-
url?: string;
49+
url: string;
5050
}
5151

5252
export interface NewTransport {

0 commit comments

Comments
 (0)