Repository navigation
feat(browser): Extract fetch request instrumentation into fetchTracingIntegration
#26273
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
logaretm
merged 2 commits into
awad/linked-traces-integration
from
awad/fetch-tracing-integration
Nov 17, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
| }); | ||
| }, | ||
|
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); | ||
|
cursor[bot] marked this conversation as resolved.
Resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 amakeRequestTracingIntegration(name, instrumentFn)factory to deduplicate the option type and setup wiring?There was a problem hiding this comment.
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 🤷♂️