@@ -89,11 +89,7 @@ export class SentryPropagator extends W3CBaggagePropagator {
8989 const url = activeSpan && getCurrentURL ( activeSpan ) ;
9090
9191 const tracePropagationTargets = getClient ( ) ?. getOptions ( ) ?. tracePropagationTargets ;
92- if (
93- typeof url === 'string' &&
94- tracePropagationTargets &&
95- ! this . _shouldInjectTraceData ( tracePropagationTargets , url )
96- ) {
92+ if ( ! shouldPropagateTraceForUrl ( url , tracePropagationTargets , this . _urlMatchesTargetsMap ) ) {
9793 DEBUG_BUILD &&
9894 logger . log (
9995 '[Tracing] Not injecting trace data for url because it does not match tracePropagationTargets:' ,
@@ -169,22 +165,36 @@ export class SentryPropagator extends W3CBaggagePropagator {
169165 public fields ( ) : string [ ] {
170166 return [ SENTRY_TRACE_HEADER , SENTRY_BAGGAGE_HEADER ] ;
171167 }
168+ }
172169
173- /** If we want to inject trace data for a given URL. */
174- private _shouldInjectTraceData ( tracePropagationTargets : Options [ 'tracePropagationTargets' ] , url : string ) : boolean {
175- if ( tracePropagationTargets === undefined ) {
176- return true ;
177- }
170+ const NOT_PROPAGATED_MESSAGE =
171+ '[Tracing] Not injecting trace data for url because it does not match tracePropagationTargets:' ;
178172
179- const cachedDecision = this . _urlMatchesTargetsMap . get ( url ) ;
180- if ( cachedDecision !== undefined ) {
181- return cachedDecision ;
182- }
173+ /**
174+ * Check if a given URL should be propagated to or not.
175+ * If no url is defined, or no trace propagation targets are defined, this will always return `true`.
176+ * You can also optionally provide a decision map, to cache decisions and avoid repeated regex lookups.
177+ */
178+ export function shouldPropagateTraceForUrl (
179+ url : string | undefined ,
180+ tracePropagationTargets : Options [ 'tracePropagationTargets' ] ,
181+ decisionMap ?: LRUMap < string , boolean > ,
182+ ) : boolean {
183+ if ( typeof url !== 'string' || ! tracePropagationTargets ) {
184+ return true ;
185+ }
183186
184- const decision = stringMatchesSomePattern ( url , tracePropagationTargets ) ;
185- this . _urlMatchesTargetsMap . set ( url , decision ) ;
186- return decision ;
187+ const cachedDecision = decisionMap ?. get ( url ) ;
188+ if ( cachedDecision !== undefined ) {
189+ DEBUG_BUILD && ! cachedDecision && logger . log ( NOT_PROPAGATED_MESSAGE , url ) ;
190+ return cachedDecision ;
187191 }
192+
193+ const decision = stringMatchesSomePattern ( url , tracePropagationTargets ) ;
194+ decisionMap ?. set ( url , decision ) ;
195+
196+ DEBUG_BUILD && ! decision && logger . log ( NOT_PROPAGATED_MESSAGE , url ) ;
197+ return decision ;
188198}
189199
190200/**
0 commit comments