Skip to content

Commit 6aa2ece

Browse files
authored
Change XHR instrumentation ordering (getsentry#2643)
1 parent 5b6ea3d commit 6aa2ece

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- "You miss 100 percent of the chances you don't take. — Wayne Gretzky" — Michael Scott
66
- [react] feat: Add @sentry/react package (#2631)
7+
- [browser] Change XHR instrumentation order to handle `onreadystatechange` breadcrumbs correctly (#2643)
78

89
## 5.16.1
910

packages/utils/src/instrument.ts

+31-22
Original file line numberDiff line numberDiff line change
@@ -218,35 +218,19 @@ function instrumentXHR(): void {
218218

219219
fill(xhrproto, 'open', function(originalOpen: () => void): () => void {
220220
return function(this: SentryWrappedXMLHttpRequest, ...args: any[]): void {
221+
const xhr = this; // tslint:disable-line:no-this-assignment
221222
const url = args[1];
222-
this.__sentry_xhr__ = {
223+
xhr.__sentry_xhr__ = {
223224
method: isString(args[0]) ? args[0].toUpperCase() : args[0],
224225
url: args[1],
225226
};
226227

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

232-
return originalOpen.apply(this, args);
233-
};
234-
});
235-
236-
fill(xhrproto, 'send', function(originalSend: () => void): () => void {
237-
return function(this: SentryWrappedXMLHttpRequest, ...args: any[]): void {
238-
const xhr = this; // tslint:disable-line:no-this-assignment
239-
const commonHandlerData = {
240-
args,
241-
startTimestamp: Date.now(),
242-
xhr,
243-
};
244-
245-
triggerHandlers('xhr', {
246-
...commonHandlerData,
247-
});
248-
249-
xhr.addEventListener('readystatechange', function(): void {
233+
const onreadystatechangeHandler = function(): void {
250234
if (xhr.readyState === 4) {
251235
try {
252236
// touching statusCode in some platforms throws
@@ -258,10 +242,35 @@ function instrumentXHR(): void {
258242
/* do nothing */
259243
}
260244
triggerHandlers('xhr', {
261-
...commonHandlerData,
245+
args,
262246
endTimestamp: Date.now(),
247+
startTimestamp: Date.now(),
248+
xhr,
263249
});
264250
}
251+
};
252+
253+
if ('onreadystatechange' in xhr && typeof xhr.onreadystatechange === 'function') {
254+
fill(xhr, 'onreadystatechange', function(original: WrappedFunction): Function {
255+
return function(...readyStateArgs: any[]): void {
256+
onreadystatechangeHandler();
257+
return original.apply(xhr, readyStateArgs);
258+
};
259+
});
260+
} else {
261+
xhr.addEventListener('readystatechange', onreadystatechangeHandler);
262+
}
263+
264+
return originalOpen.apply(xhr, args);
265+
};
266+
});
267+
268+
fill(xhrproto, 'send', function(originalSend: () => void): () => void {
269+
return function(this: SentryWrappedXMLHttpRequest, ...args: any[]): void {
270+
triggerHandlers('xhr', {
271+
args,
272+
startTimestamp: Date.now(),
273+
xhr: this,
265274
});
266275

267276
return originalSend.apply(this, args);

0 commit comments

Comments
 (0)