Skip to content

Commit 84d889f

Browse files
committed
feat: Stop passing defaultIntegrations as client option
1 parent 4de9878 commit 84d889f

File tree

61 files changed

+450
-308
lines changed

Some content is hidden

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

61 files changed

+450
-308
lines changed

dev-packages/e2e-tests/test-applications/node-exports-test-app/scripts/consistentExports.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const NODE_EXPORTS_IGNORE = [
1818
'setNodeAsyncContextStrategy',
1919
'getDefaultIntegrationsWithoutPerformance',
2020
'initWithoutDefaultIntegrations',
21+
'initWithDefaultIntegrations',
2122
'SentryContextManager',
2223
'validateOpenTelemetrySetup',
2324
'preloadOpenTelemetry',

packages/angular/src/sdk.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
browserSessionIntegration,
66
globalHandlersIntegration,
77
httpContextIntegration,
8-
init as browserInit,
8+
initWithDefaultIntegrations,
99
linkedErrorsIntegration,
1010
setContext,
1111
} from '@sentry/browser';
@@ -50,14 +50,14 @@ export function getDefaultIntegrations(_options: BrowserOptions = {}): Integrati
5050
*/
5151
export function init(options: BrowserOptions): Client | undefined {
5252
const opts = {
53-
defaultIntegrations: getDefaultIntegrations(),
5453
...options,
5554
};
5655

5756
applySdkMetadata(opts, 'angular');
5857

5958
checkAndSetAngularVersion();
60-
return browserInit(opts);
59+
60+
return initWithDefaultIntegrations(opts, getDefaultIntegrations);
6161
}
6262

