-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(react-router): Create low quality transactions filter for react router #16219
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
RulaKhaled
merged 8 commits into
develop
from
create-lowQualityTransactionsFilter-for-react-router
May 12, 2025
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
56c4346
feat(react-router): Create a transaction filter for react router
RulaKhaled 3540666
Update react router sdk tests
RulaKhaled aa9e709
Fix linter issues
RulaKhaled e4fe07f
fix imports for linter
RulaKhaled 79ec0d6
Update low quality transaction filter to an integration in react-router
RulaKhaled f862390
Fix linter issues
RulaKhaled caf789a
Resolve undefined typescript issue
RulaKhaled 497cfc1
Merge branch 'develop' into create-lowQualityTransactionsFilter-for-r…
RulaKhaled 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
35 changes: 35 additions & 0 deletions
35
packages/react-router/src/server/lowQualityTransactionsFilterIntegration.ts
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 { type Client, type Event, type EventHint, defineIntegration, logger } from '@sentry/core'; | ||
import type { NodeOptions } from '@sentry/node'; | ||
|
||
/** | ||
* Integration that filters out noisy http transactions such as requests to node_modules, favicon.ico, @id/ | ||
* | ||
*/ | ||
|
||
function _lowQualityTransactionsFilterIntegration(options: NodeOptions): { | ||
name: string; | ||
processEvent: (event: Event, hint: EventHint, client: Client) => Event | null; | ||
} { | ||
const matchedRegexes = [/GET \/node_modules\//, /GET \/favicon\.ico/, /GET \/@id\//]; | ||
|
||
return { | ||
name: 'LowQualityTransactionsFilter', | ||
|
||
processEvent(event: Event, _hint: EventHint, _client: Client): Event | null { | ||
if (event.type !== 'transaction' || !event.transaction) { | ||
return event; | ||
} | ||
|
||
if (matchedRegexes.some(regex => event.transaction?.match(regex))) { | ||
options.debug && logger.log('[ReactRouter] Filtered node_modules transaction:', event.transaction); | ||
return null; | ||
} | ||
|
||
return event; | ||
}, | ||
}; | ||
} | ||
|
||
export const lowQualityTransactionsFilterIntegration = defineIntegration((options: NodeOptions) => | ||
_lowQualityTransactionsFilterIntegration(options), | ||
); |
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
66 changes: 66 additions & 0 deletions
66
packages/react-router/test/server/lowQualityTransactionsFilterIntegration.test.ts
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,66 @@ | ||
import type { Event, EventType, Integration } from '@sentry/core'; | ||
import * as SentryCore from '@sentry/core'; | ||
import * as SentryNode from '@sentry/node'; | ||
import { afterEach, describe, expect, it, vi } from 'vitest'; | ||
import { lowQualityTransactionsFilterIntegration } from '../../src/server/lowQualityTransactionsFilterIntegration'; | ||
|
||
const loggerLog = vi.spyOn(SentryCore.logger, 'log').mockImplementation(() => {}); | ||
|
||
describe('Low Quality Transactions Filter Integration', () => { | ||
afterEach(() => { | ||
vi.clearAllMocks(); | ||
SentryNode.getGlobalScope().clear(); | ||
}); | ||
|
||
describe('integration functionality', () => { | ||
describe('filters out low quality transactions', () => { | ||
it.each([ | ||
['node_modules requests', 'GET /node_modules/some-package/index.js'], | ||
['favicon.ico requests', 'GET /favicon.ico'], | ||
['@id/ requests', 'GET /@id/some-id'], | ||
])('%s', (description, transaction) => { | ||
const integration = lowQualityTransactionsFilterIntegration({ debug: true }) as Integration; | ||
const event = { | ||
type: 'transaction' as EventType, | ||
transaction, | ||
} as Event; | ||
|
||
const result = integration.processEvent!(event, {}, {} as SentryCore.Client); | ||
|
||
expect(result).toBeNull(); | ||
|
||
expect(loggerLog).toHaveBeenCalledWith('[ReactRouter] Filtered node_modules transaction:', transaction); | ||
}); | ||
}); | ||
|
||
describe('allows high quality transactions', () => { | ||
it.each([ | ||
['normal page requests', 'GET /api/users'], | ||
['API endpoints', 'POST /data'], | ||
['app routes', 'GET /projects/123'], | ||
])('%s', (description, transaction) => { | ||
const integration = lowQualityTransactionsFilterIntegration({}) as Integration; | ||
const event = { | ||
type: 'transaction' as EventType, | ||
transaction, | ||
} as Event; | ||
|
||
const result = integration.processEvent!(event, {}, {} as SentryCore.Client); | ||
|
||
expect(result).toEqual(event); | ||
}); | ||
}); | ||
|
||
it('does not affect non-transaction events', () => { | ||
const integration = lowQualityTransactionsFilterIntegration({}) as Integration; | ||
const event = { | ||
type: 'error' as EventType, | ||
transaction: 'GET /node_modules/some-package/index.js', | ||
} as Event; | ||
|
||
const result = integration.processEvent!(event, {}, {} as SentryCore.Client); | ||
|
||
expect(result).toEqual(event); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,6 +1,9 @@ | ||
import type { Integration } from '@sentry/core'; | ||
import type { NodeClient } from '@sentry/node'; | ||
import * as SentryNode from '@sentry/node'; | ||
import { SDK_VERSION } from '@sentry/node'; | ||
import { afterEach, describe, expect, it, vi } from 'vitest'; | ||
import * as LowQualityModule from '../../src/server/lowQualityTransactionsFilterIntegration'; | ||
import { init as reactRouterInit } from '../../src/server/sdk'; | ||
|
||
const nodeInit = vi.spyOn(SentryNode, 'init'); | ||
|
@@ -39,7 +42,34 @@ describe('React Router server SDK', () => { | |
}); | ||
|
||
it('returns client from init', () => { | ||
expect(reactRouterInit({})).not.toBeUndefined(); | ||
const client = reactRouterInit({ | ||
dsn: 'https://[email protected]/1337', | ||
}) as NodeClient; | ||
expect(client).not.toBeUndefined(); | ||
}); | ||
|
||
it('adds the low quality transactions filter integration by default', () => { | ||
const filterSpy = vi.spyOn(LowQualityModule, 'lowQualityTransactionsFilterIntegration'); | ||
|
||
reactRouterInit({ | ||
dsn: 'https://[email protected]/1337', | ||
}); | ||
|
||
expect(filterSpy).toHaveBeenCalled(); | ||
|
||
expect(nodeInit).toHaveBeenCalledTimes(1); | ||
const initOptions = nodeInit.mock.calls[0]?.[0]; | ||
|
||
expect(initOptions).toBeDefined(); | ||
|
||
const defaultIntegrations = initOptions?.defaultIntegrations as Integration[]; | ||
expect(Array.isArray(defaultIntegrations)).toBe(true); | ||
|
||
const filterIntegration = defaultIntegrations.find( | ||
integration => integration.name === 'LowQualityTransactionsFilter', | ||
); | ||
|
||
expect(filterIntegration).toBeDefined(); | ||
}); | ||
}); | ||
}); |
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.
Uh oh!
There was an error while loading. Please reload this page.