@@ -89,11 +89,7 @@ export class SentryPropagator extends W3CBaggagePropagator {
89
89
const url = activeSpan && getCurrentURL ( activeSpan ) ;
90
90
91
91
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 ) ) {
97
93
DEBUG_BUILD &&
98
94
logger . log (
99
95
'[Tracing] Not injecting trace data for url because it does not match tracePropagationTargets:' ,
@@ -169,22 +165,36 @@ export class SentryPropagator extends W3CBaggagePropagator {
169
165
public fields ( ) : string [ ] {
170
166
return [ SENTRY_TRACE_HEADER , SENTRY_BAGGAGE_HEADER ] ;
171
167
}
168
+ }
172
169
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:' ;
178
172
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
+ }
183
186
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 ;
187
191
}
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 ;
188
198
}
189
199
190
200
/**
0 commit comments