6363
function checkAndSetAngularVersion(): void {

packages/astro/src/client/sdk.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { BrowserOptions } from '@sentry/browser';
22
import {
33
browserTracingIntegration,
44
getDefaultIntegrations as getBrowserDefaultIntegrations,
5-
init as initBrowserSdk,
5+
initWithDefaultIntegrations,
66
} from '@sentry/browser';
77
import type { Client, Integration } from '@sentry/core';
88
import { applySdkMetadata } from '@sentry/core';
@@ -17,13 +17,12 @@ declare const __SENTRY_TRACING__: boolean;
1717
*/
1818
export function init(options: BrowserOptions): Client | undefined {
1919
const opts = {
20-
defaultIntegrations: getDefaultIntegrations(options),
2120
...options,
2221
};
2322

2423
applySdkMetadata(opts, 'astro', ['astro', 'browser']);
2524

26-
return initBrowserSdk(opts);
25+
return initWithDefaultIntegrations(opts, getDefaultIntegrations);
2726
}
2827

2928
function getDefaultIntegrations(options: BrowserOptions): Integration[] {

packages/astro/src/index.types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ export * from '@sentry/node';
1515

1616
/** Initializes Sentry Astro SDK */
1717
export declare function init(options: Options | clientSdk.BrowserOptions | NodeOptions): Client | undefined;
18+
export declare function initWithDefaultIntegrations(
19+
options: Options | clientSdk.BrowserOptions | NodeOptions,
20+
getDefaultIntegrations: (options: Options) => Integration[],
21+
): Client | undefined;
1822

1923
export declare const linkedErrorsIntegration: typeof clientSdk.linkedErrorsIntegration;
2024
export declare const contextLinesIntegration: typeof clientSdk.contextLinesIntegration;

packages/astro/test/client/sdk.test.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import type { BrowserClient } from '@sentry/browser';
21
import {
32
browserTracingIntegration,
43
getActiveSpan,
5-
getClient,
64
getCurrentScope,
75
getGlobalScope,
86
getIsolationScope,
@@ -12,7 +10,7 @@ import * as SentryBrowser from '@sentry/browser';
1210
import { afterEach, describe, expect, it, vi } from 'vitest';
1311
import { init } from '../../src/client/sdk';
1412

15-
const browserInit = vi.spyOn(SentryBrowser, 'init');
13+
const browserInit = vi.spyOn(SentryBrowser, 'initWithDefaultIntegrations');
1614

1715
describe('Sentry client SDK', () => {
1816
describe('init', () => {
@@ -44,6 +42,7 @@ describe('Sentry client SDK', () => {
4442
},
4543
},
4644
}),
45+
expect.any(Function),
4746
);
4847
});
4948

@@ -53,45 +52,39 @@ describe('Sentry client SDK', () => {
5352
['tracesSampler', { tracesSampler: () => 1.0 }],
5453
['no tracing option set', {}],
5554
])('adds browserTracingIntegration if tracing is enabled via %s', (_, tracingOptions) => {
56-
init({
55+
const client = init({
5756
dsn: 'https://[email protected]/1337',
5857
...tracingOptions,
5958
});
6059

61-
const integrationsToInit = browserInit.mock.calls[0]![0]?.defaultIntegrations;
62-
const browserTracing = getClient<BrowserClient>()?.getIntegrationByName('BrowserTracing');
63-
64-
expect(integrationsToInit).toContainEqual(expect.objectContaining({ name: 'BrowserTracing' }));
60+
const browserTracing = client?.getIntegrationByName('BrowserTracing');
6561
expect(browserTracing).toBeDefined();
6662
});
6763

6864
it("doesn't add browserTracingIntegration if `__SENTRY_TRACING__` is set to false", () => {
6965
(globalThis as any).__SENTRY_TRACING__ = false;
7066

71-
init({
67+
const client = init({
7268
dsn: 'https://[email protected]/1337',
7369
tracesSampleRate: 1,
7470
});
7571

76-
const integrationsToInit = browserInit.mock.calls[0]![0]?.defaultIntegrations || [];
77-
const browserTracing = getClient<BrowserClient>()?.getIntegrationByName('BrowserTracing');
78-
79-
expect(integrationsToInit).not.toContainEqual(expect.objectContaining({ name: 'BrowserTracing' }));
72+
const browserTracing = client?.getIntegrationByName('BrowserTracing');
8073
expect(browserTracing).toBeUndefined();
8174

8275
delete (globalThis as any).__SENTRY_TRACING__;
8376
});
8477

8578
it('Overrides the automatically default browserTracingIntegration instance with a a user-provided browserTracingIntegration instance', () => {
86-
init({
79+
const client = init({
8780
dsn: 'https://[email protected]/1337',
8881
integrations: [
8982
browserTracingIntegration({ finalTimeout: 10, instrumentNavigation: false, instrumentPageLoad: false }),
9083
],
9184
tracesSampleRate: 1,
9285
});
9386

94-
const browserTracing = getClient<BrowserClient>()?.getIntegrationByName('BrowserTracing');
87+
const browserTracing = client?.getIntegrationByName('BrowserTracing');
9588
expect(browserTracing).toBeDefined();
9689

9790
// no active span means the settings were respected

packages/aws-serverless/src/sdk.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
flush,
1414
getCurrentScope,
1515
getDefaultIntegrationsWithoutPerformance,
16-
initWithoutDefaultIntegrations,
16+
initWithDefaultIntegrations,
1717
startSpanManual,
1818
withScope,
1919
} from '@sentry/node';
@@ -77,13 +77,12 @@ export function getDefaultIntegrations(_options: Options): Integration[] {
7777
*/
7878
export function init(options: NodeOptions = {}): NodeClient | undefined {
7979
const opts = {
80-
defaultIntegrations: getDefaultIntegrations(options),
8180
...options,
8281
};
8382

8483
applySdkMetadata(opts, 'aws-serverless');
8584

86-
return initWithoutDefaultIntegrations(opts);
85+
return initWithDefaultIntegrations(opts, getDefaultIntegrations);
8786
}
8887

8988
/** */

packages/aws-serverless/test/sdk.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ vi.mock('@sentry/node', async () => {
2525
const original = (await vi.importActual('@sentry/node')) as typeof import('@sentry/node');
2626
return {
2727
...original,
28-
initWithoutDefaultIntegrations: (options: unknown) => {
28+
initWithDefaultIntegrations: (options: unknown) => {
2929
mockInit(options);
3030
},
3131
startInactiveSpan: (...args: unknown[]) => {

packages/browser/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ export * from './exports';
66

77
export { logger };
88

9+
export { initWithDefaultIntegrations } from './sdk';
10+
911
export { reportingObserverIntegration } from './integrations/reportingobserver';
1012
export { httpClientIntegration } from './integrations/httpclient';
1113
export { contextLinesIntegration } from './integrations/contextlines';

packages/browser/src/sdk.ts

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,8 @@ export function getDefaultIntegrations(_options: Options): Integration[] {
6060
}
6161

6262
/** Exported only for tests. */
63-
export function applyDefaultOptions(optionsArg: BrowserOptions = {}): BrowserOptions {
63+
export function applyDefaultOptions(optionsArg: BrowserOptions): BrowserOptions {
6464
const defaultOptions: BrowserOptions = {
65-
defaultIntegrations: getDefaultIntegrations(optionsArg),
6665
release:
6766
typeof __SENTRY_RELEASE__ === 'string' // This allows build tooling to find-and-replace __SENTRY_RELEASE__ to inject a release value
6867
? __SENTRY_RELEASE__
@@ -72,27 +71,10 @@ export function applyDefaultOptions(optionsArg: BrowserOptions = {}): BrowserOpt
7271

7372
return {
7473
...defaultOptions,
75-
...dropTopLevelUndefinedKeys(optionsArg),
74+
...optionsArg,
7675
};
7776
}
7877

79-
/**
80-
* In contrast to the regular `dropUndefinedKeys` method,
81-
* this one does not deep-drop keys, but only on the top level.
82-
*/
83-
function dropTopLevelUndefinedKeys<T extends object>(obj: T): Partial<T> {
84-
const mutatetedObj: Partial<T> = {};
85-
86-
for (const k of Object.getOwnPropertyNames(obj)) {
87-
const key = k as keyof T;
88-
if (obj[key] !== undefined) {
89-
mutatetedObj[key] = obj[key];
90-
}
91-
}
92-
93-
return mutatetedObj;
94-
}
95-
9678
/**
9779
* The Sentry Browser SDK Client.
9880
*
@@ -143,15 +125,34 @@ export function init(browserOptions: BrowserOptions = {}): Client | undefined {
143125
if (!browserOptions.skipBrowserExtensionCheck && _checkForBrowserExtension()) {
144126
return;
145127
}
128+
return _init(browserOptions, getDefaultIntegrations(browserOptions));
129+
}
146130

147-
const options = applyDefaultOptions(browserOptions);
148-
const clientOptions: BrowserClientOptions = {
149-
...options,
150-
stackParser: stackParserFromStackParserOptions(options.stackParser || defaultStackParser),
151-
integrations: getIntegrationsToSetup(options),
152-
transport: options.transport || makeFetchTransport,
153-
};
131+
/**
132+
* Initialize a browser client with the provided options and default integrations getter function.
133+
* This is an internal method the SDK uses under the hood to set up things - you should not use this as a user!
134+
* Instead, use `init()` to initialize the SDK.
135+
*
136+
* @hidden
137+
* @internal
138+
*/
139+
export function initWithDefaultIntegrations(
140+
browserOptions: BrowserOptions = {},
141+
getDefaultIntegrationsImpl: (options: BrowserOptions) => Integration[],
142+
): BrowserClient | undefined {
143+
if (!browserOptions.skipBrowserExtensionCheck && _checkForBrowserExtension()) {
144+
return;
145+
}
154146

147+
return _init(browserOptions, getDefaultIntegrationsImpl(browserOptions));
148+
}
149+
150+
/**
151+
* Acutal implementation shared by init and initWithDefaultIntegrations.
152+
*/
153+
function _init(browserOptions: BrowserOptions = {}, defaultIntegrations: Integration[]): BrowserClient {
154+
const options = applyDefaultOptions(browserOptions);
155+
const clientOptions = getClientOptions(options, defaultIntegrations);
155156
return initAndBind(BrowserClient, clientOptions);
156157
}
157158

@@ -215,3 +216,12 @@ function _checkForBrowserExtension(): true | void {
215216
return true;
216217
}
217218
}
219+
220+
function getClientOptions(options: BrowserOptions, defaultIntegrations: Integration[]): BrowserClientOptions {
221+
return {
222+
...options,
223+
stackParser: stackParserFromStackParserOptions(options.stackParser || defaultStackParser),
224+
integrations: getIntegrationsToSetup(options, defaultIntegrations),
225+
transport: options.transport || makeFetchTransport,
226+
};
227+
}

0 commit comments

Comments
 (0)