Skip to content

Commit 24cfda2

Browse files
authored
fix(otel): Remove sampling decision in propagator (#6152)
Previously the Sentry Propagator was setting the sample rate on the opentelemetry span context based on the incoming traceparent from `sentry-trace`. This is problematic because it a span is unsampled, it does not enter a span processor. This means that we could create transactions erroneously (for example, unnecessarily promote a child span to a transaction).
1 parent f1ffb6a commit 24cfda2

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

packages/opentelemetry-node/src/propagator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ export class SentryPropagator implements TextMapPropagator {
6262
const traceparentData = extractTraceparentData(header);
6363
newContext = newContext.setValue(SENTRY_TRACE_PARENT_CONTEXT_KEY, traceparentData);
6464
if (traceparentData) {
65-
const traceFlags = traceparentData.parentSampled ? TraceFlags.SAMPLED : TraceFlags.NONE;
6665
const spanContext = {
6766
traceId: traceparentData.traceId || '',
6867
spanId: traceparentData.parentSpanId || '',
6968
isRemote: true,
70-
traceFlags,
69+
// Always sample if traceparent exists, we use SentrySpanProcessor to make sampling decisions with `startTransaction`.
70+
traceFlags: TraceFlags.SAMPLED,
7171
};
7272
newContext = trace.setSpanContext(newContext, spanContext);
7373
}

packages/opentelemetry-node/src/spanprocessor.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ function getTraceData(otelSpan: OtelSpan, parentContext: Context): Partial<Trans
129129
| Partial<DynamicSamplingContext>
130130
| undefined;
131131

132-
return {
132+
const context: Partial<TransactionContext> = {
133133
spanId,
134134
traceId,
135135
parentSpanId,
@@ -139,6 +139,13 @@ function getTraceData(otelSpan: OtelSpan, parentContext: Context): Partial<Trans
139139
source: 'custom',
140140
},
141141
};
142+
143+
// Only inherit sample rate if `traceId` is the same
144+
if (traceparentData && traceId === traceparentData.traceId) {
145+
context.parentSampled = traceparentData.parentSampled;
146+
}
147+
148+
return context;
142149
}
143150

144151
function updateSpanWithOtelData(sentrySpan: SentrySpan, otelSpan: OtelSpan): void {

0 commit comments

Comments
 (0)