Skip to content
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

feat(nuxt): Automatically add BrowserTracing #13005

Merged
merged 4 commits into from
Jul 23, 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
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ Sentry.init({
dsn: 'https://public@dsn.ingest.sentry.io/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://public@dsn.ingest.sentry.io/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://public@dsn.ingest.sentry.io/1337',
...tracingOptions,
});

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

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