Skip to content

Commit ef9916a

Browse files
authored
ref(core): Move shouldPropagateTraceForUrl from opentelemetry to core (#19254)
This function is needed in an upcoming pr by node-core/light, and since node-core/light does not depend on OpenTelemetry we need to move this to core and re-export it in `@sentry/opentelemetry` for backwards compatibility. Closes #19255 (added automatically)
1 parent e62d70b commit ef9916a

File tree

3 files changed

+40
-31
lines changed

3 files changed

+40
-31
lines changed

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ export { _setSpanForScope as _INTERNAL_setSpanForScope } from './utils/spanOnSco
9292
export { parseSampleRate } from './utils/parseSampleRate';
9393
export { applySdkMetadata } from './utils/sdkMetadata';
9494
export { getTraceData } from './utils/traceData';
95+
export { shouldPropagateTraceForUrl } from './utils/tracePropagationTargets';
9596
export { getTraceMetaTags } from './utils/meta';
9697
export { debounce } from './utils/debounce';
9798
export {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { DEBUG_BUILD } from '../debug-build';
2+
import type { CoreOptions as Options } from '../types-hoist/options';
3+
import { debug } from './debug-logger';
4+
import type { LRUMap } from './lru';
5+
import { stringMatchesSomePattern } from './string';
6+
7+
const NOT_PROPAGATED_MESSAGE =
8+
'[Tracing] Not injecting trace data for url because it does not match tracePropagationTargets:';
9+
10+
/**
11+
* Check if a given URL should be propagated to or not.
12+
* If no url is defined, or no trace propagation targets are defined, this will always return `true`.
13+
* You can also optionally provide a decision map, to cache decisions and avoid repeated regex lookups.
14+
*/
15+
export function shouldPropagateTraceForUrl(
16+
url: string | undefined,
17+
tracePropagationTargets: Options['tracePropagationTargets'],
18+
decisionMap?: LRUMap<string, boolean>,
19+
): boolean {
20+
if (typeof url !== 'string' || !tracePropagationTargets) {
21+
return true;
22+
}
23+
24+
const cachedDecision = decisionMap?.get(url);
25+
if (cachedDecision !== undefined) {
26+
DEBUG_BUILD && !cachedDecision && debug.log(NOT_PROPAGATED_MESSAGE, url);
27+
return cachedDecision;
28+
}
29+
30+
const decision = stringMatchesSomePattern(url, tracePropagationTargets);
31+
decisionMap?.set(url, decision);
32+
33+
DEBUG_BUILD && !decision && debug.log(NOT_PROPAGATED_MESSAGE, url);
34+
return decision;
35+
}

packages/opentelemetry/src/propagator.ts

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Baggage, Context, Span, SpanContext, TextMapGetter, TextMapSetter
22
import { context, INVALID_TRACEID, propagation, trace, TraceFlags } from '@opentelemetry/api';
33
import { isTracingSuppressed, W3CBaggagePropagator } from '@opentelemetry/core';
44
import { ATTR_URL_FULL, SEMATTRS_HTTP_URL } from '@opentelemetry/semantic-conventions';
5-
import type { Client, continueTrace, DynamicSamplingContext, Options, Scope } from '@sentry/core';
5+
import type { Client, continueTrace, DynamicSamplingContext, Scope } from '@sentry/core';
66
import {
77
baggageHeaderToDynamicSamplingContext,
88
debug,
@@ -18,8 +18,8 @@ import {
1818
propagationContextFromHeaders,
1919
SENTRY_BAGGAGE_KEY_PREFIX,
2020
shouldContinueTrace,
21+
shouldPropagateTraceForUrl,
2122
spanToJSON,
22-
stringMatchesSomePattern,
2323
} from '@sentry/core';
2424
import { SENTRY_BAGGAGE_HEADER, SENTRY_TRACE_HEADER, SENTRY_TRACE_STATE_URL } from './constants';
2525
import { DEBUG_BUILD } from './debug-build';
@@ -124,35 +124,8 @@ export class SentryPropagator extends W3CBaggagePropagator {
124124
}
125125
}
126126

127-
const NOT_PROPAGATED_MESSAGE =
128-
'[Tracing] Not injecting trace data for url because it does not match tracePropagationTargets:';
129-
130-
/**
131-
* Check if a given URL should be propagated to or not.
132-
* If no url is defined, or no trace propagation targets are defined, this will always return `true`.
133-
* You can also optionally provide a decision map, to cache decisions and avoid repeated regex lookups.
134-
*/
135-
export function shouldPropagateTraceForUrl(
136-
url: string | undefined,
137-
tracePropagationTargets: Options['tracePropagationTargets'],
138-
decisionMap?: LRUMap<string, boolean>,
139-
): boolean {
140-
if (typeof url !== 'string' || !tracePropagationTargets) {
141-
return true;
142-
}
143-
144-
const cachedDecision = decisionMap?.get(url);
145-
if (cachedDecision !== undefined) {
146-
DEBUG_BUILD && !cachedDecision && debug.log(NOT_PROPAGATED_MESSAGE, url);
147-
return cachedDecision;
148-
}
149-
150-
const decision = stringMatchesSomePattern(url, tracePropagationTargets);
151-
decisionMap?.set(url, decision);
152-
153-
DEBUG_BUILD && !decision && debug.log(NOT_PROPAGATED_MESSAGE, url);
154-
return decision;
155-
}
127+
// Re-exported from @sentry/core for backwards compatibility
128+
export { shouldPropagateTraceForUrl } from '@sentry/core';
156129

157130
/**
158131
* Get propagation injection data for the given context.

0 commit comments

Comments
 (0)