Skip to content

Commit e7ff725

Browse files
AbhiPrasadLms24
authored andcommitted
feat: Switch to new transports (#4943)
This PR switches over from the old transports to the new ones. It deletes the functionality from the client of passing in transports explicitly and instead expects the transport to be passed in through options. Instead of passing in an instance of a transport to the client, we pass in a constructor function. This allows the client to control exactly what options are passed into the transport. ```ts this._transport = options.transport({ ...options.transportOptions, url }); ```
1 parent b5ad491 commit e7ff725

File tree

136 files changed

+932
-1494
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

136 files changed

+932
-1494
lines changed

MIGRATION.md

-6
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,6 @@ For our efforts to reduce bundle size of the SDK we had to remove and refactor p
115115
[#4919](https://github.com/getsentry/sentry-javascript/pull/4919)). `Backend` was an unnecessary abstraction which is
116116
not present in other Sentry SDKs. For the sake of reducing complexity, increasing consistency with other Sentry SDKs and
117117
decreasing bundle-size, `Backend` was removed.
118-
<!-- TODO(v7): Add more info and PR link for passing transports in options once this is done -->
119-
<!-- TODO(v7): This needs refinement once NewTransport is the default (maybe this should get its own section with an expamp) -->
120-
- Inject transport into client instead of initializing it in the client in `setupTransport` (see
121-
[#4921](https://github.com/getsentry/sentry-javascript/pull/4921/)). If you are creating your own `Client` or
122-
calling `initAndBind`, you will have to supply your desired transport. Either provide a custom one or call
123-
`setupBrowserTransport` or `setupNodeTransport` for default transports, depending on your requirements.
124118
- Remove support for Opera browser pre v15
125119

126120
## Sentry Angular SDK Changes

packages/browser/src/client.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { BaseClient, NewTransport, Scope, SDK_VERSION } from '@sentry/core';
2-
import { ClientOptions, Event, EventHint, Options, Severity, SeverityLevel, Transport } from '@sentry/types';
1+
import { BaseClient, Scope, SDK_VERSION } from '@sentry/core';
2+
import { ClientOptions, Event, EventHint, Options, Severity, SeverityLevel } from '@sentry/types';
33
import { getGlobalObject, logger } from '@sentry/utils';
44

55
import { eventFromException, eventFromMessage } from './eventbuilder';
@@ -47,7 +47,7 @@ export class BrowserClient extends BaseClient<BrowserClientOptions> {
4747
*
4848
* @param options Configuration options for this SDK.
4949
*/
50-
public constructor(options: BrowserClientOptions, transport: Transport, newTransport?: NewTransport) {
50+
public constructor(options: BrowserClientOptions) {
5151
options._metadata = options._metadata || {};
5252
options._metadata.sdk = options._metadata.sdk || {
5353
name: 'sentry.javascript.browser',
@@ -59,7 +59,7 @@ export class BrowserClient extends BaseClient<BrowserClientOptions> {
5959
],
6060
version: SDK_VERSION,
6161
};
62-
super(options, transport, newTransport);
62+
super(options);
6363
}
6464

6565
/**

packages/browser/src/exports.ts

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export {
2727
captureEvent,
2828
captureMessage,
2929
configureScope,
30+
createTransport,
3031
getHubFromCarrier,
3132
getCurrentHub,
3233
Hub,

packages/browser/src/sdk.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ import { IS_DEBUG_BUILD } from './flags';
1414
import { ReportDialogOptions, wrap as internalWrap } from './helpers';
1515
import { Breadcrumbs, Dedupe, GlobalHandlers, LinkedErrors, TryCatch, UserAgent } from './integrations';
1616
import { defaultStackParsers } from './stack-parsers';
17-
import { FetchTransport, XHRTransport } from './transports';
18-
import { setupBrowserTransport } from './transports/setup';
17+
import { makeNewFetchTransport, makeNewXHRTransport } from './transports';
1918

2019
export const defaultIntegrations = [
2120
new CoreIntegrations.InboundFilters(),
@@ -102,17 +101,15 @@ export function init(options: BrowserOptions = {}): void {
102101
if (options.sendClientReports === undefined) {
103102
options.sendClientReports = true;
104103
}
105-
const { transport, newTransport } = setupBrowserTransport(options);
106104

107105
const clientOptions: BrowserClientOptions = {
108106
...options,
109107
stackParser: stackParserFromOptions(options.stackParser || defaultStackParsers),
110108
integrations: getIntegrationsToSetup(options),
111-
// TODO(v7): get rid of transport being passed down below
112-
transport: options.transport || (supportsFetch() ? FetchTransport : XHRTransport),
109+
transport: options.transport || (supportsFetch() ? makeNewFetchTransport : makeNewXHRTransport),
113110
};
114111

115-
initAndBind(BrowserClient, clientOptions, transport, newTransport);
112+
initAndBind(BrowserClient, clientOptions);
116113

117114
if (options.autoSessionTracking) {
118115
startSessionTracking();

packages/browser/src/transports/index.ts

-2
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,3 @@ export { XHRTransport } from './xhr';
44

55
export { makeNewFetchTransport } from './new-fetch';
66
export { makeNewXHRTransport } from './new-xhr';
7-
8-
export { setupBrowserTransport } from './setup';

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

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
import {
2-
BaseTransportOptions,
3-
createTransport,
4-
NewTransport,
5-
TransportMakeRequestResponse,
6-
TransportRequest,
7-
} from '@sentry/core';
1+
import { createTransport } from '@sentry/core';
2+
import { BaseTransportOptions, NewTransport, TransportMakeRequestResponse, TransportRequest } from '@sentry/types';
83

94
import { FetchImpl, getNativeFetchImplementation } from './utils';
105

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

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
import {
2-
BaseTransportOptions,
3-
createTransport,
4-
NewTransport,
5-
TransportMakeRequestResponse,
6-
TransportRequest,
7-
} from '@sentry/core';
1+
import { createTransport } from '@sentry/core';
2+
import { BaseTransportOptions, NewTransport, TransportMakeRequestResponse, TransportRequest } from '@sentry/types';
83
import { SyncPromise } from '@sentry/utils';
94

105
/**

packages/browser/src/transports/setup.ts

-71
This file was deleted.

packages/browser/test/unit/helper/browser-client-options.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { NoopTransport } from '@sentry/core';
1+
import { createTransport } from '@sentry/core';
2+
import { resolvedSyncPromise } from '@sentry/utils';
23

34
import { BrowserClientOptions } from '../../../src/client';
45

56
export function getDefaultBrowserClientOptions(options: Partial<BrowserClientOptions> = {}): BrowserClientOptions {
67
return {
78
integrations: [],
8-
transport: NoopTransport,
9+
transport: () => createTransport({}, _ => resolvedSyncPromise({ statusCode: 200 })),
910
stackParser: () => [],
1011
...options,
1112
};

packages/browser/test/unit/index.test.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
wrap,
1818
} from '../../src';
1919
import { getDefaultBrowserClientOptions } from './helper/browser-client-options';
20-
import { SimpleTransport } from './mocks/simpletransport';
20+
import { makeSimpleTransport } from './mocks/simpletransport';
2121

2222
const dsn = 'https://[email protected]/4291';
2323

@@ -31,7 +31,7 @@ describe('SentryBrowser', () => {
3131
init({
3232
beforeSend,
3333
dsn,
34-
transport: SimpleTransport,
34+
transport: makeSimpleTransport,
3535
});
3636
});
3737

@@ -77,7 +77,7 @@ describe('SentryBrowser', () => {
7777
describe('user', () => {
7878
const EX_USER = { email: '[email protected]' };
7979
const options = getDefaultBrowserClientOptions({ dsn });
80-
const client = new BrowserClient(options, new SimpleTransport({ dsn }));
80+
const client = new BrowserClient(options);
8181
const reportDialogSpy = jest.spyOn(client, 'showReportDialog');
8282

8383
beforeEach(() => {
@@ -150,7 +150,7 @@ describe('SentryBrowser', () => {
150150
},
151151
dsn,
152152
});
153-
getCurrentHub().bindClient(new BrowserClient(options, new SimpleTransport({ dsn })));
153+
getCurrentHub().bindClient(new BrowserClient(options));
154154
captureMessage('test');
155155
});
156156

@@ -164,7 +164,7 @@ describe('SentryBrowser', () => {
164164
},
165165
dsn,
166166
});
167-
getCurrentHub().bindClient(new BrowserClient(options, new SimpleTransport({ dsn })));
167+
getCurrentHub().bindClient(new BrowserClient(options));
168168
captureEvent({ message: 'event' });
169169
});
170170

@@ -175,7 +175,7 @@ describe('SentryBrowser', () => {
175175
dsn,
176176
integrations: [],
177177
});
178-
getCurrentHub().bindClient(new BrowserClient(options, new SimpleTransport({ dsn })));
178+
getCurrentHub().bindClient(new BrowserClient(options));
179179

180180
captureMessage('event222');
181181
captureMessage('event222');
@@ -192,7 +192,7 @@ describe('SentryBrowser', () => {
192192
dsn,
193193
integrations: [new Integrations.InboundFilters({ ignoreErrors: ['capture'] })],
194194
});
195-
getCurrentHub().bindClient(new BrowserClient(options, new SimpleTransport({ dsn })));
195+
getCurrentHub().bindClient(new BrowserClient(options));
196196

197197
captureMessage('capture');
198198

@@ -244,7 +244,7 @@ describe('SentryBrowser initialization', () => {
244244
it('should set SDK data when Sentry.init() is called', () => {
245245
init({ dsn });
246246

247-
const sdkData = (getCurrentHub().getClient() as any).getTransport()._api.metadata?.sdk;
247+
const sdkData = (getCurrentHub().getClient() as any).getOptions()._metadata.sdk;
248248

249249
expect(sdkData?.name).toBe('sentry.javascript.browser');
250250
expect(sdkData?.packages[0].name).toBe('npm:@sentry/browser');
@@ -254,9 +254,9 @@ describe('SentryBrowser initialization', () => {
254254

255255
it('should set SDK data when instantiating a client directly', () => {
256256
const options = getDefaultBrowserClientOptions({ dsn });
257-
const client = new BrowserClient(options, new SimpleTransport({ dsn }));
257+
const client = new BrowserClient(options);
258258

259-
const sdkData = (client.getTransport() as any)._api.metadata?.sdk;
259+
const sdkData = client.getOptions()._metadata?.sdk as any;
260260

261261
expect(sdkData.name).toBe('sentry.javascript.browser');
262262
expect(sdkData.packages[0].name).toBe('npm:@sentry/browser');
@@ -284,7 +284,7 @@ describe('SentryBrowser initialization', () => {
284284
},
285285
});
286286

287-
const sdkData = (getCurrentHub().getClient() as any).getTransport()._api.metadata?.sdk;
287+
const sdkData = (getCurrentHub().getClient() as any).getOptions()._metadata?.sdk;
288288

289289
expect(sdkData.name).toBe('sentry.javascript.angular');
290290
expect(sdkData.packages[0].name).toBe('npm:@sentry/angular');
@@ -305,7 +305,7 @@ describe('wrap()', () => {
305305
},
306306
dsn,
307307
});
308-
getCurrentHub().bindClient(new BrowserClient(options, new SimpleTransport({ dsn })));
308+
getCurrentHub().bindClient(new BrowserClient(options));
309309

310310
try {
311311
wrap(() => {

packages/browser/test/unit/integrations/linkederrors.test.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { createStackParser } from '@sentry/utils';
44
import { BrowserClient } from '../../../src/client';
55
import * as LinkedErrorsModule from '../../../src/integrations/linkederrors';
66
import { defaultStackParsers } from '../../../src/stack-parsers';
7-
import { setupBrowserTransport } from '../../../src/transports';
87
import { getDefaultBrowserClientOptions } from '../helper/browser-client-options';
98

109
const parser = createStackParser(...defaultStackParsers);
@@ -47,7 +46,7 @@ describe('LinkedErrors', () => {
4746

4847
const originalException = one;
4948
const options = getDefaultBrowserClientOptions({ stackParser: parser });
50-
const client = new BrowserClient(options, setupBrowserTransport(options).transport);
49+
const client = new BrowserClient(options);
5150
return client.eventFromException(originalException).then(event => {
5251
const result = LinkedErrorsModule._handler(parser, 'cause', 5, event, {
5352
originalException,
@@ -78,7 +77,7 @@ describe('LinkedErrors', () => {
7877

7978
const originalException = one;
8079
const options = getDefaultBrowserClientOptions({ stackParser: parser });
81-
const client = new BrowserClient(options, setupBrowserTransport(options).transport);
80+
const client = new BrowserClient(options);
8281
return client.eventFromException(originalException).then(event => {
8382
const result = LinkedErrorsModule._handler(parser, 'reason', 5, event, {
8483
originalException,
@@ -105,7 +104,7 @@ describe('LinkedErrors', () => {
105104
two.cause = three;
106105

107106
const options = getDefaultBrowserClientOptions({ stackParser: parser });
108-
const client = new BrowserClient(options, setupBrowserTransport(options).transport);
107+
const client = new BrowserClient(options);
109108
const originalException = one;
110109
return client.eventFromException(originalException).then(event => {
111110
const result = LinkedErrorsModule._handler(parser, 'cause', 2, event, {
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
1-
import { eventStatusFromHttpCode, resolvedSyncPromise } from '@sentry/utils';
1+
import { createTransport } from '@sentry/core';
2+
import { resolvedSyncPromise } from '@sentry/utils';
23

3-
import { Event, Response } from '../../../src';
4-
import { BaseTransport } from '../../../src/transports';
5-
6-
// @ts-ignore It's okay that we're not implementing the `_sendRequest()` method because we don't use it in our tests
7-
export class SimpleTransport extends BaseTransport {
8-
public sendEvent(_: Event): PromiseLike<Response> {
9-
return this._buffer.add(() =>
10-
resolvedSyncPromise({
11-
status: eventStatusFromHttpCode(200),
12-
}),
13-
);
14-
}
4+
export function makeSimpleTransport() {
5+
return createTransport({}, () => resolvedSyncPromise({ statusCode: 200 }));
156
}

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
/* eslint-disable @typescript-eslint/unbound-method */
2-
import { NoopTransport, Scope } from '@sentry/core';
2+
import { Scope } from '@sentry/core';
3+
import { createTransport } from '@sentry/core';
34
import { MockIntegration } from '@sentry/core/test/lib/sdk.test';
45
import { Client, Integration } from '@sentry/types';
6+
import { resolvedSyncPromise } from '@sentry/utils';
57

68
import { BrowserOptions } from '../../src';
79
import { init } from '../../src/sdk';
@@ -13,7 +15,7 @@ const PUBLIC_DSN = 'https://username@domain/123';
1315
function getDefaultBrowserOptions(options: Partial<BrowserOptions> = {}): BrowserOptions {
1416
return {
1517
integrations: [],
16-
transport: NoopTransport,
18+
transport: () => createTransport({}, _ => resolvedSyncPromise({ statusCode: 200 })),
1719
stackParser: () => [],
1820
...options,
1921
};

packages/browser/test/unit/transports/base.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ const envelopeEndpoint = 'https://sentry.io/api/42/envelope/?sentry_key=123&sent
77
// assert on what the class provides and what it leaves to the concrete class to implement
88
class SimpleTransport extends BaseTransport {}
99

10-
describe('BaseTransport', () => {
10+
// TODO(v7): Re-enable these tests with client reports
11+
describe.skip('BaseTransport', () => {
1112
describe('Client Reports', () => {
1213
const sendBeaconSpy = jest.fn();
1314
let visibilityState: string;

0 commit comments

Comments
 (0)