-
-
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 4 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
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 |
---|---|---|
@@ -1,7 +1,10 @@ | ||
import type { Event, EventType } from '@sentry/core'; | ||
import { getGlobalScope } 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 { init as reactRouterInit } from '../../src/server/sdk'; | ||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
import { init as reactRouterInit, lowQualityTransactionsFilter } from '../../src/server/sdk'; | ||
|
||
const nodeInit = vi.spyOn(SentryNode, 'init'); | ||
|
||
|
@@ -39,7 +42,97 @@ 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('registers the low quality transactions filter', async () => { | ||
const addEventProcessor = vi.spyOn(getGlobalScope(), 'addEventProcessor'); | ||
addEventProcessor.mockClear(); | ||
|
||
reactRouterInit({ | ||
dsn: 'https://[email protected]/1337', | ||
}) as NodeClient; | ||
|
||
expect(addEventProcessor).toHaveBeenCalledTimes(1); | ||
const processor = addEventProcessor.mock.calls[0]![0]; | ||
expect(processor?.id).toEqual('ReactRouterLowQualityTransactionsFilter'); | ||
}); | ||
|
||
describe('transaction filtering', () => { | ||
const beforeSendEvent = vi.fn(event => event); | ||
let client: NodeClient; | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
beforeSendEvent.mockClear(); | ||
SentryNode.getGlobalScope().clear(); | ||
|
||
client = reactRouterInit({ | ||
dsn: 'https://[email protected]/1337', | ||
}) as NodeClient; | ||
|
||
client.on('beforeSendEvent', beforeSendEvent); | ||
}); | ||
|
||
describe('filters out low quality transactions', () => { | ||
it.each(['GET /node_modules/react/index.js', 'GET /favicon.ico', 'GET /@id/package'])( | ||
'%s', | ||
async transaction => { | ||
client.captureEvent({ type: 'transaction', transaction }); | ||
|
||
await client.flush(); | ||
|
||
expect(beforeSendEvent).not.toHaveBeenCalled(); | ||
}, | ||
); | ||
}); | ||
|
||
describe('allows high quality transactions', () => { | ||
it.each(['GET /', 'GET /users', 'POST /api/data', 'GET /projects/123'])('%s', async transaction => { | ||
client.captureEvent({ type: 'transaction', transaction }); | ||
|
||
await client.flush(); | ||
|
||
expect(beforeSendEvent).toHaveBeenCalledWith(expect.objectContaining({ transaction }), expect.any(Object)); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('lowQualityTransactionsFilter', () => { | ||
describe('filters out low quality transactions', () => { | ||
it.each([ | ||
['node_modules request', 'GET /node_modules/react/index.js'], | ||
['favicon.ico request', 'GET /favicon.ico'], | ||
['@id request', 'GET /@id/package'], | ||
])('%s', (description, transaction) => { | ||
const filter = lowQualityTransactionsFilter({}); | ||
const event = { | ||
type: 'transaction' as EventType, | ||
transaction, | ||
} as Event; | ||
|
||
expect(filter(event, {})).toBeNull(); | ||
}); | ||
}); | ||
|
||
describe('does not filter good transactions', () => { | ||
it.each([ | ||
['normal page request', 'GET /users'], | ||
['API request', 'POST /api/users'], | ||
['app route', 'GET /projects/123'], | ||
])('%s', (description, transaction) => { | ||
const filter = lowQualityTransactionsFilter({}); | ||
const event = { | ||
type: 'transaction' as EventType, | ||
transaction, | ||
} as Event; | ||
|
||
expect(filter(event, {})).toBe(event); | ||
}); | ||
}); | ||
}); | ||
}); |
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.