Skip to content

Commit 904b360

Browse files
committed
fix check
1 parent df399c9 commit 904b360

File tree

3 files changed

+41
-21
lines changed

3 files changed

+41
-21
lines changed

packages/node/src/integrations/node-fetch.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type { UndiciRequest, UndiciResponse } from '@opentelemetry/instrumentation-undici';
22
import { UndiciInstrumentation } from '@opentelemetry/instrumentation-undici';
3-
import { getTraceData } from '@sentry/core';
3+
import { getClient, getTraceData, LRUMap } from '@sentry/core';
44
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, addBreadcrumb, defineIntegration, hasTracingEnabled } from '@sentry/core';
55
import { getBreadcrumbLogLevelFromHttpStatusCode, getSanitizedUrlString, parseUrl } from '@sentry/core';
6-
import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry';
6+
import { addOpenTelemetryInstrumentation, shouldPropagateTraceForUrl } from '@sentry/opentelemetry';
77
import type { IntegrationFn, SanitizedRequestData } from '@sentry/types';
88

99
interface NodeFetchOptions {
@@ -27,6 +27,8 @@ const _nativeNodeFetchIntegration = ((options: NodeFetchOptions = {}) => {
2727
return {
2828
name: 'NodeFetch',
2929
setupOnce() {
30+
const propagationDecisionMap = new LRUMap<string, boolean>(100);
31+
3032
const instrumentation = new UndiciInstrumentation({
3133
requireParentforSpans: false,
3234
ignoreRequestHook: request => {
@@ -40,7 +42,11 @@ const _nativeNodeFetchIntegration = ((options: NodeFetchOptions = {}) => {
4042
// If tracing is disabled, we still want to propagate traces
4143
// So we do that manually here, matching what the instrumentation does otherwise
4244
if (!hasTracingEnabled()) {
43-
const addedHeaders = getTraceData();
45+
const tracePropagationTargets = getClient()?.getOptions().tracePropagationTargets;
46+
const addedHeaders = shouldPropagateTraceForUrl(url, tracePropagationTargets, propagationDecisionMap)
47+
? getTraceData()
48+
: {};
49+
4450
const requestHeaders = request.headers;
4551
if (Array.isArray(requestHeaders)) {
4652
Object.entries(addedHeaders).forEach(headers => requestHeaders.push(...headers));

packages/opentelemetry/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ export { setupEventContextTrace } from './setupEventContextTrace';
4545

4646
export { setOpenTelemetryContextAsyncContextStrategy } from './asyncContextStrategy';
4747
export { wrapContextManagerClass } from './contextManager';
48-
export { SentryPropagator, getPropagationContextFromSpan } from './propagator';
48+
export {
49+
SentryPropagator,
50+
getPropagationContextFromSpan,
51+
shouldPropagateTraceForUrl,
52+
} from './propagator';
4953
export { SentrySpanProcessor } from './spanProcessor';
5054
export {
5155
SentrySampler,

packages/opentelemetry/src/propagator.ts

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)