Skip to content
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

Change XHR instrumentation ordering #2643

Merged
merged 3 commits into from Jun 8, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 19 additions & 21 deletions packages/utils/src/instrument.ts
Expand Up @@ -218,34 +218,18 @@ function instrumentXHR(): void {

fill(xhrproto, 'open', function(originalOpen: () => void): () => void {
return function(this: SentryWrappedXMLHttpRequest, ...args: any[]): void {
const xhr = this; // tslint:disable-line:no-this-assignment
const url = args[1];
this.__sentry_xhr__ = {
xhr.__sentry_xhr__ = {
method: isString(args[0]) ? args[0].toUpperCase() : args[0],
url: args[1],
};

// if Sentry key appears in URL, don't capture it as a request
if (isString(url) && this.__sentry_xhr__.method === 'POST' && url.match(/sentry_key/)) {
this.__sentry_own_request__ = true;
if (isString(url) && xhr.__sentry_xhr__.method === 'POST' && url.match(/sentry_key/)) {
xhr.__sentry_own_request__ = true;
}

return originalOpen.apply(this, args);
};
});

fill(xhrproto, 'send', function(originalSend: () => void): () => void {
return function(this: SentryWrappedXMLHttpRequest, ...args: any[]): void {
const xhr = this; // tslint:disable-line:no-this-assignment
const commonHandlerData = {
args,
startTimestamp: Date.now(),
xhr,
};

triggerHandlers('xhr', {
...commonHandlerData,
});

xhr.addEventListener('readystatechange', function(): void {
if (xhr.readyState === 4) {
try {
Expand All @@ -258,12 +242,26 @@ function instrumentXHR(): void {
/* do nothing */
}
triggerHandlers('xhr', {
...commonHandlerData,
args,
endTimestamp: Date.now(),
startTimestamp: Date.now(),
xhr,
});
}
});

return originalOpen.apply(xhr, args);
};
});

fill(xhrproto, 'send', function(originalSend: () => void): () => void {
return function(this: SentryWrappedXMLHttpRequest, ...args: any[]): void {
triggerHandlers('xhr', {
args,
startTimestamp: Date.now(),
xhr: this,
});

return originalSend.apply(this, args);
};
});
Expand Down