Skip to content

feat: Give access to XHR requests body in breadcrumb hint #2904

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 1 commit into from
Sep 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/browser/src/integrations/breadcrumbs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ export class Breadcrumbs implements Integration {
},
{
xhr: handlerData.xhr,
...(handlerData.xhr.__sentry_xhr__.body && { input: handlerData.xhr.__sentry_xhr__.body }),
},
);

Expand Down
30 changes: 30 additions & 0 deletions packages/browser/test/integration/suites/breadcrumbs.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,36 @@ describe("breadcrumbs", function() {
assert.equal(summary.breadcrumbs[0].type, "http");
assert.equal(summary.breadcrumbs[0].category, "xhr");
assert.equal(summary.breadcrumbs[0].data.method, "GET");
// To make sure that we are not providing this key for non-post requests
assert.equal(summary.breadcrumbHints[0].input, undefined);
});
}
);

it(
optional(
"should give access to request body for XMLHttpRequest POST requests",
IS_LOADER
),
function() {
return runInSandbox(sandbox, { manual: true }, function() {
var xhr = new XMLHttpRequest();
xhr.open("POST", "/base/subjects/example.json");
xhr.send('{"foo":"bar"}');
waitForXHR(xhr, function() {
Sentry.captureMessage("test");
window.finalizeManualTest();
});
}).then(function(summary) {
// The async loader doesn't wrap XHR
if (IS_LOADER) {
return;
}
assert.equal(summary.breadcrumbs.length, 1);
assert.equal(summary.breadcrumbs[0].type, "http");
assert.equal(summary.breadcrumbs[0].category, "xhr");
assert.equal(summary.breadcrumbs[0].data.method, "POST");
assert.equal(summary.breadcrumbHints[0].input, '{"foo":"bar"}');
});
}
);
Expand Down
24 changes: 24 additions & 0 deletions packages/utils/src/instrument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,16 @@ function instrumentFetch(): void {
});
}

type XHRSendInput = null | Blob | BufferSource | FormData | URLSearchParams | string;

/** JSDoc */
interface SentryWrappedXMLHttpRequest extends XMLHttpRequest {
[key: string]: any;
__sentry_xhr__?: {
method?: string;
url?: string;
status_code?: number;
body?: XHRSendInput;
};
}

Expand Down Expand Up @@ -220,6 +223,9 @@ function instrumentXHR(): void {
return;
}

// Poor man implementation of ES6 `Map` by tracking and keeping in sync key and value separately.
Copy link
Contributor

@rhcarvalho rhcarvalho Sep 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could concurrent requests get this "map" out of sync? I think not, no pre-emption.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, it's not Go 😅

const requestKeys: XMLHttpRequest[] = [];
const requestValues: Array<any>[] = [];
const xhrproto = XMLHttpRequest.prototype;

fill(xhrproto, 'open', function(originalOpen: () => void): () => void {
Expand Down Expand Up @@ -250,6 +256,21 @@ function instrumentXHR(): void {
} catch (e) {
/* do nothing */
}

try {
const requestPos = requestKeys.indexOf(xhr);
if (requestPos !== -1) {
// Make sure to pop both, key and value to keep it in sync.
requestKeys.splice(requestPos);
const args = requestValues.splice(requestPos)[0];
if (xhr.__sentry_xhr__ && args[0] !== undefined) {
xhr.__sentry_xhr__.body = args[0] as XHRSendInput;
}
}
} catch (e) {
/* do nothing */
}

triggerHandlers('xhr', {
args,
endTimestamp: Date.now(),
Expand All @@ -276,6 +297,9 @@ function instrumentXHR(): void {

fill(xhrproto, 'send', function(originalSend: () => void): () => void {
return function(this: SentryWrappedXMLHttpRequest, ...args: any[]): void {
requestKeys.push(this);
requestValues.push(args);

triggerHandlers('xhr', {
args,
startTimestamp: Date.now(),
Expand Down