Skip to content

Commit

Permalink
feat(nuxt): Automatically add BrowserTracing (#13005)
Browse files Browse the repository at this point in the history
Add `BrowserTracing` when `tracesSampleRate` is set.
  • Loading branch information
s1gr1d authored Jul 23, 2024
1 parent 5a709df commit f867cc0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ Sentry.init({
dsn: 'https://[email protected]/1337',
tunnel: `http://localhost:3031/`, // proxy server
tracesSampleRate: 1.0,
integrations: [Sentry.browserTracingIntegration()],
});
7 changes: 6 additions & 1 deletion packages/nuxt/src/client/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { init as initBrowser } from '@sentry/browser';
import {
browserTracingIntegration,
getDefaultIntegrations as getBrowserDefaultIntegrations,
init as initBrowser,
} from '@sentry/browser';
import { applySdkMetadata } from '@sentry/core';
import type { Client } from '@sentry/types';
import type { SentryNuxtOptions } from '../common/types';
Expand All @@ -10,6 +14,7 @@ import type { SentryNuxtOptions } from '../common/types';
*/
export function init(options: SentryNuxtOptions): Client | undefined {
const sentryOptions = {
defaultIntegrations: [...getBrowserDefaultIntegrations(options), browserTracingIntegration()],
...options,
};

Expand Down
27 changes: 22 additions & 5 deletions packages/nuxt/test/client/sdk.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as SentryBrowser from '@sentry/browser';
import { SDK_VERSION } from '@sentry/vue';
import { type BrowserClient, SDK_VERSION, getClient } from '@sentry/vue';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { init } from '../../src/client';

const vueInit = vi.spyOn(SentryBrowser, 'init');
const browserInit = vi.spyOn(SentryBrowser, 'init');

describe('Nuxt Client SDK', () => {
describe('init', () => {
Expand All @@ -12,7 +12,7 @@ describe('Nuxt Client SDK', () => {
});

it('Adds Nuxt metadata to the SDK options', () => {
expect(vueInit).not.toHaveBeenCalled();
expect(browserInit).not.toHaveBeenCalled();

init({
dsn: 'https://[email protected]/1337',
Expand All @@ -31,8 +31,25 @@ describe('Nuxt Client SDK', () => {
},
};

expect(vueInit).toHaveBeenCalledTimes(1);
expect(vueInit).toHaveBeenLastCalledWith(expect.objectContaining(expectedMetadata));
expect(browserInit).toHaveBeenCalledTimes(1);
expect(browserInit).toHaveBeenLastCalledWith(expect.objectContaining(expectedMetadata));
});

describe('Automatically adds BrowserTracing integration', () => {
it.each([
['tracesSampleRate', { tracesSampleRate: 0 }],
['tracesSampler', { tracesSampler: () => 1.0 }],
['enableTracing', { enableTracing: true }],
['no tracing option set', {}] /* enable "tracing without performance" by default */,
])('adds a browserTracingIntegration if tracing is enabled via %s', (_, tracingOptions) => {
init({
dsn: 'https://[email protected]/1337',
...tracingOptions,
});

const browserTracing = getClient<BrowserClient>()?.getIntegrationByName('BrowserTracing');
expect(browserTracing).toBeDefined();
});
});

it('returns client from init', () => {
Expand Down

0 comments on commit f867cc0

Please sign in to comment.