Skip to content

Commit bd528c9

Browse files
committed
fix: Correctly send span id in trace header for xhr
1 parent 66a1f02 commit bd528c9

File tree

1 file changed

+21
-72
lines changed

1 file changed

+21
-72
lines changed

packages/apm/src/integrations/tracing.ts

Lines changed: 21 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,6 @@ export class Tracing implements Integration {
8585
*/
8686
public static id: string = 'Tracing';
8787

88-
/**
89-
* If we have an xhr we need to store the url in the instance.
90-
*
91-
*/
92-
// @ts-ignore
93-
private _xhrUrl?: string;
94-
9588
/**
9689
* Is Tracing enabled, this will be determined once per pageload.
9790
*/
@@ -109,7 +102,7 @@ export class Tracing implements Integration {
109102

110103
private static _currentIndex: number = 0;
111104

112-
private static readonly _activities: { [key: number]: Activity } = {};
105+
public static readonly _activities: { [key: number]: Activity } = {};
113106

114107
private static _debounce: number = 0;
115108

@@ -124,7 +117,10 @@ export class Tracing implements Integration {
124117
idleTimeout: 500,
125118
shouldCreateSpanForRequest(url: string): boolean {
126119
const origins = (_options && _options.tracingOrigins) || defaultTracingOrigins;
127-
return origins.some((origin: string | RegExp) => isMatchingPattern(url, origin));
120+
return (
121+
origins.some((origin: string | RegExp) => isMatchingPattern(url, origin)) &&
122+
!isMatchingPattern(url, 'sentry_key')
123+
);
128124
},
129125
startTransactionOnLocationChange: true,
130126
traceFetch: true,
@@ -160,7 +156,6 @@ export class Tracing implements Integration {
160156
callback: xhrCallback,
161157
type: 'xhr',
162158
});
163-
this._traceXHR(getCurrentHub);
164159
}
165160
// tslint:disable-next-line: no-non-null-assertion
166161
if (this._options!.traceFetch !== false) {
@@ -188,65 +183,6 @@ export class Tracing implements Integration {
188183
}
189184
}
190185

191-
/**
192-
* JSDoc
193-
*/
194-
private _traceXHR(getCurrentHub: () => Hub): void {
195-
if (!('XMLHttpRequest' in getGlobalObject<Window>())) {
196-
return;
197-
}
198-
199-
const xhrproto = XMLHttpRequest.prototype;
200-
201-
fill(
202-
xhrproto,
203-
'open',
204-
originalOpen =>
205-
function(this: XMLHttpRequest, ...args: any[]): void {
206-
// @ts-ignore
207-
const self = getCurrentHub().getIntegration(Tracing);
208-
if (self) {
209-
self._xhrUrl = args[1] as string;
210-
}
211-
// tslint:disable-next-line: no-unsafe-any
212-
return originalOpen.apply(this, args);
213-
},
214-
);
215-
216-
fill(
217-
xhrproto,
218-
'send',
219-
originalSend =>
220-
function(this: XMLHttpRequest, ...args: any[]): void {
221-
// @ts-ignore
222-
const self = getCurrentHub().getIntegration(Tracing);
223-
// tslint:disable-next-line: no-non-null-assertion
224-
if (self && self._xhrUrl && self._options!.tracingOrigins) {
225-
const url = self._xhrUrl;
226-
const headers = getCurrentHub().traceHeaders();
227-
// tslint:disable-next-line: prefer-for-of no-non-null-assertion
228-
let isWhitelisted = self._options!.tracingOrigins.some((origin: string | RegExp) =>
229-
isMatchingPattern(url, origin),
230-
);
231-
232-
if (isMatchingPattern(url, 'sentry_key')) {
233-
// If sentry_key is in the url, it's an internal store request to sentry
234-
// we do not want to add the trace header to store requests
235-
isWhitelisted = false;
236-
}
237-
238-
if (isWhitelisted && this.setRequestHeader) {
239-
Object.keys(headers).forEach(key => {
240-
this.setRequestHeader(key, headers[key]);
241-
});
242-
}
243-
}
244-
// tslint:disable-next-line: no-unsafe-any
245-
return originalSend.apply(this, args);
246-
},
247-
);
248-
}
249-
250186
/**
251187
* JSDoc
252188
*/
@@ -508,6 +444,15 @@ function xhrCallback(handlerData: { [key: string]: any }): void {
508444
description: `${xhr.method} ${xhr.url}`,
509445
op: 'http',
510446
});
447+
448+
// Adding the trace header to the span
449+
const activity = Tracing._activities[handlerData.xhr.__sentry_xhr_activity_id__];
450+
if (activity) {
451+
const span = activity.span;
452+
if (span && handlerData.xhr.setRequestHeader) {
453+
handlerData.xhr.setRequestHeader('sentry-trace', span.toTraceparent());
454+
}
455+
}
511456
// tslint:enable: no-unsafe-any
512457
}
513458

@@ -520,10 +465,14 @@ function fetchCallback(handlerData: { [key: string]: any }): void {
520465
return;
521466
}
522467

523-
if (handlerData.endTimestamp && handlerData.__activity) {
524-
Tracing.popActivity(handlerData.__activity, handlerData.fetchData);
468+
if (!Tracing.options.shouldCreateSpanForRequest(handlerData.fetchData.url)) {
469+
return;
470+
}
471+
472+
if (handlerData.endTimestamp && handlerData.fetchData.__activity) {
473+
Tracing.popActivity(handlerData.fetchData.__activity, handlerData.fetchData);
525474
} else {
526-
handlerData.__activity = Tracing.pushActivity('fetch', {
475+
handlerData.fetchData.__activity = Tracing.pushActivity('fetch', {
527476
data: {
528477
...handlerData.fetchData,
529478
type: 'fetch',

0 commit comments

Comments
 (0)