Skip to content

ref(astro): Streamline how we add browser tracing #10253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions packages/astro/src/client/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import type { BrowserOptions } from '@sentry/browser';
import { BrowserTracing, init as initBrowserSdk } from '@sentry/browser';
import { getCurrentScope, hasTracingEnabled } from '@sentry/core';
import { addOrUpdateIntegration } from '@sentry/utils';
import {
BrowserTracing,
getDefaultIntegrations as getBrowserDefaultIntegrations,
init as initBrowserSdk,
setTag,
} from '@sentry/browser';
import { hasTracingEnabled } from '@sentry/core';
import type { Integration } from '@sentry/types';

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

Expand All @@ -14,27 +19,26 @@ declare const __SENTRY_TRACING__: boolean;
* @param options Configuration options for the SDK.
*/
export function init(options: BrowserOptions): void {
applySdkMetadata(options, ['astro', 'browser']);
const opts = {
defaultIntegrations: getDefaultIntegrations(options),
...options,
};

addClientIntegrations(options);
applySdkMetadata(opts, ['astro', 'browser']);

initBrowserSdk(options);
initBrowserSdk(opts);

getCurrentScope().setTag('runtime', 'browser');
setTag('runtime', 'browser');
}

function addClientIntegrations(options: BrowserOptions): void {
let integrations = options.integrations || [];

function getDefaultIntegrations(options: BrowserOptions): Integration[] | undefined {
// This evaluates to true unless __SENTRY_TRACING__ is text-replaced with "false",
// in which case everything inside will get treeshaken away
if (typeof __SENTRY_TRACING__ === 'undefined' || __SENTRY_TRACING__) {
if (hasTracingEnabled(options)) {
const defaultBrowserTracingIntegration = new BrowserTracing({});

integrations = addOrUpdateIntegration(defaultBrowserTracingIntegration, integrations);
return [...getBrowserDefaultIntegrations(options), new BrowserTracing()];
}
}

options.integrations = integrations;
return undefined;
}
5 changes: 2 additions & 3 deletions packages/astro/src/server/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { getCurrentScope } from '@sentry/core';
import type { NodeOptions } from '@sentry/node';
import { init as initNodeSdk } from '@sentry/node';
import { init as initNodeSdk, setTag } from '@sentry/node';

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

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

initNodeSdk(options);

getCurrentScope().setTag('runtime', 'node');
setTag('runtime', 'node');
}
8 changes: 4 additions & 4 deletions packages/astro/test/client/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('Sentry client SDK', () => {
...tracingOptions,
});

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

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

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

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

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

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

const integrationsToInit = browserInit.mock.calls[0][0]?.integrations;
const integrationsToInit = browserInit.mock.calls[0][0]?.defaultIntegrations;

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