Repository navigation
feat(browser): Extract resource spans into resourcesIntegration
#26276
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import type { IntegrationFn } from '@sentry/core'; | ||
| import { defineIntegration, hasSpanStreamingEnabled, spanToJSON } from '@sentry/core'; | ||
| import { SENTRY_OP } from '@sentry/conventions/attributes'; | ||
| import { NAVIGATION, PAGELOAD } from '@sentry/conventions/op'; | ||
| import { isBotUserAgent } from '../isBotUserAgent'; | ||
| import { addResourceSpans } from './entries'; | ||
|
|
||
| interface ResourcesOptions { | ||
| /** | ||
| * Resource spans with `op`s matching strings in the array will not be emitted. | ||
| * | ||
| * Default: [] | ||
| */ | ||
| ignoreResourceSpans?: Array<'resource.script' | 'resource.css' | 'resource.img' | 'resource.other' | string>; | ||
| } | ||
|
|
||
| const INTEGRATION_NAME = 'Resources'; | ||
|
|
||
| const _resourcesIntegration = ((options: ResourcesOptions = {}) => { | ||
| return { | ||
| name: INTEGRATION_NAME, | ||
| setup(client) { | ||
| if (isBotUserAgent()) { | ||
| return; | ||
| } | ||
|
|
||
| client.on('beforeIdleSpanEnd', span => { | ||
| // Interaction idle spans also end through this hook. We match the route span by op, since | ||
| // `browserTracingIntegration` may already have cleared its active idle span at this point. | ||
| const op = spanToJSON(span).attributes?.[SENTRY_OP]; | ||
| if (op !== PAGELOAD && op !== NAVIGATION) { | ||
| return; | ||
| } | ||
|
|
||
| addResourceSpans(span, { | ||
| ignoreResourceSpans: options.ignoreResourceSpans, | ||
| spanStreamingEnabled: hasSpanStreamingEnabled(client), | ||
| }); | ||
| }); | ||
| }, | ||
| }; | ||
| }) satisfies IntegrationFn; | ||
|
|
||
| /** | ||
| * Adds `resource.*` spans for the scripts, stylesheets, images and other resources loaded during a | ||
| * pageload or navigation. | ||
| */ | ||
| export const resourcesIntegration = defineIntegration(_resourcesIntegration); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import { SENTRY_OP } from '@sentry/conventions/attributes'; | ||
| import { NAVIGATION, PAGELOAD, UI_ACTION_CLICK } from '@sentry/conventions/op'; | ||
| import type { Span } from '@sentry/core'; | ||
| import { getMainCarrier, SentrySpan, setCurrentClient, spanToJSON } from '@sentry/core'; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
| import { resourcesIntegration } from '../../src/performance/resources'; | ||
| import { WINDOW } from '../../src/types'; | ||
| import { getDefaultClientOptions, TestClient } from '../utils/TestClient'; | ||
|
|
||
| describe('resourcesIntegration', () => { | ||
| let client: TestClient; | ||
| let endedSpans: Span[]; | ||
|
|
||
| beforeEach(() => { | ||
| getMainCarrier().__SENTRY__ = undefined; | ||
| client = new TestClient(getDefaultClientOptions({ tracesSampleRate: 1, integrations: [resourcesIntegration()] })); | ||
| setCurrentClient(client); | ||
| client.init(); | ||
|
|
||
| endedSpans = []; | ||
| client.on('spanEnd', span => void endedSpans.push(span)); | ||
|
|
||
| vi.stubGlobal('addEventListener', vi.fn()); | ||
| vi.stubGlobal('location', { origin: 'https://example.com' }); | ||
| vi.spyOn(WINDOW.performance, 'getEntries').mockReturnValue([ | ||
| { | ||
| entryType: 'resource', | ||
| name: 'https://example.com/app.js', | ||
| initiatorType: 'script', | ||
| startTime: 100, | ||
| duration: 50, | ||
| responseEnd: 150, | ||
| toJSON: () => ({}), | ||
| } as PerformanceResourceTiming, | ||
| ]); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| vi.unstubAllGlobals(); | ||
| }); | ||
|
|
||
| it.each([ | ||
| ['adds resource spans to pageload spans', PAGELOAD, 1], | ||
| ['adds resource spans to navigation spans', NAVIGATION, 1], | ||
| ['skips interaction idle spans', UI_ACTION_CLICK, 0], | ||
| ])('%s', (_name, op, count) => { | ||
| const span = new SentrySpan({ op, name: '/', sampled: true, startTimestamp: 1 }); | ||
|
|
||
| client.emit('beforeIdleSpanEnd', span); | ||
|
|
||
| expect( | ||
| endedSpans.map(s => spanToJSON(s).attributes?.[SENTRY_OP]).filter(o => o === 'resource.script'), | ||
| ).toHaveLength(count); | ||
|
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. Test uses hard-coded span opLow Severity The new Triggered by project rule: PR Review Guidelines for Cursor Bot Reviewed by Cursor Bugbot for commit 3d3643d. Configure here. |
||
| }); | ||
|
|
||
| it('does not add resource spans for bot user agents', () => { | ||
| vi.spyOn(WINDOW.navigator, 'userAgent', 'get').mockReturnValue('Mozilla/5.0 (compatible; Googlebot/2.1)'); | ||
| const botClient = new TestClient(getDefaultClientOptions({ tracesSampleRate: 1 })); | ||
| resourcesIntegration().setup?.(botClient); | ||
|
|
||
| botClient.emit('beforeIdleSpanEnd', new SentrySpan({ op: PAGELOAD, name: '/', sampled: true, startTimestamp: 1 })); | ||
|
|
||
| expect(endedSpans).toHaveLength(0); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { resourcesIntegration } from '@sentry/browser-utils'; |


Uh oh!
There was an error while loading. Please reload this page.