Skip to content
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
1 change: 1 addition & 0 deletions packages/browser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export type { CachedRouteProvider, RouteProvider } from '@sentry/browser-utils';
export { interactionsIntegration } from './integrations/interactions';
export { linkedTracesIntegration } from './integrations/linkedTraces';
export { longTasksIntegration } from './integrations/longTasks';
export { fetchTracingIntegration } from './integrations/fetchTracing';
export { xhrTracingIntegration } from './integrations/xhrTracing';

export type { RequestInstrumentationOptions } from './tracing/request';
Expand Down
35 changes: 35 additions & 0 deletions packages/browser/src/integrations/fetchTracing.ts

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l: this is the same as in xhrTracing.ts, might be worth it to add a makeRequestTracingIntegration(name, instrumentFn) factory to deduplicate the option type and setup wiring?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought about it, but the shared part is just a one-line setup so it's a bit of a wash 🤷‍♂️

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { isBotUserAgent } from '@sentry/browser-utils';
import type { IntegrationFn } from '@sentry/core';
import { defineIntegration } from '@sentry/core';
import type { RequestInstrumentationOptions } from '../tracing/request';
import { instrumentFetchRequests } from '../tracing/request';

const INTEGRATION_NAME = 'FetchTracing';

type FetchTracingOptions = Partial<
Pick<
RequestInstrumentationOptions,
'shouldCreateSpanForRequest' | 'enableHTTPTimings' | 'onRequestSpanStart' | 'onRequestSpanEnd'
>
>;

const _fetchTracingIntegration = ((options: FetchTracingOptions = {}) => {
return {
name: INTEGRATION_NAME,
setup(client) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also kinda related to this comment: https://github.com/getsentry/sentry-javascript/pull/26260/changes#r4586643156

Each fetch from a bot becomes a root span, is that intended?

if (isBotUserAgent()) {
return;
}

instrumentFetchRequests(client, {
...options,
tracePropagationTargets: client.getOptions().tracePropagationTargets,
});
},
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
};
}) satisfies IntegrationFn;

