Skip to content
Merged
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
17 changes: 15 additions & 2 deletions packages/tracing/src/browser/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import {

import { getActiveTransaction, hasTracingEnabled } from '../utils';

// TODO (v8): Remove `tracingOrigins`
export const DEFAULT_TRACING_ORIGINS = ['localhost', /^\//];
export const DEFAULT_TRACE_PROPAGATION_TARGETS = ['localhost', /^\//];

/** Options for Request Instrumentation */
export interface RequestInstrumentationOptions {
Expand All @@ -22,6 +24,14 @@ export interface RequestInstrumentationOptions {
*/
tracingOrigins: Array<string | RegExp>;

/**
* List of strings and/or regexes used to determine which outgoing requests will have `sentry-trace` and `baggage`
* headers attached.
*
* Default: ['localhost', /^\//] {@see DEFAULT_TRACE_PROPAGATION_TARGETS}
*/
tracePropagationTargets: Array<string | RegExp>;

/**
* Flag to disable patching all together for fetch requests.
*
Expand Down Expand Up @@ -99,19 +109,22 @@ export const defaultRequestInstrumentationOptions: RequestInstrumentationOptions
traceFetch: true,
traceXHR: true,
tracingOrigins: DEFAULT_TRACING_ORIGINS,
tracePropagationTargets: DEFAULT_TRACE_PROPAGATION_TARGETS,
};

/** Registers span creators for xhr and fetch requests */
export function instrumentOutgoingRequests(_options?: Partial<RequestInstrumentationOptions>): void {
const { traceFetch, traceXHR, tracingOrigins, shouldCreateSpanForRequest } = {
const { traceFetch, traceXHR, tracingOrigins, tracePropagationTargets, shouldCreateSpanForRequest } = {
...defaultRequestInstrumentationOptions,
..._options,
};

const shouldCreateSpan =
typeof shouldCreateSpanForRequest === 'function' ? shouldCreateSpanForRequest : (_: string) => true;

const shouldAttachHeaders = (url: string): boolean => tracingOrigins.some(origin => isMatchingPattern(url, origin));
const shouldAttachHeaders = (url: string): boolean =>
tracingOrigins.some(origin => isMatchingPattern(url, origin)) ||
tracePropagationTargets.some(origin => isMatchingPattern(url, origin));

const spans: Record<string, Span> = {};

Expand Down