Skip to content

Commit

Permalink
fix(node): Avoid setting sentry-trace header multiple times
Browse files Browse the repository at this point in the history
If, for whatever reason, `propagator.inject()` is called multiple times, we do not want to add multiple `sentry-trace` headers. In this case, the first one wins.
  • Loading branch information
mydea committed Jan 30, 2025
1 parent 668c276 commit f44d6a7
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion packages/opentelemetry/src/propagator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ export class SentryPropagator extends W3CBaggagePropagator {
}

// We also want to avoid setting the default OTEL trace ID, if we get that for whatever reason
if (traceId && traceId !== INVALID_TRACEID) {
// If a sentry-trace header already exists, we do nothing
const existingSentryTrace = getExistingSentryTrace(carrier);
if (traceId && traceId !== INVALID_TRACEID && !existingSentryTrace) {
setter.set(carrier, SENTRY_TRACE_HEADER, generateSentryTraceHeader(traceId, spanId, sampled));
}

Expand Down Expand Up @@ -264,6 +266,16 @@ function getExistingBaggage(carrier: unknown): string | undefined {
}
}

/** Try to get the existing sentry-trace header. */
function getExistingSentryTrace(carrier: unknown): string | undefined {
try {
const sentryTrace = (carrier as Record<string, string | string[]>)[SENTRY_TRACE_HEADER];
return Array.isArray(sentryTrace) ? sentryTrace.join(',') : sentryTrace;
} catch {
return undefined;
}
}

/**
* It is pretty tricky to get access to the outgoing request URL of a request in the propagator.
* As we only have access to the context of the span to be sent and the carrier (=headers),
Expand Down

0 comments on commit f44d6a7

Please sign in to comment.