Skip to content

Commit

Permalink
fix(otel): Remove sampling decision in propagator (#6152)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
AbhiPrasad committed Nov 9, 2022
1 parent f1ffb6a commit 24cfda2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/opentelemetry-node/src/propagator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ export class SentryPropagator implements TextMapPropagator {
const traceparentData = extractTraceparentData(header);
newContext = newContext.setValue(SENTRY_TRACE_PARENT_CONTEXT_KEY, traceparentData);
if (traceparentData) {
const traceFlags = traceparentData.parentSampled ? TraceFlags.SAMPLED : TraceFlags.NONE;
const spanContext = {
traceId: traceparentData.traceId || '',
spanId: traceparentData.parentSpanId || '',
isRemote: true,
traceFlags,
// Always sample if traceparent exists, we use SentrySpanProcessor to make sampling decisions with `startTransaction`.
traceFlags: TraceFlags.SAMPLED,
};
newContext = trace.setSpanContext(newContext, spanContext);
}
Expand Down
9 changes: 8 additions & 1 deletion packages/opentelemetry-node/src/spanprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ function getTraceData(otelSpan: OtelSpan, parentContext: Context): Partial<Trans
| Partial<DynamicSamplingContext>
| undefined;

return {
const context: Partial<TransactionContext> = {
spanId,
traceId,
parentSpanId,
Expand All @@ -139,6 +139,13 @@ function getTraceData(otelSpan: OtelSpan, parentContext: Context): Partial<Trans
source: 'custom',
},
};

// Only inherit sample rate if `traceId` is the same
if (traceparentData && traceId === traceparentData.traceId) {
context.parentSampled = traceparentData.parentSampled;
}

return context;
}

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

0 comments on commit 24cfda2

Please sign in to comment.