/**
* Creates spans for outgoing fetch requests and attaches trace propagation headers to them.
*/
export const fetchTracingIntegration = defineIntegration(_fetchTracingIntegration);
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
35 changes: 15 additions & 20 deletions packages/browser/src/tracing/browserTracingIntegration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ import type { WebVitalsOptions } from '../integrations/webVitals';
import { webVitalsIntegration } from '../integrations/webVitals';
import { registerBackgroundTabDetection } from './backgroundtab';
import { linkedTracesIntegration } from '../integrations/linkedTraces';
import { fetchTracingIntegration } from '../integrations/fetchTracing';
import { xhrTracingIntegration } from '../integrations/xhrTracing';
import { defaultRequestInstrumentationOptions, instrumentFetchRequests } from './request';
import {
SENTRY_SEGMENT_NAME_SOURCE,
SENTRY_OP,
Expand Down Expand Up @@ -292,7 +292,12 @@ export type BrowserTracingCoreOptions = Omit<
| 'enableElementTiming'
| 'enableLongTask'
| 'enableLongAnimationFrame'
| 'traceFetch'
| 'traceXHR'
| 'shouldCreateSpanForRequest'
| 'enableHTTPTimings'
| 'onRequestSpanStart'
| 'onRequestSpanEnd'
| 'linkPreviousTrace'
| 'consistentTraceSampling'
>;
Expand All @@ -306,7 +311,6 @@ const DEFAULT_BROWSER_TRACING_OPTIONS: BrowserTracingCoreOptions = {
ignoreResourceSpans: [],
detectRedirects: true,
enableReportPageLoaded: false,
...defaultRequestInstrumentationOptions,
};

/**
Expand All @@ -318,6 +322,7 @@ const DEFAULT_FORWARDED_OPTIONS = {
enableInp: true,
enableLongTask: true,
enableLongAnimationFrame: true,
traceFetch: true,
traceXHR: true,
linkPreviousTrace: 'in-memory',
} satisfies Partial<BrowserTracingOptions>;
Expand Down Expand Up @@ -347,6 +352,7 @@ export const browserTracingIntegration = ((options: Partial<BrowserTracingOption
webVitals,
enableLongTask,
enableLongAnimationFrame,
traceFetch,
traceXHR,
shouldCreateSpanForRequest,
enableHTTPTimings,
Expand Down Expand Up @@ -374,10 +380,14 @@ export const browserTracingIntegration = ((options: Partial<BrowserTracingOption
client.addIntegration(longTasksIntegration({ enableLongTask, enableLongAnimationFrame }));
}

const requestOptions = { shouldCreateSpanForRequest, enableHTTPTimings, onRequestSpanStart, onRequestSpanEnd };

if (traceFetch) {
client.addIntegration(fetchTracingIntegration(requestOptions));
}

if (traceXHR) {
client.addIntegration(
xhrTracingIntegration({ shouldCreateSpanForRequest, enableHTTPTimings, onRequestSpanStart, onRequestSpanEnd }),
);
client.addIntegration(xhrTracingIntegration(requestOptions));
}

if (linkPreviousTrace !== 'off') {
Expand Down Expand Up @@ -412,17 +422,12 @@ function _browserTracingIntegration(
finalTimeout,
childSpanTimeout,
markBackgroundSpan,
traceFetch,
shouldCreateSpanForRequest,
enableHTTPTimings,
ignoreResourceSpans,
instrumentPageLoad,
instrumentNavigation,
instrumentBfcacheRestore,
detectRedirects,
enableReportPageLoaded,
onRequestSpanStart,
onRequestSpanEnd,
} = {
...DEFAULT_BROWSER_TRACING_OPTIONS,
...options,
Expand Down Expand Up @@ -794,16 +799,6 @@ function _browserTracingIntegration(
if (markBackgroundSpan) {
registerBackgroundTabDetection();
}

if (traceFetch) {
instrumentFetchRequests(client, {
tracePropagationTargets: client.getOptions().tracePropagationTargets,
shouldCreateSpanForRequest,
enableHTTPTimings,
onRequestSpanStart,
onRequestSpanEnd,
});
}
},
} satisfies Integration;
}
Expand Down
31 changes: 31 additions & 0 deletions packages/browser/test/integrations/fetchTracing.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* @vitest-environment jsdom
*/

import { afterEach, describe, expect, it, vi } from 'vitest';
import { BrowserClient } from '../../src/client';
import { fetchTracingIntegration } from '../../src/integrations/fetchTracing';
import * as requestModule from '../../src/tracing/request';
import { getDefaultBrowserClientOptions } from '../helper/browser-client-options';

describe('fetchTracingIntegration', () => {
afterEach(() => {
vi.restoreAllMocks();
});

it.each([
[
'instruments fetch requests for normal user agents',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) Chrome/120.0.0.0',
1,
],
['does not instrument fetch requests for bot user agents', 'Mozilla/5.0 (compatible; Googlebot/2.1)', 0],
])('%s', (_name, userAgent, calls) => {
const instrumentSpy = vi.spyOn(requestModule, 'instrumentFetchRequests').mockImplementation(() => {});
vi.spyOn(window.navigator, 'userAgent', 'get').mockReturnValue(userAgent);

fetchTracingIntegration().setup?.(new BrowserClient(getDefaultBrowserClientOptions()));

expect(instrumentSpy).toHaveBeenCalledTimes(calls);
});
});
21 changes: 21 additions & 0 deletions packages/browser/test/tracing/browserTracingIntegration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,27 @@ describe('browserTracingIntegration', () => {
expect(!!client.getIntegrationByName('LongTasks')).toBe(registered);
});

it.each([
['registers fetchTracingIntegration by default', {}, true],
['does not register fetchTracingIntegration when traceFetch is off', { traceFetch: false }, false],
[
'does not register fetchTracingIntegration when traceFetch is explicitly undefined',
{ traceFetch: undefined },
false,
],
])('%s', (_name, options, registered) => {
const client = new BrowserClient(
getDefaultBrowserClientOptions({
tracesSampleRate: 1,
integrations: [browserTracingIntegration(options)],
}),
);
setCurrentClient(client);
client.init();

expect(!!client.getIntegrationByName('FetchTracing')).toBe(registered);
});

it.each([
['registers xhrTracingIntegration by default', {}, true],
['does not register xhrTracingIntegration when traceXHR is off', { traceXHR: false }, false],
Expand Down
Loading