-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(browser): Allow to capture request payload/responses #7287
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,8 +14,13 @@ import { | |
|
||
import { WINDOW } from '../helpers'; | ||
|
||
interface XhrFetchOptions { | ||
captureRequestPayload: boolean; | ||
captureResponsePayload: boolean; | ||
} | ||
|
||
/** JSDoc */ | ||
interface BreadcrumbsOptions { | ||
interface BreadcrumbsOptions extends XhrFetchOptions { | ||
console: boolean; | ||
dom: | ||
| boolean | ||
|
@@ -66,6 +71,8 @@ export class Breadcrumbs implements Integration { | |
history: true, | ||
sentry: true, | ||
xhr: true, | ||
captureRequestPayload: false, | ||
captureResponsePayload: false, | ||
...options, | ||
}; | ||
} | ||
|
@@ -86,10 +93,10 @@ export class Breadcrumbs implements Integration { | |
addInstrumentationHandler('dom', _domBreadcrumb(this.options.dom)); | ||
} | ||
if (this.options.xhr) { | ||
addInstrumentationHandler('xhr', _xhrBreadcrumb); | ||
addInstrumentationHandler('xhr', _xhrBreadcrumb(this.options)); | ||
} | ||
if (this.options.fetch) { | ||
addInstrumentationHandler('fetch', _fetchBreadcrumb); | ||
addInstrumentationHandler('fetch', _fetchBreadcrumb(this.options)); | ||
} | ||
if (this.options.history) { | ||
addInstrumentationHandler('history', _historyBreadcrumb); | ||
|
@@ -217,79 +224,111 @@ function _consoleBreadcrumb(handlerData: { [key: string]: any }): void { | |
* Creates breadcrumbs from XHR API calls | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
function _xhrBreadcrumb(handlerData: { [key: string]: any }): void { | ||
if (handlerData.endTimestamp) { | ||
// We only capture complete, non-sentry requests | ||
if (handlerData.xhr.__sentry_own_request__) { | ||
return; | ||
} | ||
function _xhrBreadcrumb(options: XhrFetchOptions): (handlerData: { [key: string]: any }) => void { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
return (handlerData: { [key: string]: any }): void => { | ||
if (handlerData.endTimestamp) { | ||
// We only capture complete, non-sentry requests | ||
if (handlerData.xhr.__sentry_own_request__) { | ||
return; | ||
} | ||
|
||
const { method, url, status_code, body } = handlerData.xhr.__sentry_xhr__ || {}; | ||
const { method, url, status_code, body } = handlerData.xhr.__sentry_xhr__ || {}; | ||
|
||
getCurrentHub().addBreadcrumb( | ||
{ | ||
category: 'xhr', | ||
data: { | ||
method, | ||
url, | ||
status_code, | ||
const xhrData: { [key: string]: unknown } = { | ||
method, | ||
url, | ||
status_code, | ||
}; | ||
|
||
if (options.captureRequestPayload && body) { | ||
xhrData.request_payload = body; | ||
} | ||
|
||
if (options.captureResponsePayload && handlerData.xhr.responseText) { | ||
xhrData.response_payload = handlerData.xhr.responseText; | ||
} | ||
|
||
getCurrentHub().addBreadcrumb( | ||
{ | ||
category: 'xhr', | ||
data: xhrData, | ||
type: 'http', | ||
}, | ||
type: 'http', | ||
}, | ||
{ | ||
xhr: handlerData.xhr, | ||
input: body, | ||
}, | ||
); | ||
{ | ||
xhr: handlerData.xhr, | ||
input: body, | ||
}, | ||
); | ||
|
||
return; | ||
} | ||
return; | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Creates breadcrumbs from fetch API calls | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
function _fetchBreadcrumb(handlerData: { [key: string]: any }): void { | ||
// We only capture complete fetch requests | ||
if (!handlerData.endTimestamp) { | ||
return; | ||
} | ||
function _fetchBreadcrumb(options: XhrFetchOptions): (handlerData: { [key: string]: any }) => Promise<void> { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
return async (handlerData: { [key: string]: any }): Promise<void> => { | ||
// We only capture complete fetch requests | ||
if (!handlerData.endTimestamp) { | ||
return; | ||
} | ||
|
||
if (handlerData.fetchData.url.match(/sentry_key/) && handlerData.fetchData.method === 'POST') { | ||
// We will not create breadcrumbs for fetch requests that contain `sentry_key` (internal sentry requests) | ||
return; | ||
} | ||
if (handlerData.fetchData.url.match(/sentry_key/) && handlerData.fetchData.method === 'POST') { | ||
// We will not create breadcrumbs for fetch requests that contain `sentry_key` (internal sentry requests) | ||
return; | ||
} | ||
|
||
if (handlerData.error) { | ||
getCurrentHub().addBreadcrumb( | ||
{ | ||
category: 'fetch', | ||
data: handlerData.fetchData, | ||
level: 'error', | ||
type: 'http', | ||
}, | ||
{ | ||
data: handlerData.error, | ||
input: handlerData.args, | ||
}, | ||
); | ||
} else { | ||
getCurrentHub().addBreadcrumb( | ||
{ | ||
category: 'fetch', | ||
data: { | ||
...handlerData.fetchData, | ||
status_code: handlerData.response.status, | ||
if (options.captureRequestPayload && handlerData.args[1] && handlerData.args[1].body) { | ||
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. Same question here irt falsy |
||
handlerData.fetchData.request_payload = handlerData.args[1].body; | ||
} | ||
|
||
if (options.captureResponsePayload && handlerData.response) { | ||
try { | ||
// We need to clone() this in order to avoid consuming the original response body | ||
const response = handlerData.response.clone(); | ||
const text = await response.text(); | ||
if (text) { | ||
handlerData.fetchData.response_payload = text; | ||
Comment on lines
+294
to
+295
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. And here for falsy values |
||
} | ||
} catch (error) { | ||
// if something happens here, we don't want to break the whole breadcrumb | ||
} | ||
} | ||
|
||
if (handlerData.error) { | ||
getCurrentHub().addBreadcrumb( | ||
{ | ||
category: 'fetch', | ||
data: handlerData.fetchData, | ||
level: 'error', | ||
type: 'http', | ||
}, | ||
type: 'http', | ||
}, | ||
{ | ||
input: handlerData.args, | ||
response: handlerData.response, | ||
}, | ||
); | ||
} | ||
{ | ||
data: handlerData.error, | ||
input: handlerData.args, | ||
}, | ||
); | ||
} else { | ||
getCurrentHub().addBreadcrumb( | ||
{ | ||
category: 'fetch', | ||
data: { | ||
...handlerData.fetchData, | ||
status_code: handlerData.response.status, | ||
}, | ||
type: 'http', | ||
}, | ||
{ | ||
input: handlerData.args, | ||
response: handlerData.response, | ||
}, | ||
); | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
|
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
14 changes: 14 additions & 0 deletions
14
packages/integration-tests/suites/integrations/browser/breadcrumbs/fetch/defaults/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,14 @@ | ||
import * as Sentry from '@sentry/browser'; | ||
import { Breadcrumbs } from '@sentry/browser'; | ||
|
||
window.Sentry = Sentry; | ||
window.breadcrumbs = []; | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
integrations: [new Breadcrumbs()], | ||
beforeBreadcrumb(breadcrumb) { | ||
window.breadcrumbs.push(breadcrumb); | ||
return breadcrumb; | ||
}, | ||
}); |
10 changes: 10 additions & 0 deletions
10
packages/integration-tests/suites/integrations/browser/breadcrumbs/fetch/defaults/subject.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,10 @@ | ||
fetch('http://localhost:7654/foo', { | ||
method: 'POST', | ||
body: '{"foo":"bar"}', | ||
}) | ||
.then(res => { | ||
return res.json(); | ||
}) | ||
.then(json => { | ||
// do something with the response | ||
}); |
41 changes: 41 additions & 0 deletions
41
packages/integration-tests/suites/integrations/browser/breadcrumbs/fetch/defaults/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,41 @@ | ||
import { expect } from '@playwright/test'; | ||
import type { Breadcrumb } from '@sentry/types'; | ||
|
||
import { sentryTest } from '../../../../../../utils/fixtures'; | ||
|
||
sentryTest('works with default options', async ({ getLocalTestPath, page }) => { | ||
await page.route('**/foo', route => { | ||
return route.fulfill({ | ||
status: 200, | ||
body: JSON.stringify({ | ||
testApi: 'OK', | ||
}), | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
}); | ||
|
||
const request = page.waitForRequest('**/foo'); | ||
const url = await getLocalTestPath({ testDir: __dirname }); | ||
|
||
await page.goto(url); | ||
await request; | ||
|
||
const breadcrumbs = await page.evaluate(() => { | ||
return (window as unknown as Window & { breadcrumbs: Breadcrumb[] }).breadcrumbs; | ||
}); | ||
|
||
expect(breadcrumbs).toEqual([ | ||
{ | ||
category: 'fetch', | ||
data: { | ||
method: 'POST', | ||
status_code: 200, | ||
url: 'http://localhost:7654/foo', | ||
}, | ||
timestamp: expect.any(Number), | ||
type: 'http', | ||
}, | ||
]); | ||
}); |
14 changes: 14 additions & 0 deletions
14
...es/integration-tests/suites/integrations/browser/breadcrumbs/fetch/requestPayload/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,14 @@ | ||
import * as Sentry from '@sentry/browser'; | ||
import { Breadcrumbs } from '@sentry/browser'; | ||
|
||
window.Sentry = Sentry; | ||
window.breadcrumbs = []; | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
integrations: [new Breadcrumbs({ captureRequestPayload: true })], | ||
beforeBreadcrumb(breadcrumb) { | ||
window.breadcrumbs.push(breadcrumb); | ||
return breadcrumb; | ||
}, | ||
}); |
10 changes: 10 additions & 0 deletions
10
...integration-tests/suites/integrations/browser/breadcrumbs/fetch/requestPayload/subject.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,10 @@ | ||
fetch('http://localhost:7654/foo', { | ||
method: 'POST', | ||
body: '{"foo":"bar"}', | ||
}) | ||
.then(res => { | ||
return res.json(); | ||
}) | ||
.then(json => { | ||
// do something with the response | ||
}); |
42 changes: 42 additions & 0 deletions
42
...es/integration-tests/suites/integrations/browser/breadcrumbs/fetch/requestPayload/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,42 @@ | ||
import { expect } from '@playwright/test'; | ||
import type { Breadcrumb } from '@sentry/types'; | ||
|
||
import { sentryTest } from '../../../../../../utils/fixtures'; | ||
|
||
sentryTest('captures request_payload breadcrumb if configured', async ({ getLocalTestPath, page }) => { | ||
await page.route('**/foo', route => { | ||
return route.fulfill({ | ||
status: 200, | ||
body: JSON.stringify({ | ||
testApi: 'OK', | ||
}), | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
}); | ||
|
||
const request = page.waitForRequest('**/foo'); | ||
const url = await getLocalTestPath({ testDir: __dirname }); | ||
|
||
await page.goto(url); | ||
await request; | ||
|
||
const breadcrumbs = await page.evaluate(() => { | ||
return (window as unknown as Window & { breadcrumbs: Breadcrumb[] }).breadcrumbs; | ||
}); | ||
|
||
expect(breadcrumbs).toEqual([ | ||
{ | ||
category: 'fetch', | ||
data: { | ||
method: 'POST', | ||
request_payload: '{"foo":"bar"}', | ||
status_code: 200, | ||
url: 'http://localhost:7654/foo', | ||
}, | ||
timestamp: expect.any(Number), | ||
type: 'http', | ||
}, | ||
]); | ||
}); |
14 changes: 14 additions & 0 deletions
14
...s/integration-tests/suites/integrations/browser/breadcrumbs/fetch/responsePayload/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,14 @@ | ||
import * as Sentry from '@sentry/browser'; | ||
import { Breadcrumbs } from '@sentry/browser'; | ||
|
||
window.Sentry = Sentry; | ||
window.breadcrumbs = []; | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
integrations: [new Breadcrumbs({ captureResponsePayload: true })], | ||
beforeBreadcrumb(breadcrumb) { | ||
window.breadcrumbs.push(breadcrumb); | ||
return breadcrumb; | ||
}, | ||
}); |
10 changes: 10 additions & 0 deletions
10
...ntegration-tests/suites/integrations/browser/breadcrumbs/fetch/responsePayload/subject.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,10 @@ | ||
fetch('http://localhost:7654/foo', { | ||
method: 'POST', | ||
body: '{"foo":"bar"}', | ||
}) | ||
.then(res => { | ||
return res.json(); | ||
}) | ||
.then(json => { | ||
// do something with the response | ||
}); |
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.
We should probably capture falsy
body
values, no?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.
Good question, wasn't so sure about that. the response body seemed to be
''
when "empty", the request bodyundefined
. We can also just pass this through as-is and just not care what it is! WDYT?