Skip to content

Commit c2cb28d

Browse files
committed
WIP: skip next emitted spans??
1 parent d5ac938 commit c2cb28d

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

packages/opentelemetry/src/sampler.ts

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ export class SentrySampler implements Sampler {
5252
return { decision: SamplingDecision.NOT_RECORD, traceState };
5353
}
5454

55+
// If we encounter a span emitted by Next.js, we do not want to sample it
56+
// The reason for this is that the data quality of the spans varies, it is different per version of Next,
57+
// and we need to keep our manual instrumentation around for the edge runtime anyhow.
58+
if (spanAttributes['next.span_type']) {
59+
return { decision: SamplingDecision.NOT_RECORD, traceState: traceState };
60+
}
61+
5562
// If we have a http.client span that has no local parent, we never want to sample it
5663
// but we want to leave downstream sampling decisions up to the server
5764
if (
@@ -62,20 +69,7 @@ export class SentrySampler implements Sampler {
6269
return { decision: SamplingDecision.NOT_RECORD, traceState };
6370
}
6471

65-
let parentSampled: boolean | undefined = undefined;
66-
67-
// Only inherit sample rate if `traceId` is the same
68-
// Note for testing: `isSpanContextValid()` checks the format of the traceId/spanId, so we need to pass valid ones
69-
if (parentSpan && parentContext && isSpanContextValid(parentContext) && parentContext.traceId === traceId) {
70-
if (parentContext.isRemote) {
71-
parentSampled = getParentRemoteSampled(parentSpan);
72-
DEBUG_BUILD &&
73-
logger.log(`[Tracing] Inheriting remote parent's sampled decision for ${spanName}: ${parentSampled}`);
74-
} else {
75-
parentSampled = getSamplingDecision(parentContext);
76-
DEBUG_BUILD && logger.log(`[Tracing] Inheriting parent's sampled decision for ${spanName}: ${parentSampled}`);
77-
}
78-
}
72+
const parentSampled = parentSpan ? getParentSampled(parentSpan, traceId, spanName) : undefined;
7973

8074
const [sampled, sampleRate] = sampleSpan(options, {
8175
name: spanName,
@@ -129,3 +123,24 @@ function getParentRemoteSampled(parentSpan: Span): boolean | undefined {
129123
// Only inherit sampled if `traceId` is the same
130124
return traceparentData && traceId === traceparentData.traceId ? traceparentData.sampled : undefined;
131125
}
126+
127+
function getParentSampled(parentSpan: Span, traceId: string, spanName: string): boolean | undefined {
128+
const parentContext = parentSpan.spanContext();
129+
130+
// Only inherit sample rate if `traceId` is the same
131+
// Note for testing: `isSpanContextValid()` checks the format of the traceId/spanId, so we need to pass valid ones
132+
if (isSpanContextValid(parentContext) && parentContext.traceId === traceId) {
133+
if (parentContext.isRemote) {
134+
const parentSampled = getParentRemoteSampled(parentSpan);
135+
DEBUG_BUILD &&
136+
logger.log(`[Tracing] Inheriting remote parent's sampled decision for ${spanName}: ${parentSampled}`);
137+
return parentSampled;
138+
}
139+
140+
const parentSampled = getSamplingDecision(parentContext);
141+
DEBUG_BUILD && logger.log(`[Tracing] Inheriting parent's sampled decision for ${spanName}: ${parentSampled}`);
142+
return parentSampled;
143+
}
144+
145+
return undefined;
146+
}

0 commit comments

Comments
 (0)