Skip to content

Commit 07af71c

Browse files
committed
ref(node): Avoid using propagator for node-fetch TWP instrumentation
Instead, use the new and improved `getTraceData` directly, which avoids URL issues etc. and just gets the trace data.
1 parent 02be07d commit 07af71c

File tree

4 files changed

+30
-32
lines changed

4 files changed

+30
-32
lines changed

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

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
1-
import { context, propagation, trace } from '@opentelemetry/api';
21
import type { UndiciRequest, UndiciResponse } from '@opentelemetry/instrumentation-undici';
32
import { UndiciInstrumentation } from '@opentelemetry/instrumentation-undici';
4-
import {
5-
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
6-
addBreadcrumb,
7-
defineIntegration,
8-
getCurrentScope,
9-
hasTracingEnabled,
10-
} from '@sentry/core';
3+
import { getTraceData } from '@sentry/core';
4+
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, addBreadcrumb, defineIntegration, hasTracingEnabled } from '@sentry/core';
115
import { getBreadcrumbLogLevelFromHttpStatusCode, getSanitizedUrlString, parseUrl } from '@sentry/core';
12-
import {
13-
addOpenTelemetryInstrumentation,
14-
generateSpanContextForPropagationContext,
15-
getPropagationContextFromSpan,
16-
} from '@sentry/opentelemetry';
6+
import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry';
177
import type { IntegrationFn, SanitizedRequestData } from '@sentry/types';
188

199
interface NodeFetchOptions {
@@ -50,23 +40,7 @@ const _nativeNodeFetchIntegration = ((options: NodeFetchOptions = {}) => {
5040
// If tracing is disabled, we still want to propagate traces
5141
// So we do that manually here, matching what the instrumentation does otherwise
5242
if (!hasTracingEnabled()) {
53-
const ctx = context.active();
54-
const addedHeaders: Record<string, string> = {};
55-
56-
// We generate a virtual span context from the active one,
57-
// Where we attach the URL to the trace state, so the propagator can pick it up
58-
const activeSpan = trace.getSpan(ctx);
59-
const propagationContext = activeSpan
60-
? getPropagationContextFromSpan(activeSpan)
61-
: getCurrentScope().getPropagationContext();
62-
63-
const spanContext = generateSpanContextForPropagationContext(propagationContext);
64-
// We know that in practice we'll _always_ haven a traceState here
65-
spanContext.traceState = spanContext.traceState?.set('sentry.url', url);
66-
const ctxWithUrlTraceState = trace.setSpanContext(ctx, spanContext);
67-
68-
propagation.inject(ctxWithUrlTraceState, addedHeaders);
69-
43+
const addedHeaders = getTraceData();
7044
const requestHeaders = request.headers;
7145
if (Array.isArray(requestHeaders)) {
7246
Object.entries(addedHeaders).forEach(headers => requestHeaders.push(...headers));

packages/opentelemetry/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export { getDynamicSamplingContextFromSpan } from '@sentry/core';
2424
export { isSentryRequestSpan } from './utils/isSentryRequest';
2525

2626
export { enhanceDscWithOpenTelemetryRootSpanName } from './utils/enhanceDscWithOpenTelemetryRootSpanName';
27+
// eslint-disable-next-line deprecation/deprecation
2728
export { generateSpanContextForPropagationContext } from './utils/generateSpanContextForPropagationContext';
2829

2930
export { getActiveSpan } from './utils/getActiveSpan';

packages/opentelemetry/src/propagator.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import type { Baggage, Context, Span, TextMapGetter, TextMapSetter } from '@opentelemetry/api';
1+
import type { Baggage, Context, Span, SpanContext, TextMapGetter, TextMapSetter} from '@opentelemetry/api';
2+
import { TraceFlags } from '@opentelemetry/api';
23
import { INVALID_TRACEID } from '@opentelemetry/api';
34
import { context } from '@opentelemetry/api';
45
import { propagation, trace } from '@opentelemetry/api';
@@ -30,8 +31,8 @@ import {
3031
} from './constants';
3132
import { DEBUG_BUILD } from './debug-build';
3233
import { getScopesFromContext, setScopesOnContext } from './utils/contextData';
33-
import { generateSpanContextForPropagationContext } from './utils/generateSpanContextForPropagationContext';
3434
import { getSamplingDecision } from './utils/getSamplingDecision';
35+
import { makeTraceState } from './utils/makeTraceState';
3536
import { setIsSetup } from './utils/setupCheck';
3637

3738
/** Get the Sentry propagation context from a span context. */
@@ -287,3 +288,23 @@ function getCurrentURL(span: Span): string | undefined {
287288

288289
return undefined;
289290
}
291+
292+
// TODO: Adjust this behavior to avoid invalid spans
293+
function generateSpanContextForPropagationContext(propagationContext: PropagationContext): SpanContext {
294+
// We store the DSC as OTEL trace state on the span context
295+
const traceState = makeTraceState({
296+
parentSpanId: propagationContext.parentSpanId,
297+
dsc: propagationContext.dsc,
298+
sampled: propagationContext.sampled,
299+
});
300+
301+
const spanContext: SpanContext = {
302+
traceId: propagationContext.traceId,
303+
spanId: propagationContext.parentSpanId || '',
304+
isRemote: true,
305+
traceFlags: propagationContext.sampled ? TraceFlags.SAMPLED : TraceFlags.NONE,
306+
traceState,
307+
};
308+
309+
return spanContext;
310+
}

packages/opentelemetry/src/utils/generateSpanContextForPropagationContext.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { makeTraceState } from './makeTraceState';
66
/**
77
* Generates a SpanContext that represents a PropagationContext.
88
* This can be set on a `context` to make this a (virtual) active span.
9+
*
10+
* @deprecated This function is deprecated and will be removed in the next major version.
911
*/
1012
export function generateSpanContextForPropagationContext(propagationContext: PropagationContext): SpanContext {
1113
// We store the DSC as OTEL trace state on the span context

0 commit comments

Comments
 (0)