Skip to content

Commit b41142c

Browse files
committed
feat: Add shouldCreateSpanForRequest option
1 parent 856dbd5 commit b41142c

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

packages/apm/src/integrations/tracing.ts

+29-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ interface TracingOptions {
66
tracingOrigins: Array<string | RegExp>;
77
traceFetch: boolean;
88
traceXHR: boolean;
9+
/**
10+
* This function will be called before creating a span for a request with the given url.
11+
* Return false if you don't want a span for the given url.
12+
*
13+
* By default it uses the `tracingOrigins` options as a url match.
14+
*/
15+
shouldCreateSpanForRequest(url: string): boolean;
916
idleTimeout: number;
1017
startTransactionOnLocationChange: boolean;
1118
tracesSampleRate: number;
@@ -70,6 +77,10 @@ export class Tracing implements Integration {
7077
const defaultTracingOrigins = ['localhost', /^\//];
7178
const defaults = {
7279
idleTimeout: 500,
80+
shouldCreateSpanForRequest(url: string): boolean {
81+
const origins = (_options && _options.tracingOrigins) || defaultTracingOrigins;
82+
return origins.some((origin: string | RegExp) => isMatchingPattern(url, origin));
83+
},
7384
startTransactionOnLocationChange: true,
7485
traceFetch: true,
7586
traceXHR: true,
@@ -393,17 +404,32 @@ export class Tracing implements Integration {
393404
* Creates breadcrumbs from XHR API calls
394405
*/
395406
function xhrCallback(handlerData: { [key: string]: any }): void {
407+
if (!Tracing.options.traceXHR) {
408+
return;
409+
}
410+
411+
// tslint:disable-next-line: no-unsafe-any
412+
if (!handlerData || !handlerData.xhr || !handlerData.xhr.__sentry_xhr__) {
413+
return;
414+
}
415+
396416
// tslint:disable: no-unsafe-any
397-
if (handlerData.requestComplete && handlerData.xhr.__sentry_xhr_activity_id__) {
398-
Tracing.popActivity(handlerData.xhr.__sentry_xhr_activity_id__, handlerData.xhr.__sentry_xhr__);
417+
const xhr = handlerData.xhr.__sentry_xhr__;
418+
419+
if (!Tracing.options.shouldCreateSpanForRequest(xhr.url)) {
399420
return;
400421
}
422+
401423
// We only capture complete, non-sentry requests
402424
if (handlerData.xhr.__sentry_own_request__) {
403425
return;
404426
}
405427

406-
const xhr = handlerData.xhr.__sentry_xhr__;
428+
if (handlerData.requestComplete && handlerData.xhr.__sentry_xhr_activity_id__) {
429+
Tracing.popActivity(handlerData.xhr.__sentry_xhr_activity_id__, handlerData.xhr.__sentry_xhr__);
430+
return;
431+
}
432+
407433
handlerData.xhr.__sentry_xhr_activity_id__ = Tracing.pushActivity('xhr', {
408434
data: {
409435
request_data: xhr.data,

0 commit comments

Comments
 (0)