Skip to content

Commit 80e69e0

Browse files
committed
ref(astro): Streamline how we add browser tracing
1 parent 33d1cb0 commit 80e69e0

File tree

3 files changed

+24
-21
lines changed

3 files changed

+24
-21
lines changed

packages/astro/src/client/sdk.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import type { BrowserOptions } from '@sentry/browser';
2-
import { BrowserTracing, init as initBrowserSdk } from '@sentry/browser';
3-
import { getCurrentScope, hasTracingEnabled } from '@sentry/core';
4-
import { addOrUpdateIntegration } from '@sentry/utils';
2+
import {
3+
BrowserTracing,
4+
getDefaultIntegrations as getBrowserDefaultIntegrations,
5+
init as initBrowserSdk,
6+
setTag,
7+
} from '@sentry/browser';
8+
import { hasTracingEnabled } from '@sentry/core';
9+
import type { Integration } from '@sentry/types';
510

611
import { applySdkMetadata } from '../common/metadata';
712

@@ -14,27 +19,26 @@ declare const __SENTRY_TRACING__: boolean;
1419
* @param options Configuration options for the SDK.
1520
*/
1621
export function init(options: BrowserOptions): void {
17-
applySdkMetadata(options, ['astro', 'browser']);
22+
const opts = {
23+
defaultIntegrations: getDefaultIntegrations(options),
24+
...options,
25+
};
1826

19-
addClientIntegrations(options);
27+
applySdkMetadata(opts, ['astro', 'browser']);
2028

21-
initBrowserSdk(options);
29+
initBrowserSdk(opts);
2230

23-
getCurrentScope().setTag('runtime', 'browser');
31+
setTag('runtime', 'browser');
2432
}
2533

26-
function addClientIntegrations(options: BrowserOptions): void {
27-
let integrations = options.integrations || [];
28-
34+
function getDefaultIntegrations(options: BrowserOptions): Integration[] | undefined {
2935
// This evaluates to true unless __SENTRY_TRACING__ is text-replaced with "false",
3036
// in which case everything inside will get treeshaken away
3137
if (typeof __SENTRY_TRACING__ === 'undefined' || __SENTRY_TRACING__) {
3238
if (hasTracingEnabled(options)) {
33-
const defaultBrowserTracingIntegration = new BrowserTracing({});
34-
35-
integrations = addOrUpdateIntegration(defaultBrowserTracingIntegration, integrations);
39+
return [...getBrowserDefaultIntegrations(options), new BrowserTracing()];
3640
}
3741
}
3842

39-
options.integrations = integrations;
43+
return undefined;
4044
}

packages/astro/src/server/sdk.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { getCurrentScope } from '@sentry/core';
21
import type { NodeOptions } from '@sentry/node';
3-
import { init as initNodeSdk } from '@sentry/node';
2+
import { init as initNodeSdk, setTag } from '@sentry/node';
43

54
import { applySdkMetadata } from '../common/metadata';
65

@@ -13,5 +12,5 @@ export function init(options: NodeOptions): void {
1312

1413
initNodeSdk(options);
1514

16-
getCurrentScope().setTag('runtime', 'node');
15+
setTag('runtime', 'node');
1716
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ describe('Sentry client SDK', () => {
6060
...tracingOptions,
6161
});
6262

63-
const integrationsToInit = browserInit.mock.calls[0][0]?.integrations;
63+
const integrationsToInit = browserInit.mock.calls[0][0]?.defaultIntegrations;
6464
const browserTracing = getClient<BrowserClient>()?.getIntegrationByName('BrowserTracing');
6565

6666
expect(integrationsToInit).toContainEqual(expect.objectContaining({ name: 'BrowserTracing' }));
@@ -76,7 +76,7 @@ describe('Sentry client SDK', () => {
7676
...tracingOptions,
7777
});
7878

79-
const integrationsToInit = browserInit.mock.calls[0][0]?.integrations;
79+
const integrationsToInit = browserInit.mock.calls[0][0]?.defaultIntegrations;
8080
const browserTracing = getClient<BrowserClient>()?.getIntegrationByName('BrowserTracing');
8181

8282
expect(integrationsToInit).not.toContainEqual(expect.objectContaining({ name: 'BrowserTracing' }));
@@ -91,7 +91,7 @@ describe('Sentry client SDK', () => {
9191
enableTracing: true,
9292
});
9393

94-
const integrationsToInit = browserInit.mock.calls[0][0]?.integrations;
94+
const integrationsToInit = browserInit.mock.calls[0][0]?.defaultIntegrations;
9595
const browserTracing = getClient<BrowserClient>()?.getIntegrationByName('BrowserTracing');
9696

9797
expect(integrationsToInit).not.toContainEqual(expect.objectContaining({ name: 'BrowserTracing' }));
@@ -107,7 +107,7 @@ describe('Sentry client SDK', () => {
107107
enableTracing: true,
108108
});
109109

110-
const integrationsToInit = browserInit.mock.calls[0][0]?.integrations;
110+
const integrationsToInit = browserInit.mock.calls[0][0]?.defaultIntegrations;
111111

112112
const browserTracing = getClient<BrowserClient>()?.getIntegrationByName('BrowserTracing') as BrowserTracing;
113113
const options = browserTracing.options;

0 commit comments

Comments
 (0)