-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(replay): Add Request body with attachRawBodyFromRequest option
#18501
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
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
257e8de
feat(replay): Add `Request` body with `attachRawBodyFromRequest` option
s1gr1d 889d18b
clean up and add tests
s1gr1d cf74a2c
add changelog entry
s1gr1d 529fca1
add integration test
s1gr1d e6a8f80
add test with ReadableStream
s1gr1d 95ac846
Merge branch 'develop' into sig/replay-raw-request-bodies
s1gr1d b29ec49
increase size limits
s1gr1d 30a958d
fix test
s1gr1d c3cdc67
reduce bundle size
s1gr1d 4cc098b
Merge branch 'develop' into sig/replay-raw-request-bodies
s1gr1d 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
22 changes: 22 additions & 0 deletions
22
dev-packages/browser-integration-tests/suites/replay/attachRawBodyFromRequest/init.js
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,22 @@ | ||
| import * as Sentry from '@sentry/browser'; | ||
|
|
||
| window.Sentry = Sentry; | ||
| window.Replay = Sentry.replayIntegration({ | ||
| flushMinDelay: 200, | ||
| flushMaxDelay: 200, | ||
| minReplayDuration: 0, | ||
|
|
||
| networkDetailAllowUrls: ['http://sentry-test.io/foo'], | ||
| networkCaptureBodies: true, | ||
| attachRawBodyFromRequest: true, | ||
| }); | ||
|
|
||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| sampleRate: 1, | ||
| // We ensure to sample for errors, so by default nothing is sent | ||
| replaysSessionSampleRate: 0.0, | ||
| replaysOnErrorSampleRate: 1.0, | ||
|
|
||
| integrations: [window.Replay], | ||
| }); |
10 changes: 10 additions & 0 deletions
10
dev-packages/browser-integration-tests/suites/replay/attachRawBodyFromRequest/template.html
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,10 @@ | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| </head> | ||
| <body> | ||
| <h1>attachRawBodyFromRequest Test</h1> | ||
| </body> | ||
| </html> | ||
|
|
145 changes: 145 additions & 0 deletions
145
dev-packages/browser-integration-tests/suites/replay/attachRawBodyFromRequest/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,145 @@ | ||
| import type { PlaywrightTestArgs } from '@playwright/test'; | ||
| import { expect } from '@playwright/test'; | ||
| import type { TestFixtures } from '../../../utils/fixtures'; | ||
| import { sentryTest } from '../../../utils/fixtures'; | ||
| import { envelopeRequestParser, waitForErrorRequest } from '../../../utils/helpers'; | ||
| import { collectReplayRequests, getReplayPerformanceSpans, shouldSkipReplayTest } from '../../../utils/replayHelpers'; | ||
|
|
||
| /** | ||
| * Shared helper to run the common test flow | ||
| */ | ||
| async function runRequestFetchTest( | ||
| { page, getLocalTestUrl }: { page: PlaywrightTestArgs['page']; getLocalTestUrl: TestFixtures['getLocalTestUrl'] }, | ||
| options: { | ||
| evaluateFn: () => void; | ||
| expectedBody: any; | ||
| expectedSize: number | any; | ||
| expectedExtraReplayData?: any; | ||
| }, | ||
| ) { | ||
| if (shouldSkipReplayTest()) { | ||
| sentryTest.skip(); | ||
| } | ||
|
|
||
| await page.route('http://sentry-test.io/foo', route => route.fulfill({ status: 200 })); | ||
|
|
||
| const requestPromise = waitForErrorRequest(page); | ||
| const replayRequestPromise = collectReplayRequests(page, recordingEvents => | ||
| getReplayPerformanceSpans(recordingEvents).some(span => span.op === 'resource.fetch'), | ||
| ); | ||
|
|
||
| const url = await getLocalTestUrl({ testDir: __dirname }); | ||
| await page.goto(url); | ||
| await page.evaluate(options.evaluateFn); | ||
|
|
||
| // Envelope/Breadcrumbs | ||
| const eventData = envelopeRequestParser(await requestPromise); | ||
| expect(eventData.exception?.values).toHaveLength(1); | ||
|
|
||
| const fetchBreadcrumbs = eventData?.breadcrumbs?.filter(b => b.category === 'fetch'); | ||
| expect(fetchBreadcrumbs).toHaveLength(1); | ||
| expect(fetchBreadcrumbs![0]).toEqual({ | ||
| timestamp: expect.any(Number), | ||
| category: 'fetch', | ||
| type: 'http', | ||
| data: { | ||
| method: 'POST', | ||
| request_body_size: options.expectedSize, | ||
| status_code: 200, | ||
| url: 'http://sentry-test.io/foo', | ||
| }, | ||
| }); | ||
|
|
||
| // Replay Spans | ||
| const { replayRecordingSnapshots } = await replayRequestPromise; | ||
| const fetchSpans = getReplayPerformanceSpans(replayRecordingSnapshots).filter(s => s.op === 'resource.fetch'); | ||
| expect(fetchSpans).toHaveLength(1); | ||
|
|
||
| expect(fetchSpans[0]).toMatchObject({ | ||
| data: { | ||
| method: 'POST', | ||
| statusCode: 200, | ||
| request: { | ||
| body: options.expectedBody, | ||
| }, | ||
| ...options.expectedExtraReplayData, | ||
| }, | ||
| description: 'http://sentry-test.io/foo', | ||
| endTimestamp: expect.any(Number), | ||
| op: 'resource.fetch', | ||
| startTimestamp: expect.any(Number), | ||
| }); | ||
| } | ||
|
|
||
| sentryTest('captures request body when using Request object with text body', async ({ page, getLocalTestUrl }) => { | ||
| await runRequestFetchTest( | ||
| { page, getLocalTestUrl }, | ||
| { | ||
| evaluateFn: () => { | ||
| const request = new Request('http://sentry-test.io/foo', { method: 'POST', body: 'Request body text' }); | ||
| // @ts-expect-error Sentry is a global | ||
| fetch(request).then(() => Sentry.captureException('test error')); | ||
| }, | ||
| expectedBody: 'Request body text', | ||
| expectedSize: 17, | ||
| }, | ||
| ); | ||
| }); | ||
|
|
||
| sentryTest('captures request body when using Request object with JSON body', async ({ page, getLocalTestUrl }) => { | ||
| await runRequestFetchTest( | ||
| { page, getLocalTestUrl }, | ||
| { | ||
| evaluateFn: () => { | ||
| const request = new Request('http://sentry-test.io/foo', { | ||
| method: 'POST', | ||
| body: JSON.stringify({ name: 'John', age: 30 }), | ||
| }); | ||
| // @ts-expect-error Sentry is a global | ||
| fetch(request).then(() => Sentry.captureException('test error')); | ||
| }, | ||
| expectedBody: { name: 'John', age: 30 }, | ||
| expectedSize: expect.any(Number), | ||
| }, | ||
| ); | ||
| }); | ||
|
|
||
| sentryTest('prioritizes options body over Request object body', async ({ page, getLocalTestUrl, browserName }) => { | ||
| const additionalHeaders = browserName === 'webkit' ? { 'content-type': 'text/plain' } : undefined; | ||
|
|
||
| await runRequestFetchTest( | ||
| { page, getLocalTestUrl }, | ||
| { | ||
| evaluateFn: () => { | ||
| const request = new Request('http://sentry-test.io/foo', { method: 'POST', body: 'original body' }); | ||
| // Second argument body should override the Request body | ||
| // @ts-expect-error Sentry is a global | ||
| fetch(request, { body: 'override body' }).then(() => Sentry.captureException('test error')); | ||
| }, | ||
| expectedBody: 'override body', | ||
| expectedSize: 13, | ||
| expectedExtraReplayData: { | ||
| request: { size: 13, headers: {} }, // Specific override structure check | ||
| ...(additionalHeaders && { response: { headers: additionalHeaders } }), | ||
| }, | ||
| }, | ||
| ); | ||
| }); | ||
|
|
||
| sentryTest('captures request body with FormData in Request object', async ({ page, getLocalTestUrl }) => { | ||
| await runRequestFetchTest( | ||
| { page, getLocalTestUrl }, | ||
| { | ||
| evaluateFn: () => { | ||
| const params = new URLSearchParams(); | ||
| params.append('key1', 'value1'); | ||
| params.append('key2', 'value2'); | ||
| const request = new Request('http://sentry-test.io/foo', { method: 'POST', body: params }); | ||
| // @ts-expect-error Sentry is a global | ||
| fetch(request).then(() => Sentry.captureException('test error')); | ||
| }, | ||
| expectedBody: 'key1=value1&key2=value2', | ||
| expectedSize: 23, | ||
| }, | ||
| ); | ||
| }); |
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.
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.
q: It is possible that the user creates a req object with a stream body, should we explicitly return
undefinedhere or is it ignored safely at the caller?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.
Currently, we would also attach the stream. It's a very unlikely use case because it would mean that you create a
ReadableStreamyourself and attach it to aRequest, which results in a double-nested readable stream 🤔